【发布时间】:2013-01-17 19:38:09
【问题描述】:
您好,我正在尝试构建类似水平画廊的东西,我可以在其中使用画廊/相机中的图像添加或删除页面。
我正在尝试使其适用于大型位图,因此我使用一种算法来缩放位图并将其设置为每个页面的图像视图。 该算法需要 ImageView 的宽度/高度(按比例缩小)。
我遇到的问题是,当我的自定义PagerAdapter方法执行时,ImageView的宽度/高度还不知道(getWidth/getHeight返回0),所以它不起作用:
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.document_page, null);
((ViewPager) collection).addView(view, 0);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
// Obtain the image file URI
// Call algorithm to get scaled bitmap using ImageView width and height --> PROBLEM: imageView.getWidth()/Height() return 0!!
// Set ImageView with scaled bitmap to avoid OutOfMemory Exception
return view;
}
你有什么建议?
谢谢。
【问题讨论】:
-
在观看 Chet Haase 的两个小教程时,我再次想到了这个问题。它们刚刚被上传并涵盖了
inSampleSize(youtube.com/watch?v=12cB7gnL6po) 和一般的位图加载性能 (youtube.com/watch?v=rsQet4nBVi8)。请务必检查您是否可以使用一些知识来优化您的代码并避免异常。 -
最后,你解决了吗?我也有同样的问题
标签: android android-viewpager android-imageview