Android GalleryView 回收
我正在使用 GalleryView 来处理大约 40 张图像,而且速度很慢,因为没有回收...
任何人都可以向我展示在 getView
方法上对 GalleryView
的基本回收。
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
I'm using GalleryView with ~40 images, and so slow because no recycling...
Anyone can show me a basic recycling to GalleryView
on getView
method.
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.sample_1,
R.drawable.sample_2,
R.drawable.sample_3,
R.drawable.sample_4,
R.drawable.sample_5,
R.drawable.sample_6,
R.drawable.sample_7
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray a = c.obtainStyledAttributes(R.styleable.HelloGallery);
mGalleryItemBackground = a.getResourceId(
R.styleable.HelloGallery_android_galleryItemBackground, 0);
a.recycle();
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
i.setImageResource(mImageIds[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将
convertView
转换为您想要的视图,而不是在getView
中创建新的ImageView
。以下是一种方法的示例:只需转换 ConvertView 以匹配您想要的内容,但首先确保其正确的视图类型。
更新:您还应该在显示图像之前对图像进行缩减采样。假设您在
res/drawable
下保存了一个 500x500 像素的图像,但该图像在屏幕上仅占用 125x125 像素。在显示图像之前,您需要对图像进行下采样。要知道需要对位图进行下采样多少,您必须首先获取其大小。现在我们有了大小,可以计算对图像进行下采样的量。如果我们有一个 500x500 位图,并且想要一个 125x125 位图,我们将从
int inSample = 500/125;
获得的每 4 个像素中保留 1 个,现在只需对资源进行解码,我们就可以进行下采样了位图。
请记住,原始位图不受影响。您可以再次解码图像并将
opts.inSampleSize
设置为1
,您将获得整个 500x500 位图图像。Instead of creating a new
ImageView
ingetView
you should convert theconvertView
to the view you want. Here is an example of one way to do it:Simply convert the convertView to match what you want, but first make sure its the proper view type.
Update: You should also downsample the images before you display them. Lets assume that you have a 500x500 pixel image saved under
res/drawable
but the image is only going to take up 125x125 pixels on the screen. You need to downsample the image before you display it. To know how much you need to downsample the bitmap you must first get its sizeNow that we have the size calculate how much to downsample the image. If we have a 500x500 bitmap and we want a 125x125 bitmap we keep 1 out of every 4 pixels which we get from
int inSample = 500/125;
Now simply decode the resources and we have our down-sampled bitmap.
Keep in mind that the original bitmap is unaffected. You can decode the image again and set
opts.inSampleSize
to1
and you will get the entire 500x500 bitmap image.Gallery 小部件实际上有一个错误,导致它始终返回空的 ConvertView。这意味着,即使您实现使用 ConvertView 的 getView 方法,您也不会获得性能提升,因为无论如何它总是会创建一个新的。
查看此答案
了解一种可能的解决方案。此人基本上采用了 Gallery 小部件并对其进行了所需的更正。您可以下载 EcoGallery 并将其包含在您自己的包中,以便您可以使用正确回收的 Gallery Widget。您的其他选择是: (1) 实现您自己的逻辑来保存和使用 View 对象的缓存。 (2) 找到一些其他的 View widget 组合来实现类似于 gallery 的效果,并使用它们来代替实际的 Gallery Widget。
请注意,如果您选择下载此 EcoGallery,您需要网站上列出的 3 个 java 文件,并且您必须从 sdk 中获取 2 个不同的 xml 文件并将它们包含在您的项目中。
The Gallery widget actually has a bug that causes it to always return a null convertView. This means that even if you implement a getView method that uses the convertView you aren't going to gain performance because its always going to create a new one anyway.
Check out this answer
For one possible solution. This person basically took the Gallery widget and made the needed corrections to it. You can download the EcoGallery and include it in your own package so that you can use a Gallery Widget that properly recycles. You'r other options are: (1) Implement your own logic to hold and use a cache of View objects. (2) Find some other combination of View widgets to achieve an effect similar to the gallery and use them instead of the actual Gallery Widget.
Note that if you choose to download this EcoGallery you need the 3 java files listed on the site, and you'll have to grab 2 different xml files from inside your sdk and include them in your project.