我想要一个图像列表 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/