Android 圖片的縮圖

來源:互聯網
上載者:User

標籤:

<1>簡介

之前往往是通過Bitmap、Drawable和Canvas配合完成,需要寫一系列繁雜的邏輯去縮小原有圖片,從而得到縮圖。

現在我給大家介紹一種比較簡單的方法:(網上有)

在Android 2.2版本中,新增了一個ThumbnailUtils工具類來是實現縮圖,此工具類的功能是強大的,使用是簡單,它提供了一個常量和三個方法。利用這些常數和方法,可以輕鬆快捷的實現圖片和視頻的縮圖功能。

<2>ThumbnailUtils工具類

常量:

OPTIONS_RECYCLE_INPUT

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

方法:

    Bitmap createVideoThumbnail(String filePath, int kind)

    建立一張視頻的縮圖。如果視頻已損壞或者格式不支援可能返回null。

              參數:

                                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: 產生目標的高度

 <3>具體實現:

package xiaosi.thumbnail;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.drawable.BitmapDrawable;import android.graphics.drawable.Drawable;import android.media.ThumbnailUtils;import android.os.Bundle;import android.widget.ImageView;public class ThumbnailActivity extends Activity {        private Bitmap bitmap = null;    private ImageView image;     @Override       public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main);          image = (ImageView) findViewById(R.id.image);          //得到原圖片        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.v);        //得到縮圖        bitmap = ThumbnailUtils.extractThumbnail(bitmap, 100, 100);          image.setImageBitmap(bitmap);      }  }  

main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout      xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical"     android:background="#999999"      android:layout_width="fill_parent"      android:layout_height="fill_parent">     <ImageView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:src="@drawable/v"         android:layout_marginLeft="10dip"/>     <TextView           android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="縮圖:"          android:textColor="#000000"/>     <ImageView android:id="@+id/image"          android:layout_width="wrap_content"          android:layout_height="wrap_content"         android:layout_marginLeft="10dip"/> </LinearLayout>

 

Android 圖片的縮圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.