我有一个列表视图显示视频与他们的缩略图,我正在使用光标适配器(使用ContentProvider的获取).问题是列表项增加列表视图的性能变得非常差.请让我知道最好的方法,使其懒惰加载.
新的:
最后我得到了一些解决方案.它的工作对我很好.如有任何建议,请通知我.
我的新代码在这里. (它使用堆栈与生产者消费者patern和bacground线程进行)
@覆盖
public void bindView(View view,Context context,Cursor cursor){
ViewHolder holder = (ViewHolder) view.getTag(); String name = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)); int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Video.Media._ID)); holder.getIdView().setText(name); holder.getThumbView().setTag((Integer)id); if(LazyLoader.getInstance().containsKey(id)) { Object bitmap = LazyLoader.getInstance().getBitMap(id); if(null != bitmap && (bitmap instanceof Bitmap)) { holder.getThumbView().setImageBitmap((Bitmap)bitmap); } else { holder.getThumbView().setImageDrawable(null); } } else { holder.getThumbView().setImageDrawable(null); LazyItem lazyItem = new LazyItem(); lazyItem.setId(id); lazyItem.setThumbNail(holder.getThumbView()); LazyLoader.getInstance().putItem(lazyItem); } } public class LazyLoaderHelper extends Thread { private ContentResolver resolver = null; private Activity activity; private boolean isActive = true; ExecutorService executor; public LazyLoaderHelper(Activity _activity,ContentResolver _resolver) { resolver = _resolver; activity = _activity; isActive = true; executor = Executors.newFixedThreadPool(10); } public void stopThread() { isActive = false; executor.shutdown(); } @Override public void run() { while (isActive) { if(LazyLoader.getInstance().getSize() > 0) { final LazyItem lazyItem = LazyLoader.getInstance().getItem(); if(null != lazyItem) { executor.execute(new Runnable() { @Override public void run() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 1; if ((null != resolver) && (null != activity)) { final Bitmap thumb = MediaStore.Video.Thumbnails.getThumbnail(resolver,lazyItem.getId(),MediaStore.Video.Thumbnails.MICRO_KIND,options); if (null != thumb) { LazyLoader.getInstance().putBitMap(lazyItem.getId(),thumb); } else { LazyLoader.getInstance().putBitMap(lazyItem.getId(),new Object()); } if ((Integer) lazyItem.getThumbNail().getTag() == lazyItem.getId()) { activity.runOnUiThread(new Runnable() { @Override public void run() { if (null != thumb) { lazyItem.getThumbNail().setImageBitmap(thumb); } else { lazyItem.getThumbNail().setImageDrawable(null); } } }); } } } }); } } } } }
解决方法
您可以使用picaso库的Android它是开源的,可以轻松地与您的应用程序集成,您可以从github从下面的链接下载库
http://square.github.io/picasso/
并且可以通过在列表视图适配器中使用以下方法轻松实现延迟加载
Picasso.with(上下文)
.load(URL)
.placeholder(R.drawable.user_placeholder)
.error(R.drawable.user_placeholder_error)
.into(ImageView的);
Picasso.with(上下文).load(R.drawable.landing_screen).into(imageView1);Picasso.with(上下文).load( “文件:///android_asset/DvpvklR.png”).into(imageView2);Picasso.with(context).load(new File(…)).into(imageView3);