android - 在 android 的 gridview 中插入位图数组而不是可绘制对象

标签 android android-gridview android-bitmap

我想要一个图像列表 GridView在我的应用程序中。我正在使用我的 ImageAdapter初始化类 GridView .

这是我的 ImageAdapter类:

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<Bitmap>而不是 Integer[] mThumbIds .

我的 Bitmap数组列表是这样的:

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));
}

我该怎么做?

最佳答案

您可以简单地使用方法 setImageBitmap()来自 ImageView

在您的特定情况下,在您的 ImageAdapter 类上,将 Bitmap 数组传递给构造函数。

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

关于android - 在 android 的 gridview 中插入位图数组而不是可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32025591/

相关文章:

java - android 获取 android 库上下文与应用程序上下文

android - 如何在android中的gridview滚动中保留复选框状态

java - 拖动项目时滚动 gridview(作为阴影)

Android:ImageView 的 findViewById(自定义适配器)

android - 使用 Path 从 Bitmap 中剪切区域

android - 拍照后相机不保存

android - 如何为 Exoplayer 启用/支持 TLS 1.1、1.2?

android - errorBody() 应为 BEGIN_ARRAY

android - 为什么 BitmapFactory 将我的图像缩小 1.333 倍而不是 2 倍?

android - 如何获取位图中的 X 和 Y 触摸位置