现在的位置: 首页 > 综合 > 正文

Android缩放drawable 压缩图片 matrix Android缩放drawable Matrix

2014年10月12日 ⁄ 综合 ⁄ 共 2597字 ⁄ 字号 评论关闭

Q:加载时图片时有一些图片太大,导致内存溢出,想把这些图片压缩成一个缩略图,该怎么做?

不能只是缩小图片大小,而是要把体积降下来,几百K的图片压缩成几K或几百B.

A1:

压缩图片质量:   
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, fos);   
其中的quality为0~100, 可以压缩图片质量, 不过对于大图必须对图片resize  

这个是等比例缩放:
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);

newBitmap = Bitmap.createBitmap(oldBitmap, 0, 0, width, height, matrix,
true);//用距阵的方式缩放


这个是截取图片某部分:
bitmap = Bitmap.createBitmap(bitmap, x, y, width, height);

这几个方法都是针对Bitmap的, 不过鉴于Bitmap可以从file中读取, 也可以写入file.  

这是我知道Android自带库里中唯一可以缩放和压缩的图片方法.

--------------------------------------------------------------------------------------------------------------------------------------------------

A2:

内存溢出,你这么处理就可以。用完及时回收
BitmapFactory.Options options = new BitmapFactory.Options();

options.inTempStorage = new byte[16*1024];

Bitmap bitmapImage = BitmapFactory.decodeFile(path,opt);

Q:Bitmap如何保存成为一个bmp文件:

A:很多网友可能发现了Android的Bitmap对象无法保存成为一个bmp文件,即提供的方法只有compress(Bitmap.CompressFormat format, int quality, OutputStream stream) ,可以存为png和jpg,png可能还好说,但是jpg是有损压缩会降低图片的质量,其实Google还提供了一个API在Bitmap类,通过copyPixelsToBuffer(Buffer dst) 这个方法来解决,Buffer类型,和前几天我们说到的NIO中的ByteBuffer处理方式一样,需要说明的是java中的Buffer在内存中是连续成块的,同时底层有C++支持效率还是很高的。

  通过copyPixelsToBuffer(Buffer dst) 方法将会返回一个RGB8888格式的DIB文件,DIB位图和设备无关,这里Android123提醒大家,如果想显示出位图,还需要将其加上位图的文件头才行。

Q:

Android缩放drawable Matrix

一、   相关概念

1.         Drawable 就是一个可画的
对象,其可能是一张位图(
 
BitmapDrawable ),也可能是一个 图形(ShapeDrawable ),还有可能是一
个图层(
 
LayerDrawable ),我们根据画图的需求,创建相应的可画对象

2.         Canvas 画布,绘制的目
的区域,用于绘图

3.         Bitmap 位图,用于图的
处理

4.         Matrix 矩阵,此例中用
于操作图片

二、   步骤

1.          drawable 画到位图对象上

2.         对位图对象做缩放(或旋转等)操作

3.         把位图再转换成 drawable

三、 示例 

static Bitmap drawableToBitmap(Drawable drawable) // drawable 转换成 bitmap 
         {
                   int width = drawable.getIntrinsicWidth();   // 取 drawable 的长宽 
                   int height = drawable.getIntrinsicHeight();
                 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888:Bitmap.Config.RGB_565;         // 取 drawable 的颜色格式 
                   Bitmap bitmap = Bitmap.createBitmap(width, height, config);     // 建立对应 bitmap 
                   Canvas canvas = new Canvas(bitmap);         // 建立对应 bitmap 的画布 
                   drawable.setBounds(0, 0, width, height);
                   drawable.draw(canvas);      // 把 drawable 内容画到画布中 
                   return bitmap;
         }

         static Drawable zoomDrawable(Drawable drawable, int w, int h)
         {
                   int width = drawable.getIntrinsicWidth();
                   int height= drawable.getIntrinsicHeight();
                   Bitmap oldbmp = drawableToBitmap(drawable); // drawable 转换成 bitmap 
                   Matrix matrix = new Matrix();   // 创建操作图片用的 Matrix 对象 
                   float scaleWidth = ((float)w / width);   // 计算缩放比例 
                   float scaleHeight = ((float)h / height);
                   matrix.postScale(scaleWidth, scaleHeight);         // 设置缩放比例 
                   Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);       // 建立新的 bitmap ,其内容是对原 bitmap 的缩放后的图 
                   return new BitmapDrawable(newbmp);       // 把 bitmap 转换成 drawable 并返回 
         }
}

抱歉!评论已关闭.