android 視頻的縮略圖 緩存機制和 異步加載縮略圖


在這次的工作開發項目中,涉及到一個視頻縮略圖的視頻列表;這個在大家看來,制作視頻縮略圖就是兩行代碼就搞定的事。確實是這樣的,百度一下,每個帖子都知道制作視頻縮略圖的方法,在這里確實也是一樣的,但是我要實現的是讓縮略圖 在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     }

就這樣,全部代碼寫完了;這個視頻縮略圖的加載和顯示都完美的實現了;

不過以上都是核心代碼,如果你看的不是很明白可以留言,

我會上傳源碼

 

本版為原創,轉載請注明來路;要尊重程序員

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM