Android的GalleryView回收Android、GalleryView

2023-09-12 08:14:13 作者:傲骨寒俗

我使用GalleryView与〜40张图片,和这么慢,因为没有回收...

任何人都可以告诉我一个基本的循环利用 GalleryView getView 方法。

 公共类ImageAdapter扩展了BaseAdapter {
    INT mGalleryItemBackground;
    私人语境mContext;

    私人整数[] 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
    };

    公共ImageAdapter(上下文C){
        mContext = C;

        TypedArray A = c.obtainStyledAttributes(R.styleable.HelloGallery);
        mGalleryItemBackground = a.getResourceId(
                R.styleable.HelloGallery_android_galleryItemBackground,0);
        a.recycle();
    }

    公众诠释getCount将(){
        返回mImageIds.length;
    }

     公共对象的getItem(INT位置){
        返回的位置;
    }

    众长getItemId(INT位置){
        返回的位置;
    }

    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ImageView的我=新ImageView的(mContext);

        i.setImageResource(mImageIds [位置]);
        i.setLayoutParams(新Gallery.LayoutParams(150,100));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);

        返回我;
    }
}
 

解决方案

而不是在 getView 你应该转换 convertView 到你想要的视图。这里是一种方法的一个例子来做到这一点:

 公开查看getView(INT位置,查看简历,ViewGroup中父){

    如果(!convertView istanceof ImageView的)
    {
        ImageView的CV =新ImageView的(mContext);
        cv.setLayoutParams(新Gallery.LayoutParams(150,100));
        cv.setScaleType(ImageView.ScaleType.FIT_XY);
        cv.setBackgroundResource(mGalleryItemBackground);

    }
    cv.setImageResource(mImageIds [位置]);

    返回简历;
}
 

简单地转换convertView来匹配你想要什么,但首先要确保它在正确的视图类型。

更新:你也应该下采样图像,你显示它们之前。让我们假设你有一个500×500像素的图像在水库保存/绘制但图像只会占用为125x125像素的屏幕上。您需要下采样图像您显示它。要知道你需要多少下采样,您必须先得到位图的大小

  INT MAXSIZE = 125; //使125位图上的大小的上限
INT渣油; //指向RES位图/绘制

BitmapFactory.Options选择采用=新BitmapFactory.Options();
opts.inJustDe codeBounds = TRUE; //只能得到位图的大小,而不是位图本身
BitmapFactory.de codeResource(c.getResources(),渣油,选择采用);

INT W = opts.outHeight,H = opts.outHeight;
INT maxDim =(并且R w; H)瓦特:H; //获取更大尺寸
 
android Gallery与ImageView的结合使用

现在,我们有大小来计算多少下采样图像。如果我们有一个500×500的位图,我们希望有一个125x125像素的位图,我们保留1,每4个像素这是我们从得到的 INT inSample = 500/125;

  INT inSample = maxDim / MAXSIZE;

OPTS =新BitmapFactory.Options();
opts.inSampleSize = inSample;
 

现在只需去code中的资源,我们有我们的下采样位图。

 位图B = BitmapFactory.de codeResource(c.getResources(),渣油,选择采用);
 

请记住,原始位不受影响。您可以再次去$ C C图像$,并设置 opts.inSampleSize 1 ,你会得到整个500×500位图图像。

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 ImageView in getView you should convert the convertView to the view you want. Here is an example of one way to do it:

public View getView(int position, View cv, ViewGroup parent) {

    if (! convertView istanceof ImageView)
    {
        ImageView cv = new ImageView(mContext);
        cv.setLayoutParams(new Gallery.LayoutParams(150, 100));
        cv.setScaleType(ImageView.ScaleType.FIT_XY);
        cv.setBackgroundResource(mGalleryItemBackground);

    }
    cv.setImageResource(mImageIds[position]);

    return cv;
}

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 size

int maxSize = 125; // make 125 the upper limit on the bitmap size
int resId; // points to bitmap in res/drawable

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true; // Only get the bitmap size, not the bitmap itself
BitmapFactory.decodeResource(c.getResources(), resId, opts);

int w = opts.outHeight, h = opts.outHeight;
int maxDim = (w>h)?w:h; // Get the bigger dimension

Now 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;

int inSample = maxDim/maxSize; 

opts = new BitmapFactory.Options();
opts.inSampleSize = inSample;

Now simply decode the resources and we have our down-sampled bitmap.

Bitmap b = BitmapFactory.decodeResource(c.getResources(), resId, opts);

Keep in mind that the original bitmap is unaffected. You can decode the image again and set opts.inSampleSize to 1 and you will get the entire 500x500 bitmap image.