找到你要的答案

Q:insert bitmaps array instead of drawables in gridview in android

Q:插入位图阵列代替Android在GridView显示元素

I want to have a list of images as GridView in my app. I am using my ImageAdapter class for initializing GridView.

Here is my ImageAdapter class:

public class ImageAdapter extends BaseAdapter {
   private Context mContext;

   // Constructor
   public ImageAdapter(Context c) {
      mContext = c;
   }

   public int getCount() {
      return mThumbIds.length;
   }

   public Object getItem(int position) {
      return null;
   }

   public long getItemId(int position) {
      return 0;
   }

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;

      if (convertView == null) {
         imageView = new ImageView(mContext);
         imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         imageView.setPadding(8, 8, 8, 8);
      } 
      else 
      {
         imageView = (ImageView) convertView;
      }
      imageView.setImageResource(mThumbIds[position]);
      return imageView;
   }

   // Keep all Images in array
   public Integer[] mThumbIds = {
      R.drawable.sample_2, R.drawable.sample_3,
      R.drawable.sample_4, R.drawable.sample_5,
      R.drawable.sample_6, R.drawable.sample_7,
      R.drawable.sample_0, 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,
      R.drawable.sample_0, 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
   };
}

But I want to use my ArrayList<Bitmap> instead of Integer[] mThumbIds.

My Bitmap arraylist is like this:

ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();

byte[] data = null;

for (int i = 0; i<image.size(); i++) {
    try {
        data = Base64.decodeBase64(image.get(i).getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    bitmaps.add(BitmapFactory.decodeByteArray(data, 0, data.length));
}

What should I do for that?

我想有一个图像列表控件在我的应用程序。我用我的图片适配器类初始化GridView。

这里是我的图片适配器类:

public class ImageAdapter extends BaseAdapter {
   private Context mContext;

   // Constructor
   public ImageAdapter(Context c) {
      mContext = c;
   }

   public int getCount() {
      return mThumbIds.length;
   }

   public Object getItem(int position) {
      return null;
   }

   public long getItemId(int position) {
      return 0;
   }

   // create a new ImageView for each item referenced by the Adapter
   public View getView(int position, View convertView, ViewGroup parent) {
      ImageView imageView;

      if (convertView == null) {
         imageView = new ImageView(mContext);
         imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
         imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
         imageView.setPadding(8, 8, 8, 8);
      } 
      else 
      {
         imageView = (ImageView) convertView;
      }
      imageView.setImageResource(mThumbIds[position]);
      return imageView;
   }

   // Keep all Images in array
   public Integer[] mThumbIds = {
      R.drawable.sample_2, R.drawable.sample_3,
      R.drawable.sample_4, R.drawable.sample_5,
      R.drawable.sample_6, R.drawable.sample_7,
      R.drawable.sample_0, 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,
      R.drawable.sample_0, 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
   };
}

但我想用我的ArrayList <;位图>;而不是整数[] mthumbids。

我的Bitmap arraylist是这样的:

ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();

byte[] data = null;

for (int i = 0; i<image.size(); i++) {
    try {
        data = Base64.decodeBase64(image.get(i).getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    bitmaps.add(BitmapFactory.decodeByteArray(data, 0, data.length));
}

我该怎么做?

answer1: 回答1:

You can simply use the method setImageBitmap() from ImageView for that.

In your particular case, on your ImageAdapter class, pass the Array of Bitmap on the constructor.

private ArrayList<BitMap> images;

// Constructor
public ImageAdapter(Context c, ArrayList<BitMap> images) {
    mContext = c;
    this.images = images;
}

Then, on your getView() method, set the image like this:

imageView.setImageBitmap(images.get(position);

And you can safely remove this:

// Keep all Images in array
public Integer[] mThumbIds = {
   R.drawable.sample_2, R.drawable.sample_3,
   R.drawable.sample_4, R.drawable.sample_5,
   R.drawable.sample_6, R.drawable.sample_7,
   R.drawable.sample_0, 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,
   R.drawable.sample_0, 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
};

你可以简单的使用方法setimagebitmap()从ImageView。

在特定的情况下,在您的图片适配器类的构造函数,通过位图阵列。

private ArrayList<BitMap> images;

// Constructor
public ImageAdapter(Context c, ArrayList<BitMap> images) {
    mContext = c;
    this.images = images;
}

然后,在你的getview()方法,设置这样的形象:

imageView.setImageBitmap(images.get(position);

你可以安全删除:

// Keep all Images in array
public Integer[] mThumbIds = {
   R.drawable.sample_2, R.drawable.sample_3,
   R.drawable.sample_4, R.drawable.sample_5,
   R.drawable.sample_6, R.drawable.sample_7,
   R.drawable.sample_0, 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,
   R.drawable.sample_0, 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
};
answer2: 回答2:

Instead of passing mThumbIds to adapter, pass arraylist of bitmaps.

If you are decoding bitmaps you need to take a special care such that it is not done on UI thread. This can be done using AsyncTask. Please refer: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

Also, for caching the bitmaps, number of bitmaps to be cached should depend on memory available.

而不是通过mthumbids适配器,通过创建位图。

If you are decoding bitmaps you need to take a special care such that it is not done on UI thread. This can be done using AsyncTask. Please refer: http://developer.android.com/training/displaying-bitmaps/process-bitmap.html

另外,缓存的位图,将位图的数量应取决于可用的内存。

android  android-gridview  android-bitmap