天天看點

Android大圖檔之縮略圖,以及對原圖按照指定寬高裁剪成縮略圖



《Android大圖檔之變換縮略圖,以及對原始大圖檔按照指定寬、高裁剪成縮略圖》

在Android的ImageView加載圖像資源過程中,出于性能和記憶體開銷的需要,有時候需要把一個原始的超大圖檔按照一定比例等比例縮放成較小的縮略圖,或者需要把原始的超大圖檔,裁剪成指定寬高值的較小圖檔,針對這種開發需求,可以使用Android SDK自身提供的工具類:ThumbnailUtils完成。

ThumbnailUtils的在Android官方的開發文檔連結位址:

http://developer.android.com/reference/android/media/ThumbnailUtils.html

下面以一個超大圖檔為例,使用ThumbnailUtils等比例縮放成縮略圖和按照指定的寬高裁剪成滿足需要的縮略圖。

測試用的MainActivity.java :

package zhangphil.thumbnail;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;
import android.os.Bundle;
import android.widget.ImageView;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		ImageView imageView0 = (ImageView) findViewById(R.id.imageView0);
		// 對原始資源圖檔不做任何處理和調整,直接加載到ImageView。
		imageView0.setImageResource(R.drawable.image);

		Bitmap sourceBitmap = BitmapFactory.decodeResource(this.getResources(),
				R.drawable.image);

		// 把原始bitmap截取成一個 MIN_SIZE * MIN_SIZE 的正方形縮略圖。注意:預設是以中心為原點截取成縮略圖。
		int MIN_SIZE = 300;
		ImageView imageView1 = (ImageView) findViewById(R.id.imageView1);
		Bitmap bmp1 = ThumbnailUtils.extractThumbnail(sourceBitmap, MIN_SIZE,
				MIN_SIZE);
		imageView1.setImageBitmap(bmp1);
		
		
		// 獲得原始bitmap的高和寬,下面将對原始Bitmap等比例縮放成縮略圖加載。
		int h = sourceBitmap.getHeight();
		int w = sourceBitmap.getWidth();

		// 縮略圖縮放的比例尺
		int THUMB_SIZE;

		THUMB_SIZE = 5;
		// 對原始圖檔Bitmap等比例縮小5倍的縮略圖
		ImageView imageView2 = (ImageView) findViewById(R.id.imageView2);
		Bitmap bmp2 = ThumbnailUtils.extractThumbnail(sourceBitmap, w / THUMB_SIZE, h
				/ THUMB_SIZE);
		imageView2.setImageBitmap(bmp2);

		// 對原始圖檔Bitmap等比例縮小10倍的縮略圖。
		THUMB_SIZE = 10;
		ImageView imageView3 = (ImageView) findViewById(R.id.imageView3);
		Bitmap bmp3 = ThumbnailUtils.extractThumbnail(sourceBitmap, w / THUMB_SIZE, h
				/ THUMB_SIZE);
		imageView3.setImageBitmap(bmp3);
	}
}
           

MainActivity.java需要的布局檔案activity_main.xml :

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

</ScrollView>           

需要處理的目标的大圖,原始大圖資源:

代碼運作處理結果如圖所示: