Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > 解决Android快速滑动问题

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

投稿:hebedich

这篇文章主要给大家介绍了个人在解决Android快速滑动时图片一闪一闪问题的一些思路和方法,非常实用,有需要的小伙伴可以参考下。

快速滑动图片一闪一闪的问题,图片加载等处理在这里不介绍,主要就是介绍下在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;
  }
}

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

您可能感兴趣的文章:
阅读全文