在下面的代码中,为什么当我从SQLite显示两个图像时,函数loadImageFromResouce
会运行22次?
public class ArrayAdapter_Product extends ArrayAdapter<Product_Object>
{
private TextView Ten_SP_TxtV;
private TextView DonGia_SP_TxtV;
private Activity context;
private int layoutResourceId;
private ByteArrayInputStream ImageStream;
private Product_Object San_Pham = null;
private Product_Holder holder;
ArrayList<Product_Object> product = new ArrayList<Product_Object>();
private static int reqWidth = 90;
private static int reqHeight = 90;
int dem = 0;
int dem_2 = 0;
int count_set = 0;
public ArrayAdapter_Product(Activity context, int layoutResourceId, ArrayList<Product_Object> product) {
super(context, layoutResourceId, product);
this.context = context;
this.layoutResourceId = layoutResourceId;
this.product = product;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();
View row = null;
if (row == null) {
dem++;
row = inflater.inflate(R.layout.item_product, null, true);
holder = new Product_Holder();
holder.ImageV_Temp = (ImageView) row.findViewById(R.id.Anh_SP);
ViewTreeObserver observer = holder.ImageV_Temp.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
reqWidth = holder.ImageV_Temp.getWidth();
reqHeight = holder.ImageV_Temp.getHeight();
Log.i("BITMAP", "ReqW:" + reqWidth + ";ReqH:" + reqHeight);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
holder.ImageV_Temp.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
else
holder.ImageV_Temp.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
row.setTag(holder);
Log.d("BITMAP", "dem "+String.valueOf(dem));
}
else {
holder = (Product_Holder) view.getTag();
dem_2++;
Log.d("BITMAP", "dem_2"+String.valueOf(dem_2));
}
San_Pham = product.get(position);
byte[] Out_Image;
Out_Image = San_Pham.Get_Image();
ImageWorker imageWorker = new ImageWorker(holder.ImageV_Temp);
imageWorker.execute(Out_Image);
return row;
}
private class ImageWorker extends AsyncTask<byte[], Void, Bitmap> {
private WeakReference<ImageView> WeakReference;
public ImageWorker(ImageView imgV) {
WeakReference = new WeakReference<ImageView>(imgV);
}
protected Bitmap doInBackground(byte[]... resId) {
return loadImageFromResouce(resId[0], reqWidth, reqHeight);
}
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (WeakReference != null && bitmap != null) {
ImageView imgV = WeakReference.get();
if (imgV != null) {
imgV.setImageBitmap(bitmap);
count_set++;
Log.d("BITMAP", "BYTE BITMAP SET IMAGEVIEW:" + bitmap.getByteCount());
Log.d("BITMAP", "SO LAN SET BITMAP:" + count_set);
}
}
}
private Bitmap loadImageFromResouce(byte[] Out_Image, int reqWidth, int reqHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(Out_Image, 0, Out_Image.length, options);
Log.d("BITMAP", "BYTE BITMAP BAN DAU:" + Out_Image.length);
int insampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = insampleSize;
Bitmap bm = BitmapFactory.decodeByteArray(Out_Image, 0, Out_Image.length, options);
Log.d("BITMAP", "BYTE BITMAP SAU KHI XU LY:" + bm.getByteCount());
return bm;
}
private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
int consumeMemory = options.outWidth * options.outHeight * 4;
String mineType = options.outMimeType;
Log.d("BITMAP", "MEMORY: " + consumeMemory
+ "; Ori_W: " + options.outWidth
+ "; Ori_H: " + options.outHeight);
Log.d("BITMAP", " " + mineType);
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
Log.d("BITMAP", "inSampleSize - " + inSampleSize);
}
}
return inSampleSize;
}
}
static class Product_Holder {
ImageView ImageV_Temp;
}
}
答案 0 :(得分:0)
要使用视图图案,您需要在视图上设置标记。这段代码:
View row = null;
if (row == null) {
没用。它应该更像是:
if (view == null) {
// inflate the view and construct the viewholder
...
} else {
holder = (ViewHolder)view.getTag();
}
// Now operate on the view and holder