閱讀507 返回首頁    go 阿裏雲 go 技術社區[雲棲]


利用Android 2.2新特性完成縮略圖

 在Android 2.2版本之前,如果需要完成縮略圖功能,往往是通過Bitmap、Drawable和Canvas配合完成,需要寫一係列繁雜的邏輯去縮小原有圖片,從而得到縮略圖。但是到了Andorid 2.2版本,如果大家還這麼做,那麼就證明大家已經成為那個專打怪獸的奧特曼(out man)超人了。

  在Android 2.2版本中,新增了一個ThumbnailUtils工具類來是實現縮略圖,此工具類的功能是強大的,使用是簡單,它提供了一個常量和三個方法。利用這些常數和方法,可以輕鬆快捷的實現圖片和視頻的縮略圖功能。(親,你沒有看錯,還包括生成視頻的縮略圖哦)

  下麵將依次說明一下這些常數和方法。

  常數:

    OPTIONS_RECYCLE_INPUT: 從此常量用於表示應該回收extractThumbnail(Bitmap, int, int, int)輸入源圖片(第一個參數),除非輸出圖片就是輸入圖片。

  方法:

    Bitmap createVideoThumbnail(String filePath, int kind)

      從方法名稱即可看出,這個方法用於生成視頻縮略圖。

        參數:

          filePath: 視頻文件路徑

          kind:  文件種類,可以是 MINI_KIND 或 MICRO_KIND

 

    Bitmap extractThumbnail(Bitmap source, int width, int height, int options)

      此方法用於生成一個指定大小的圖片縮略圖。

        參數:

          source: 需要被創造縮略圖的源位圖對象

          width: 生成目標的寬度

          height: 生成目標的高度

          options:在縮略圖抽取時提供的選項

 

    Bitmap extractThumbnail(Bitmap source, int width, int height)

      此方法用於生成一個指定大小的圖片縮略圖。

        參數:

          source: 需要被創造縮略圖的源位圖對象

          width: 生成目標的寬度

          height: 生成目標的高度

  簡單的方法,簡單的常數,看完這些後,就可以開始創建縮略圖了。下麵以創建一個美女圖片的縮略圖為例,展示一下創建縮略圖的步驟:  

  布局文件 main.xml:

1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical" android:background="#999999" android:layout_width="fill_parent" android:layout_height="fill_parent">
3     <ImageView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:src="@drawable/girl"/>
4     <TextView  android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="縮略圖:" android:textColor="#000000"/>
5     <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="20dp" android:layout_marginTop="10dp"/>
6 </LinearLayout>

  Activity代碼:

 1 public class MainActivity extends Activity {
 2     
 3     @Override
 4     public void onCreate(Bundle savedInstanceState) {
 5        super.onCreate(savedInstanceState);
 6        setContentView(R.layout.main);
 7         
 8        ImageView humbnail = (ImageView) findViewById(R.id.image);        
 9         
10        //獲得一個Drawable對象
11        Drawable girl = getResources().getDrawable(R.drawable.girl);
12        //將Drawable對象強轉為Bitmap位圖對象
13        Bitmap bitmap = ((BitmapDrawable)girl).getBitmap();
14        //利用Bitmap位圖對象生成縮略圖
15        bitmap = ThumbnailUtils.extractThumbnail(bitmap, 51, 108);
16                 
17        humbnail.setImageBitmap(bitmap);
18     }
19 }

  運行結果:  

  好了,運行結果出來了,如圖所示,運行結果如預想一樣,創建了一個美女圖片的縮略圖,並且顯示了出來。這樣的結果看上去絲毫問題沒有,但是在這裏,需要指出的是,雖然結果正確,但是過程卻是不正確的。

  “過程不正確,那作者你為什麼要這麼寫呢?”,這樣的問題可能會出現在各位讀者的腦海中。別急,接下來我就解釋一下我為什麼要這麼寫,另外再重寫一個正確的代碼給大家。大家注意下麵這兩行代碼:

1        //獲得一個Drawable對象
2        Drawable girl = getResources().getDrawable(R.drawable.girl);
3        //將Drawable對象強轉為Bitmap位圖對象
4        Bitmap bitmap = ((BitmapDrawable)girl).getBitmap();

  我相信絕大部分人都會通過這麼一種強製轉換的方法來實現從Drawable到BitmapDrawable的轉換,這也是網上瘋狂流傳並誤導大家的。首先,利用強製轉換的方法來實現類型轉換這種方式,從代碼的角度看,它是不優雅的,是簡單粗暴的,缺乏代碼的美感;其次,因為BitmapDrawable是Drawable的子類,所以無法保證沒一個Drawable對象都能正確的強製轉換為BitmapDrawable,這就給代碼增加了安全的因素。

  其實Google的工程師們提供了從資源文件優雅獲取BitmapDrawable對象的方式。下麵,我將貼出真正正確的寫法。

 1 public class MainActivity extends Activity {
 2     
 3     @Override
 4     public void onCreate(Bundle savedInstanceState) {
 5        super.onCreate(savedInstanceState);
 6        setContentView(R.layout.main);
 7         
 8        ImageView humbnail = (ImageView) findViewById(R.id.image);        
 9         
10        //通過openRawResource獲取一個InputStream對象
11        InputStream input = getResources().openRawResource(R.drawable.girl);
12        //通過InputStream創建BitmapDrawable對象
13        BitmapDrawable girl = new BitmapDrawable(input);
14        //通過BitmapDrawable對象獲取Bitmap對象
15        Bitmap bitmap = girl.getBitmap();
16        //利用Bitmpa對象創建縮略圖
17        bitmap = ThumbnailUtils.extractThumbnail(bitmap, 51, 108);
18                 
19        humbnail.setImageBitmap(bitmap);
20     }
21 }

  這樣獲取BitmapDrawable對象的方式,是否比前麵的代碼優雅並且安全了呢?

  



最後更新:2017-04-02 06:51:53

  上一篇:go msysGit 各種中文問題的解決
  下一篇:go finally 不會執行的情況