Android Matrix放大缩小图片

package androidstudy.matrix;

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;
import android.widget.RelativeLayout;

public class maxtrix extends Activity {
	private ImageView mImageView;
	private Button mButton1;
	private Button mButton2;
	private RelativeLayout layout1;
	private Bitmap bmp;
	private int id = 0;
	private int displayWidth;
	private int displayHeight;
	private float scaleWidth = 1;
	private float scaleHeight = 1;

	/** 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;
		displayHeight = dm.heightPixels - 80;
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.q2);
		mImageView = (ImageView) findViewById(R.id.imageView1);
		layout1 = (RelativeLayout) findViewById(R.id.layout1);
		mButton1 = (Button) findViewById(R.id.button1);
		mButton2 = (Button) findViewById(R.id.button2);

		mButton1.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				small();
			}
		});
		mButton2.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				big();
			}
		});
	}

	public void small() {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		double scale = 0.8;
		scaleWidth = (float) (scaleWidth * scale);
		scaleHeight = (float) (scaleHeight * scale);
		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));
		}
		id++;
		ImageView imageView = new ImageView(maxtrix.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		layout1.addView(imageView);
		setContentView(layout1);
		// 图片最大的时候。放大按钮disable,缩小的时候设为enable
		mButton2.setEnabled(true);
	}

	public void big() {
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		double scale = 1.25;
		scaleWidth = (float) (scaleWidth * scale);
		scaleHeight = (float) (scaleHeight * scale);
		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));
		}
		id++;
		ImageView imageView = new ImageView(maxtrix.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		layout1.addView(imageView);
		setContentView(layout1);
		// 如果在放大图片就超出屏幕,就把Button disable
		if (scaleWidth * scale * bmpWidth > displayWidth
				|| scaleHeight * scale * bmpHeight > displayHeight)
			mButton2.setEnabled(false);

	}
}
暂无评论
  • 1:请一针见血的评论。
  • 2:评论需要审核通过后才能显示。
  • 3:评论字数限制在1000字以内。
  • 当前字数:0
热门文章
推荐文章
随机文章
关于本站 - 广告服务 - 版权声明 - 联系我们 - 友情链接 - 网站地图 - 帮助中心