欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2023-11-18 11:51:52
快速滑动图片一闪一闪的问题,图片加载等处理在这里不介绍,主要就是介绍下在adapter中维护一个linkedhashmap解决上述问题 package com.l...

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

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