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;
}
}
Instead of creating a new
ImageViewingetViewyou should convert theconvertViewto 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 500×500 pixel image saved under
res/drawablebut the image is only going to take up 125×125 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 500×500 bitmap and we want a 125×125 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.inSampleSizeto1and you will get the entire 500×500 bitmap image.