在 android 的 gridview 中插入位图数组而不是可绘制对象
insert bitmaps array instead of drawables in gridview in android
我想在我的应用程序中有一个图像列表 GridView
。我正在使用 ImageAdapter
class 来初始化 GridView
.
这是我的 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
};
}
但我想使用 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));
}
我该怎么做?
您只需使用 ImageView
中的方法 setImageBitmap()
即可。
在您的特定情况下,在您的 ImageAdapter
class 上,将 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
};
不是将 mThumbId 传递给适配器,而是传递位图数组列表。
如果您正在解码位图,您需要特别小心,不要在 UI 线程上完成。
这可以使用 AsyncTask 来完成。请参考:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
此外,对于缓存位图,要缓存的位图数量应取决于可用内存。
我想在我的应用程序中有一个图像列表 GridView
。我正在使用 ImageAdapter
class 来初始化 GridView
.
这是我的 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
};
}
但我想使用 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));
}
我该怎么做?
您只需使用 ImageView
中的方法 setImageBitmap()
即可。
在您的特定情况下,在您的 ImageAdapter
class 上,将 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
};
不是将 mThumbId 传递给适配器,而是传递位图数组列表。
如果您正在解码位图,您需要特别小心,不要在 UI 线程上完成。 这可以使用 AsyncTask 来完成。请参考:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
此外,对于缓存位图,要缓存的位图数量应取决于可用内存。