andorid优化之图片的缩放

来源:互联网 发布:尹美莱 知乎 编辑:程序博客网 时间:2024/05/29 17:10

这几天开发一个app,发现导航页添加几个图片后特别卡,滑动的时候,明显的有卡顿感,发现是图片太大的问题,后来经过简单的缩放,就没有卡顿感了

1,首先操作图片要把图片读取到内存中,意思就是把图片转换成Bitmap对象

据提代码如下

Bitmap bitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.guide_01);

2,定义矩阵对象

Matrix matrix = new Matrix();

3,缩放原图

matrix.postScale(0.5f, 0.5f);


Matrix主要用于对平面进行缩放,平移,旋转以及倾斜操作,为简化矩阵变换,Android封装了一系列方法来进行矩阵变换,其中包括pre系列方法:preScale,preTranslate,preRotate,preSkew,set系列方法:setScale,setTranslate,setRotate,setSkew,post系列方法:postScale,postTranslate,postRotate,postSkew。

关于Matrix详细请看大牛博客:http://blog.csdn.net/upseven/article/details/9275207

4,重新创建图片

bitmap1 = Bitmap.createBitmap(bitmap1, 0, 0, bitmap1.getWidth(), bitmap1.getHeight(),  matrix, true); 

bmp.getWidth(), bmp.getHeight()分别表示缩放后的位图宽高  


Bitmap.createBitmap(source, x, y, width, height, m, filter)
这个方法具体的参数如下
source: 要处理的一个原图<span style="white-space:pre"></span> * x:在原图上的x起始坐标<span style="white-space:pre"></span> * y:y的起始坐标<span style="white-space:pre"></span> * width:要创建的图片从x坐标到水平方向上的距离<span style="white-space:pre"></span> * height:要创建的图片从y坐标到垂直方向上的距离<span style="white-space:pre"></span> * matrix:矩阵对象,确定图片放缩的比例<span style="white-space:pre"></span> * boolean filter:是否过滤,具体我也不是很清楚

5,设置给控件

imageView.setImageBitmap(bitmap1);

6,设置充满屏幕

imageView.setScaleType(ScaleType.FIT_XY);






1 0
原创粉丝点击