listview滑动卡顿

来源:互联网 发布:淘宝爱护佳腰带 编辑:程序博客网 时间:2024/04/27 23:17

  listview滑动卡顿,网上搜索了一下原因有很多,排除了一下,最后发现和图片加载相关。一开始是自己自定义了一个Cache类,用来缓存图片,最后发现这样做的效率并不高,不如用ImageLoader,下面贴代码

 1.在adapter里

  

private List<EscorterListEntity.DataEntity> mList=new ArrayList<>();    private Context context;    ImageLoader imageLoader = null;    DisplayImageOptions options;    private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();    public void setData(List<EscorterListEntity.DataEntity> entityList,Context context){        this.mList=entityList;        this.context=context;        notifyDataSetChanged();        options = new DisplayImageOptions.Builder()                .showImageOnLoading(R.mipmap.defaultphoto)                .showImageForEmptyUri(R.mipmap.defaultphoto)                .showImageOnFail(R.mipmap.defaultphoto)                .cacheInMemory(true).cacheOnDisc(true).considerExifParams(true)                .displayer(new RoundedBitmapDisplayer(0)).build();        imageLoader = ImageLoader.getInstance();    }


private static class AnimateFirstDisplayListener extends            SimpleImageLoadingListener {        static final List<String> displayedImages = Collections                .synchronizedList(new LinkedList<String>());        @Override        public void onLoadingComplete(String imageUri, View view,                                      Bitmap loadedImage) {            if (loadedImage != null) {                try {                    ImageView imageView = (ImageView) view;                    //用来控制显示头像的剪切范围,3/4时明显用户的照片显示不全                    Bitmap bm = BitmapTools.cutBitmap(loadedImage, 4 / 4f);                    imageView.setImageBitmap(bm);                    boolean firstDisplay = !displayedImages.contains(imageUri);                    if (firstDisplay) {                        FadeInBitmapDisplayer.animate(imageView, 500);                        displayedImages.add(imageUri);                    }                } catch (Exception e) {                }            }        }    }

bitmapTools.class:

public class BitmapTools {public static Bitmap cutBitmap(Bitmap bitmap, float HWRate) {try {int sh = bitmap.getHeight();int sw = bitmap.getWidth();int needW, needH;float p1 = 1.0f * sh / sw;float p2 = HWRate;if (p1 > p2) {needW = sw;needH = (int) (sw * HWRate);} else {needW = (int) (sh / HWRate);needH = sh;}int x = (sw - needW) / 2;int y = (sh - needH) / 2;Matrix matrix = new Matrix();matrix.postScale(1, 1);Bitmap bm = Bitmap.createBitmap(bitmap, x,  y,needW, needH, matrix, true);return bm;} catch (Exception ex) {return bitmap;}}}


2.在getView()方法里,设置image

imageLoader.displayImage(url,holder.iv_avatar_w, options,animateFirstListener);

3.别忘了在Application中初始化

<span></span>/****         * 图片加载组件初始化         */        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(                this.getApplicationContext())                .threadPriority(Thread.NORM_PRIORITY - 2)                .denyCacheImageMultipleSizesInMemory()                .discCacheFileNameGenerator(new Md5FileNameGenerator())                .tasksProcessingOrder(QueueProcessingType.LIFO)                .writeDebugLogs().build();        ImageLoader.getInstance().init(config);




0 0
原创粉丝点击