Matrix放大、缩小和旋转图片

一、放大、缩小图片

 

private LinearLayout imageLayout;
	private ImageView imageView;
	private Button bt_big;
	private Button bt_small;

	private float scaleWidth = 1;
	private float scaleHeight = 1;
	private int id = 0;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.big_small);

		imageLayout = (LinearLayout) findViewById(R.id.imageLayout);
		imageView = (ImageView) findViewById(R.id.imageView1);
		bt_big = (Button) findViewById(R.id.button1);
		bt_small = (Button) findViewById(R.id.button2);

		bt_big.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				big(imageView);
			}
		});
		
		bt_small.setOnClickListener(new Button.OnClickListener() {
			public void onClick(View v) {
				small(imageView);
			}
		});
	}

	/* 图片放大的method */
	private void big(ImageView mImageView) {
		// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像
		mImageView.setDrawingCacheEnabled(true);
		// 获取ImageView中的图像
		Bitmap bmp = Bitmap.createBitmap(mImageView.getDrawingCache());
		// 从ImaggeView对象(iv_photo)中获取图像后,要记得调用setDrawingCacheEnabled(false)
		// 清空画图缓冲区
		mImageView.setDrawingCacheEnabled(false);
		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){
			imageLayout.removeView(mImageView);
		}
		else{
			imageLayout.removeView((ImageView)findViewById(id));
		}
		id = id + 1;
		ImageView imageView = new ImageView(BigAndSmall.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		imageLayout.addView(imageView);
	}
	
	/* 图片缩小的method */
	private void small(ImageView mImageView) {
		// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像
		mImageView.setDrawingCacheEnabled(true);
		// 获取ImageView中的图像
		Bitmap bmp = Bitmap.createBitmap(mImageView.getDrawingCache());
		// 从ImaggeView对象(iv_photo)中获取图像后,要记得调用setDrawingCacheEnabled(false)
		// 清空画图缓冲区
		mImageView.setDrawingCacheEnabled(false);
		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){
			imageLayout.removeView(mImageView);
		}
		else{
			imageLayout.removeView((ImageView)findViewById(id));
		}
		id = id + 1;
		ImageView imageView = new ImageView(BigAndSmall.this);
		imageView.setId(id);
		imageView.setImageBitmap(resizeBmp);
		imageLayout.addView(imageView);
	}

 big_small.xml

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:orientation="vertical">
	<LinearLayout
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:id="@+id/imageLayout">
		<ImageView
			android:layout_width="wrap_content"
			android:src="@drawable/img2"
			android:layout_height="wrap_content"
			android:id="@+id/imageView1"></ImageView>
	</LinearLayout>
	<Button
		android:text="放大"
		android:id="@+id/button1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"></Button>
	<Button
		android:text="缩小"
		android:id="@+id/button2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"></Button>
</LinearLayout>
 


 

 

二、旋转图片

 

	private ImageView imageView;
	private Button bt_left;
	private Button bt_right;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.rotate);
		
		imageView = (ImageView) findViewById(R.id.imageView1);
		bt_left = (Button) findViewById(R.id.button1);
		bt_right = (Button) findViewById(R.id.button2);
		
		bt_left.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				rotate(imageView , 90);
			}
			
		});
		
		bt_right.setOnClickListener(new OnClickListener(){

			public void onClick(View v) {
				rotate(imageView , -90);
			}
			
		});
		
		
	}

	public void rotate(ImageView imageView, int scaleAngle) {
		Bitmap bitmap = getBitmapFromImage(imageView);
		int widthOrig = bitmap.getWidth();
		int heightOrig = bitmap.getHeight();
		/* ScaleTimes=1,维持1:1的宽高比例 */
		int scaleTimes = 1;
		int newWidth = widthOrig * scaleTimes;
		int newHeight = heightOrig * scaleTimes;
		/* 计算旋转的Matrix比例 */
		float scaleWidth = ((float) newWidth) / widthOrig;
		float scaleHeight = ((float) newHeight) / heightOrig;
		Matrix matrix = new Matrix();
		/* 使用Matrix.postScale设定维度 */
		matrix.postScale(scaleWidth, scaleHeight);
		/* 使用Matrix.postRotate方法旋转Bitmap */
		// matrix.postRotate(5*ScaleAngle);
		matrix.setRotate(5 * scaleAngle);
		/* 建立新的Bitmap对象 */
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, widthOrig,
				heightOrig, matrix, true);
		BitmapDrawable myNewBitmapDrawable = new BitmapDrawable(resizedBitmap);
		imageView.setImageDrawable(myNewBitmapDrawable);
	}

	public Bitmap getBitmapFromImage(ImageView mImageView) {
		// ImageView对象(iv_photo)必须做如下设置后,才能获取其中的图像
		mImageView.setDrawingCacheEnabled(true);
		// 获取ImageView中的图像
		Bitmap bmp = Bitmap.createBitmap(mImageView.getDrawingCache());
		// 从ImaggeView对象(iv_photo)中获取图像后,要记得调用setDrawingCacheEnabled(false)
		// 清空画图缓冲区
		mImageView.setDrawingCacheEnabled(false);
		return bmp;
	}
 



 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值