在这次的工作开发项目中,涉及到一个视频缩略图的视频列表;这个在大家看来,制作视频缩略图就是两行代码就搞定的事。确实是这样的,百度一下,每个帖子都知道制作视频缩略图的方法,在这里确实也是一样的,但是我要实现的是让缩略图 在ListView上显示,而且不会出现卡顿,其实也不是特别难,确实很实用;
主要的实现机制就是 异步加载 和 缓存机制 在加上一个绑定TAG机制;接下来就开始去实现吧
先上我的是 结构图:
这个包结构的 自定义 ListView 实现了 仿手机联系人按一定的数据分类 功能,和浮动组标题效果,但是这些功能都不是我今天要强调的,这里只说 怎么样实现缩略图的缓存机制,所以重要的的事 MyVideoThumbLoader类,
简略的的介绍下其他类:
VideoBean 是视频的实体类,用来存储视频数据,包括 视频路径,文件名,时长,。。。做过项目的都懂
VideoUtil 是工具类,写了一些基本的查找视频文件的方法,和创建缩略图的方法
MyPinnedSectionListView 是继承了ListView ,可以不用管内部具体,在这里只用到普通的ListView 就可以了
。。。
在我们拿到了 视频文件信息 ArrayList 并且传到了 MyImageAdapter 内部前提下
,Adapter内部结构如下:
1 package com.example.jiuan_item3; 2 3 import java.util.ArrayList; 4 import java.util.Collections; 5 import java.util.Comparator; 6 import java.util.HashMap; 7 import java.util.Map; 8 9 import android.annotation.SuppressLint; 10 import android.app.ActionBar.LayoutParams; 11 import android.content.Context; 12 import android.graphics.Bitmap; 13 import android.graphics.Color; 14 import android.provider.MediaStore; 15 import android.view.View; 16 import android.view.ViewGroup; 17 import android.widget.BaseAdapter; 18 import android.widget.ImageView; 19 import android.widget.LinearLayout; 20 import android.widget.TextView; 21 22 /** 23 * // * listview适配器,设置特殊item(为蓝色背景的首字母栏) // 24 */ 25 public class MyImageAdapter extends BaseAdapter { 26 private Context context; 27 private ArrayList<VideoBean> datas; 28 public Map<String, Integer> maps; 29 private MyVideoThumbLoader mVideoThumbLoader; 30 31 39 /** 40 * @param context 41 * @param datas 42 * 具有数据提供的构造 43 */ 44 public MyImageAdapter(Context context, ArrayList<VideoBean> datas) { 45 this.context = context; 46 this.datas = datas; 47 sortLetter(datas); 48 mVideoThumbLoader = new MyVideoThumbLoader();// 初始化缩略图载入方法 49 } 50 51 /** 52 * 获取需要顶部悬浮显示的view,这是另外的功能,可不理会 53 */ 54 public View getPinnedSectionView(int position) { 55 ViewGroup view = (ViewGroup) getView(position, null, null);// x 56 View vAlpha = view.getChildAt(0); 57 return vAlpha; 58 } 59 60 @Override 61 public int getCount() { 62 return datas.size(); 63 } 64 65 @Override 66 public VideoBean getItem(int position) { 67 return datas.get(position); 68 } 69 70 @Override 71 public long getItemId(int position) { 72 return position; 73 } 74 75 @Override 76 public View getView(int position, View convertView, ViewGroup parent) { 77 78 View view = View.inflate(context, R.layout.alpha_item, null); 79 final TextView tvAlpha = (TextView) view 80 .findViewById(R.id.alphaitem_tv_alpha); 81 TextView tvContent = (TextView) view 82 .findViewById(R.id.alphaitem_tv_content); 83 ImageView tvImg = (ImageView) view.findViewById(R.id.imgview); 84 85 VideoBean myData = getItem(position); 86 87 tvAlpha.setText(myData.getFileParentVideo());
//实现分组的功能,可不理会 88 if (maps.get(myData.getFileParentVideo()) == position) { 89 tvAlpha.setVisibility(View.VISIBLE); 90 } else { 91 tvAlpha.setVisibility(View.GONE); 92 } 93 tvAlpha.setTag(position); 94
//重点代码 95 String path = datas.get(position).getLocationPath(); 96 tvImg.setTag(path);//绑定imageview 97 mVideoThumbLoader.showThumbByAsynctack(path, tvImg); 98 99 tvContent.setText(datas.get(position).getVidioName()); 100 101 return view; 102 } 103 104 private void sortLetter(ArrayList<VideoBean> datas) { 105 // 这是个分组的排序算法,为了让所有的数据统一性 106 Collections.sort(datas, new Comparator<VideoBean>() { 107 @Override 108 public int compare(VideoBean lhs, VideoBean rhs) { 109 return lhs.getFileParentVideo().compareTo( 110 rhs.getFileParentVideo()); 111 } 112 }); 113 maps = new HashMap<String, Integer>(); 114 for (int i = 0; i < datas.size(); i++) { 115 if (!maps.containsKey(datas.get(i).getFileParentVideo())) { 116 maps.put(datas.get(i).getFileParentVideo(), i); 117 } 118 } 119 } 120 }
上面代码中运用到了 MyVideoThumbLoader,这个类,所有贴出这个代码如下:
1 package com.example.jiuan_item3; 2 3 import android.annotation.SuppressLint; 4 import android.graphics.Bitmap; 5 import android.os.AsyncTask; 6 import android.os.Handler; 7 import android.os.Handler.Callback; 8 import android.os.Message; 9 import android.provider.MediaStore; 10 import android.support.v4.util.LruCache; 11 import android.widget.ImageView; 12 13 public class MyVideoThumbLoader { 14 private ImageView imgView; 15 private String path; 16 //创建cache 17 private LruCache<String, Bitmap> lruCache; 18 31 @SuppressLint("NewApi") 32 public MyVideoThumbLoader(){ 33 int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取最大的运行内存 34 int maxSize = maxMemory /4;//拿到缓存的内存大小 35 lruCache = new LruCache<String, Bitmap>(maxSize){ 36 @Override 37 protected int sizeOf(String key, Bitmap value) { 38 //这个方法会在每次存入缓存的时候调用 39 return value.getByteCount(); 40 } 41 }; 42 } 43 44 public void addVideoThumbToCache(String path,Bitmap bitmap){ 45 if(getVideoThumbToCache(path) == null){ 46 //当前地址没有缓存时,就添加 47 lruCache.put(path, bitmap); 48 } 49 } 50 public Bitmap getVideoThumbToCache(String path){ 51 52 return lruCache.get(path); 53 54 } 55 public void showThumbByAsynctack(String path,ImageView imgview){ 56 57 if(getVideoThumbToCache(path) == null){ 58 //异步加载 59 new MyBobAsynctack(imgview, path).execute(path); 60 }else{ 61 imgview.setImageBitmap(getVideoThumbToCache(path)); 62 } 63 64 } 65 66 class MyBobAsynctack extends AsyncTask<String, Void, Bitmap> { 67 private ImageView imgView; 68 private String path; 69 70 public MyBobAsynctack(ImageView imageView,String path) { 71 this.imgView = imageView; 72 this.path = path; 73 } 74 75 @Override 76 protected Bitmap doInBackground(String... params) {
//这里的创建缩略图方法是调用VideoUtil类的方法,也是通过 android中提供的 ThumbnailUtils.createVideoThumbnail(vidioPath, kind); 77 Bitmap bitmap = VideoUtil.createVideoThumbnail(params[0], 70, 50, MediaStore.Video.Thumbnails.MICRO_KIND); 78
//加入缓存中 79 if(getVideoThumbToCache(params[0]) == null){ 80 addVideoThumbToCache(path, bitmap); 81 } 82 return bitmap; 83 } 84 @Override 85 protected void onPostExecute(Bitmap bitmap) { 86 if(imgView.getTag().equals(path)){//通过 Tag可以绑定 图片地址和 imageView,这是解决Listview加载图片错位的解决办法之一 87 imgView.setImageBitmap(bitmap); 88 } 89 } 90 } 108 }
通过上面的 videoThumbLoader类,就可以看出这个机制的核心所在,异步方法实现图片的加载,这样不会阻塞ui线程,然后缓存加载,又可以节省加载时间;然后在 adapter的geiViewf方法里调用设置缩略图;为了节省每次都去创建缩略图的时间,LruCache实现了图片的缓存机制,LruCache其实底层就是一个 HashMap map 类;具体可以百度下LruCache
1 public MyVideoThumbLoader(){ 2 int maxMemory = (int) Runtime.getRuntime().maxMemory();//获取最大的运行内存 3 int maxSize = maxMemory /4; 4 lruCache = new LruCache<String, Bitmap>(maxSize){ 5 @Override 6 protected int sizeOf(String key, Bitmap value) { 7 //这个方法会在每次存入缓存的时候调用,必须要实现的方法 8 return value.getByteCount(); 9 } 10 }; 11 }
就这样,全部代码写完了;这个视频缩略图的加载和显示都完美的实现了;
不过以上都是核心代码,如果你看的不是很明白可以留言,
我会上传源码
本版为原创,转载请注明来路;要尊重程序员