动态放大缩小ImageView里的图片(运用Matrix对象来缩放图片文件)
?AndroidDecodeFile文件
?
package com.wm.AndroidDecodeFile;
import java.io.File;
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.view.View.OnClickListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class AndroidDecodeFile extends Activity {
?private ImageView mImageView;
?private Button mButton01;
?private Button mButton02;
?private AbsoluteLayout layout1;
?private Bitmap bmp;
?private int id = 0;
?private int displayWidth;
?private int displayHeight;
?private float scaleWidth = 1;
?private float scaleHeight = 1;
?private String fileName;
?/** Called when the activity is first created. */
?@Override
?public void onCreate(Bundle savedInstanceState) {
??super.onCreate(savedInstanceState);
??setContentView(R.layout.main);
??
??/* 取得屏幕分辨率大小 */
??DisplayMetrics dm = new DisplayMetrics();
??getWindowManager().getDefaultDisplay().getMetrics(dm);
??displayWidth = dm.widthPixels;
??/* 屏幕高度须扣除下方Button高度 */
??displayHeight = dm.heightPixels - 80;
??/* 初始化相关变量 */
??bmp = BitmapFactory.decodeResource(getResources(), R.drawable.p2);
??mImageView = (ImageView) findViewById(R.id.ImageView01);
??layout1 = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout01);
??
??mButton01 = (Button) findViewById(R.id.Button01);
??mButton01.setText("大");
??mButton01.setOnClickListener(myBigClick);
??mButton02 = (Button) findViewById(R.id.Button02);
??mButton02.setText("小");
??mButton02.setOnClickListener(mySmallClick);
?}
?private OnClickListener myBigClick = new OnClickListener() {
??public void onClick(View arg0) {
???big();???
??}
?};
?private OnClickListener mySmallClick = new OnClickListener() {
??public void onClick(View arg0) {
???small();
??}
?};
?/* 图片缩小的method */
?private void small() {
??int bmpWidth = bmp.getWidth();
??int bmpHeight = bmp.getHeight();
??/* 设置图片缩小的比例 */
??double scale = 0.8;
??/* 计算出这次要缩小的比例 */
??scaleWidth = (float) (scaleWidth * scale);
??scaleHeight = (float) (scaleHeight * scale);
??/* 产生reSize后的Bitmap对象 */
??Matrix matrix = new Matrix();
??matrix.postScale(scaleWidth, scaleHeight);
??Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
????matrix, true);
??if (id == 0) {
???/* 如果是第一次按,就删除原来默认的ImageView */
???layout1.removeView(mImageView);
??} else {
???/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
???layout1.removeView((ImageView) findViewById(id));
??}
??/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
??id++;
??ImageView imageView = new ImageView(AndroidDecodeFile.this);
??imageView.setId(id);
??imageView.setImageBitmap(resizeBmp);
??layout1.addView(imageView);
??setContentView(layout1);
??/* 因为图片放到最大时放大按钮会disable,所以在缩小时把他重设为enable */
??mButton01.setEnabled(true);
?}
?/* 图片放大的method */
?private void big() {
??int bmpWidth = bmp.getWidth();
??int bmpHeight = bmp.getHeight();
??/* 设置图片放大的比例 */
??double scale = 1.25;
??/* 计算这次要放大的比例 */
??scaleWidth = (float) (scaleWidth * scale);
??scaleHeight = (float) (scaleHeight * scale);
??/* 产生reSize后的Bitmap对象 */
??Matrix matrix = new Matrix();
??matrix.postScale(scaleWidth, scaleHeight);
??Bitmap resizeBmp = Bitmap.createBitmap(bmp, 0, 0, bmpWidth, bmpHeight,
????matrix, true);
??if (id == 0) {
???/* 如果是第一次按,就删除原来设置的ImageView */
???layout1.removeView(mImageView);
??} else {
???/* 如果不是第一次按,就删除上次放大缩小所产生的ImageView */
???layout1.removeView((ImageView) findViewById(id));
??}
??/* 产生新的ImageView,放入reSize的Bitmap对象,再放入Layout中 */
??id++;
??ImageView imageView = new ImageView(AndroidDecodeFile.this);
??imageView.setId(id);
??imageView.setImageBitmap(resizeBmp);
??layout1.addView(imageView);
??setContentView(layout1);
??/* 如果再放大会超过屏幕大小,就把Button disable */
??if (scaleWidth * scale * bmpWidth > displayWidth
????|| scaleHeight * scale * bmpHeight > displayHeight) {
???mButton01.setEnabled(false);
??}
?}
}
?
main.xml文件
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/AbsoluteLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/ImageView01"
android:layout_width="290px"
android:layout_height="250px"
android:layout_x="0px"
android:layout_y="0px"
android:src="@drawable/p2"></ImageView>
<Button
android:id="@+id/Button01"
android:layout_width="90px"
android:layout_height="60px"
android:textSize="18sp"
android:layout_x="20px"
android:layout_y="372px"></Button>
<Button
android:id="@+id/Button02"
android:layout_width="90px"
android:layout_height="60px"
android:textSize="18sp"
android:layout_x="210px"
android:layout_y="372px"></Button>
</AbsoluteLayout>
?
讲的比较详细