2010年7月3日星期六

利用Matrix類對位圖(bitmap)進行放大和縮小

本練習利用Matrix類的postScale()方法, 通過Bitmap.createBitmap()方法從一個源位圖創建另一個縮放位圖, 從而做到放大和縮小的效果.

(本應用程序如果進行太大的縮放會發生錯誤, 但這不是本練習的重點, 所以筆者沒有特別處理.)

利用Matrix類對位圖(bitmap)進行放大和縮小

main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
>
<Button
android:id="@+id/enlarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Enlarge + "
/>
<Button
android:id="@+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Original Size "
/>
<Button
android:id="@+id/Reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Reduce - "
/>
</LinearLayout>
<ImageView
android:id="@+id/imageview"
android:layout_gravity="center"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
/>
</LinearLayout>


AndroidImage.java
package com.AndroidImage;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class AndroidImage extends Activity {

private ImageView myImageView;
private float scale;
private Bitmap bitmap;
int bmpWidth, bmpHeight;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

myImageView = (ImageView)findViewById(R.id.imageview);

Button buttonEnlarge = (Button)findViewById(R.id.enlarge);
Button buttonReset = (Button)findViewById(R.id.reset);
Button buttonReduce = (Button)findViewById(R.id.Reduce);

bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
bmpWidth = bitmap.getWidth();
bmpHeight = bitmap.getHeight();
scale= 1;

loadImage();

buttonEnlarge.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 2;
loadImage();

}});

buttonReset.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale = 1;
loadImage();

}});

buttonReduce.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
scale *= 0.5;
loadImage();

}});

}

private void loadImage(){

Matrix matrix = new Matrix();
matrix.postScale(scale, scale);

Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true);
myImageView.setImageBitmap(resizedBitmap);

}
}




沒有留言:

發佈留言