利用Matrix class 對影像(bitmap)進行放大和縮小

1 篇文章 / 0 new
author
利用Matrix class 對影像(bitmap)進行放大和縮小
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>

src
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) {
                scale *= 2;
                loadImage();
            }
        });
        buttonReset.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                scale = 1;
                loadImage();
            }
        });
        buttonReduce.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {
                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);
    }
}

from http://androidbiancheng.blogspot.com/2010/07/matrixbitmap.html
Free Web Hosting