快速滑动图片一闪一闪的问题,图片加载等处理在这里不介绍,主要就是介绍下在Adapter中维护一个linkedHashMap解决上述问题
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; }}
以上所述就是本文的全部内容了,希望大家能够喜欢。
总结以上是内存溢出为你收集整理的解决Android快速滑动时图片一闪一闪问题全部内容,希望文章能够帮你解决解决Android快速滑动时图片一闪一闪问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)