`

图片缩放和旋转

阅读更多

Android图片缩放总结及比较
 原文链接:http://www.linuxidc.com/Linux/2011-08/40109.htm

 

 

使用Matrix类对图片进行 等比例缩放和旋转:

// 加载需要操作的图片
		Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
				R.drawable.default_screen);
		// 获取这个图片的宽和高
		int width = bitmapOrg.getWidth();
		int height = bitmapOrg.getHeight();

		// 定义预转换成的图片的宽和高
		int newWidth = 162;
		int newHight = 170;

		// 计算缩放率,新尺寸除原尺寸
//		float scaleWidth = (float) newWidth / width;
//		float scaleHeight = (float) newHight / height;
		float scaleWidth = 0.3f;
		float scaleHeight = 0.3f;
		// 创建操作图片用的matrix对象
		Matrix matrix = new Matrix();
		// 缩放图片动作
		matrix.postScale(scaleWidth, scaleHeight);
		//旋转图片动作
		matrix.postRotate(90);

		// 创建新的图片
		Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
				height, matrix, true);
		// 将上面创建的Bitmap转换成Drawable对象,使得其可以使用在imageView,imageButton上。
		BitmapDrawable bitmapDrawable = new BitmapDrawable(resizedBitmap);
		ImageView imageView = (ImageView)findViewById(R.id.imageView1);
		imageView.setImageBitmap(resizedBitmap);

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics