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



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

在Android的ImageView載入圖像資源過程當中,出於性能和內存開銷的需要。有時候需要把一個原始的超大圖片依照必定比例等比例縮放成較小的縮略圖。或者需要把原始的超大圖片,裁剪成指定寬高值的較小圖片,針對這樣的開發需求,可以使用Android SDK自身提供的工具類:ThumbnailUtils完畢。
ThumbnailUtils的在Android官方的開發文檔連接地址:
http://developer.android.com/reference/android/media/ThumbnailUtils.htmljava

如下以一個超大圖片爲例,使用ThumbnailUtils等比例縮放成縮略圖和依照指定的寬高裁剪成知足需要的縮略圖。android

測試用的MainActivity.java :app

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倍的縮略圖。ide

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>


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



代碼執行處理結果如圖所看到的:性能

相關文章
相關標籤/搜索