您的位置:首页 > 其它

使用Matrix放大或缩小图片

2014-12-13 11:28 232 查看
今天在工程中需要用到图片做标注,发现给定的图片宽高有点大,本来想再截一张小图片,太麻烦。

发现可以用代码自定义放大缩小图片,然后就试着做了一下,效果还不错。

下面贴一下我的代码:

1、从工程的资源文件中读取一张图片

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
				R.drawable.custom_info_bubble);
2、计算bitmap的宽和高
int bitmapWidth = bitmap.getWidth();
int bitmapHeight = bitmap.getHeight();
3、设置图片缩小的比例
double scale = 0.5;
4、计算出这次要缩小的比例
float scaleWidth = (float) (1 * scale);
float scaleHeight = (float) (1 * scale);
5、使用Matrix产生缩小后的Bitmap对象

Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmapWidth, bitmapHeight,matrix, true);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: