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" /> <ImageView android:id="@+id/imageView" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center" ></ImageView> <ImageView android:id="@+id/newImage" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" ></ImageView> <Button android:id="@+id/smallButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="small" ></Button> <Button android:id="@+id/bigButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="big" ></Button> </LinearLayout> MatrixActivity.java package com.itxinke.www; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MatrixActivity extends Activity { /** Called when the activity is first created. */ private ImageView imageView,newImageView; private Button smallButton,bigButton; private int displayWidth,displayHeight; private Bitmap bmp; private float scaleWidth = 1; private float scaleHeight = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); displayWidth = dm.widthPixels; displayHeight = dm.heightPixels - 80; bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ex04_23); imageView = (ImageView)findViewById(R.id.imageView); newImageView = (ImageView)findViewById(R.id.newImage); smallButton = (Button)findViewById(R.id.smallButton); bigButton = (Button)findViewById(R.id.bigButton); imageView.setImageResource(R.drawable.s); smallButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub small(); } }); bigButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub big(); } }); } public void small() { int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); float scale = 0.8f; scaleWidth = (float)(scaleWidth * scale); scaleHeight = (float)(scaleHeight * scale); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true); newImageView.setImageBitmap(newBmp); if(false == bigButton.isEnabled()) { bigButton.setEnabled(true); } } public void big() { int bmpWidth = bmp.getWidth(); int bmpHeight = bmp.getHeight(); float scale = 1.1f; scaleWidth = (float)(scaleWidth * scale); scaleHeight = (float)(scaleHeight * scale); Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap newBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight, matrix, true); newImageView.setImageBitmap(newBmp); if(newBmp.getWidth() > displayWidth || newBmp.getHeight() > displayHeight) { bigButton.setEnabled(false); } } }