第二十一篇 Android listview异步加载本地视频缩略图
2018-01-07 20:05
441 查看
最近项目中需要在listview上加载视频缩略图,如果直接使用ThumbnailUtils类获取会出现oom,然后参考了网上这边
文章,感觉不错,稍微做了下调整,这里做个记录,方便后续复用.
MyVideoThumbLoader类:
<
4000
p>MyImageView类:需要使用MyImageView,不能使用ImageView来显示视频缩略图.
}
VideoAdapter类:可根据业务需求自定义.
文章,感觉不错,稍微做了下调整,这里做个记录,方便后续复用.
MyVideoThumbLoader类:
public class MyVideoThumbLoader { // 创建cache private LruCache<String, Bitmap> lruCache; private Context mContext; public MyVideoThumbLoader(Context context) { mContext = context; int maxMemory = (int) Runtime.getRuntime().maxMemory();// 获取最大的运行内存 int maxSize = maxMemory / 4; // 拿到缓存的内存大小 lruCache = new LruCache<String, Bitmap>(maxSize) { @Override protected int sizeOf(String key, Bitmap value) { // 这个方法会在每次存入缓存的时候调用 return value.getByteCount(); } }; } public void addVideoThumbToCache(String path, Bitmap bitmap) { if (getVideoThumbToCache(path) == null) { // 当前地址没有缓存时,就添加 lruCache.put(path, bitmap); } } public Bitmap getVideoThumbToCache(String path) { return lruCache.get(path); } public void showThumbByAsynctack(String path, MyImageView imgview) { if (getVideoThumbToCache(path) == null) { // 异步加载 new MyBobAsynctack(imgview, path).execute(path); } else { imgview.setImageBitmap(getVideoThumbToCache(path)); } } class MyBobAsynctack extends AsyncTask<String, Void, Bitmap> { private MyImageView imgView; private String path; public MyBobAsynctack(MyImageView imageView, String path) { this.imgView = imageView; this.path = path; } @Override protected Bitmap doInBackground(String... params) { Bitmap bitmap2 = null; try { ThumbnailUtils tu = new ThumbnailUtils(); Bitmap bitmap = tu.createVideoThumbnail(params[0], MediaStore.Video.Thumbnails.MICRO_KIND); System.out.println("111111path: " + path + " bitmap: " + bitmap); if (bitmap == null) { //将drawable转bitmap bitmap = android.graphics.BitmapFactory.decodeResource( mContext.getResources(), R.drawable.icon_video); System.out.println("5555555path: " + path + " bitmap: " + bitmap); } // //直接对Bitmap进行缩略操作,最后一个参数定义为OPTIONS_RECYCLE_INPUT ,来回收资源 bitmap2 = tu.extractThumbnail(bitmap, 50, 50, ThumbnailUtils.OPTIONS_RECYCLE_INPUT); System.out.println("path: " + path + "bitmap2: " + bitmap2); // 加入缓存中 if (getVideoThumbToCache(params[0]) == null) { addVideoThumbToCache(path, bitmap2); } } catch (Exception e) { e.printStackTrace(); } return bitmap2; } @Override protected void onPostExecute(Bitmap bitmap) { if (imgView.getTag().equals(path)) {// 通过 Tag可以绑定 图片地址和 // imageView,这是解决Listview加载图片错位的解决办法之一 imgView.setImageBitmap(bitmap); } } } }
<
4000
p>MyImageView类:需要使用MyImageView,不能使用ImageView来显示视频缩略图.
public class MyImageView extends ImageView { public MyImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public MyImageView(Context context, AttributeSet attrs) { super(context, attrs); } public MyImageView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { try { super.onDraw(canvas); } catch (Exception e) { System.out.println("trying to use a recycled bitmap"); } }
}
VideoAdapter类:可根据业务需求自定义.
public class VideoAdapter extends BaseAdapter { List<FileBean> datas; private LayoutInflater mInflater; private int resources; private Context mContext; private MyVideoThumbLoader mVideoThumbLoader; public VideoAdapter(Context mContext, int resources, List<FileBean> datas) { this.mContext = mContext; this.resources = resources; this.datas = datas; mInflater = LayoutInflater.from(mContext); mVideoThumbLoader = new MyVideoThumbLoader(mContext);// 初始化缩略图载入方法 } @Override public int getCount() { if (datas==null)return 0; return datas.size(); } @Override public Object getItem(int position) { if (position < 0 || position >= datas.size()) return null; return datas.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; FileBean entity = datas.get(position); if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate(resources, parent, false); holder.fileIcon = (MyImageView) convertView.findViewById(R.id.image); holder.fileName = (TextView) convertView.findViewById(R.id.name); holder.time = (TextView) convertView.findViewById(R.id.time); holder.fileSize = (TextView) convertView.findViewById(R.id.size); holder.check = (ImageView) convertView.findViewById(R.id.ivCheck); convertView.setTag(holder); holder.fileIcon.setTag(entity.getFile().getAbsolutePath()); } else { holder = (ViewHolder) convertView.getTag(); } if (entity.getFile().length() <= 0) { holder.fileIcon.setImageResource(R.drawable.icon_video); }else{ mVideoThumbLoader.showThumbByAsynctack(entity.getFile().getAbsolutePath(), holder.fileIcon); } holder.fileName.setText(entity.getFileName()); holder.time.setText(String.valueOf(entity.getFile().lastModified())); holder.fileSize.setText(FileUtils.formatSize(String.valueOf(entity.getFileSize()))); holder.check.setVisibility(entity.isChoose() ? View.VISIBLE : View.GONE); return convertView; } class ViewHolder { MyImageView fileIcon; TextView fileName; TextView time; TextView fileSize; ImageView check; } }
相关文章推荐
- android 视频的缩略图 缓存机制和 异步加载缩略图
- Android ListView 异步加载图片并缓存到本地
- Android:ListView异步加载图片(实现网络下载、存储本地、缓存内存、压缩显示)
- Android实现ListView异步加载图片 - ORIETech
- Android开发之listview优化+图片异步加载+避免图片显示闪烁(修改版)
- Android之ListView异步加载网络图片(优化缓存机制)【转】
- Android实现ListView异步加载图片
- listview异步图片加载之优化篇(android)
- android ListView异步加载图片(双缓存)
- Android ListView 异步加载图片错位的问题
- Android之ListView异步加载网络图片(优化缓存机制)
- android listview分页异步加载图片及图片缓存
- Android ListView滑动过程中图片异步加载优化(配合Volley使用)
- Android ListView 图片异步加载和图片内存缓存
- Android--ListView异步加载图片
- Android ListView异步加载图片乱序问题,原因分析及解决方案
- Android之ListView异步加载网络图片(优化缓存机制)
- android-------用双缓存技术优化listview异步加载网络图片
- Android实习03:ListView网络异步加载图片的优化显示(1)
- android Listview 软引用SoftReference异步加载图片