解决Android快速滑动时图片一闪一闪问题

时间:2021-05-02

快速滑动图片一闪一闪的问题,图片加载等处理在这里不介绍,主要就是介绍下在Adapter中维护一个LinkedHashMap解决上述问题

? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 package com.longraise.seller.adapter; import android.content.Context; import android.graphics.Bitmap; import android.graphics.drawable.BitmapDrawable; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import com.longraise.seller.R; import com.longraise.seller.view.VoiceButton; import java.lang.ref.SoftReference; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import cc.sdkutil.control.image.CCImageLoader; import cc.sdkutil.control.image.CCImageLoaderCallback; import cc.sdkutil.control.inject.CCInjectUtil; import cc.sdkutil.model.inject.CCInjectRes; /** * Created by wangcong on 15-1-8. */ public class OrderFinishAdapter extends BaseAdapter { //convertview id private final static int BASE_ID = 0x0fff00; private Context mContext; private List<Map<String, Object>> mAllList; //图片加载相关 CCImageLoader mImageLoader; //用于缓存图片,减小快速滑动时图片显示落帧现象 final LinkedHashMap<String, SoftReference<Bitmap>> linkedHashMap; final int MAX_SIZE = 16; public OrderFinishAdapter(Context context, List<Map<String, Object>> list) { this.mContext = context; this.mAllList = list; linkedHashMap = new LinkedHashMap<String, SoftReference<Bitmap>>(16, 0.75f, true) { private static final long serialVersionUID = 1L; @Override protected boolean removeEldestEntry(Entry<String, SoftReference<Bitmap>> eldest) { boolean flag = size() > MAX_SIZE; if (flag) { SoftReference<Bitmap> softReference = eldest.getValue(); Bitmap bitmap = softReference.get(); if (bitmap != null) bitmap.recycle(); remove(eldest.getKey()); } return flag; } }; } public int getCount() { return mAllList.size(); } public Object getItem(int position) { return mAllList.get(position); } public long getItemId(int position) { return position; } @SuppressWarnings("deprecation") public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate(R.layout.adapter_finish_item, parent, false); holder = new ViewHolder(); CCInjectUtil.inject(holder, convertView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } convertView.setId(BASE_ID + position); final Map<String, Object> map = mAllList.get(position); //处理图片 SoftReference<Bitmap> softReference = linkedHashMap.get(map.get("orderImgUrl")); Bitmap bitmap = softReference == null ? null : softReference.get(); if (bitmap == null) { holder.image.setBackgroundResource(R.drawable.default_image_error); if (mImageLoader == null) mImageLoader = new CCImageLoader.Builder().needCacheInDisk().outSize(120, 120) .callback(new CCImageLoaderCallback() { @Override public void onSuccess(Bitmap bitmap, Object... objs) { super.onSuccess(bitmap, objs); if (bitmap != null) { //图片加载成功后处理 ImageView imageView = (ImageView) objs[0]; imageView.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), bitmap)); SoftReference<Bitmap> soft = new SoftReference<Bitmap>(bitmap); linkedHashMap.put(objs[1] + "", soft); bitmap = null; } } }).build(); mImageLoader.loadNetImage(map.get("orderImgUrl") + "", holder.image, map.get("orderImgUrl")); } else { holder.image.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(), bitmap)); } return convertView; } static class ViewHolder { @CCInjectRes(R.id.order_item_image) ImageView image; } }

以上所述就是本文的全部内容了,希望大家能够喜欢。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章