Android Bitmap和Drawable互转及使用BitmapFactory解析图片流

一、Bitmap转Drawable

Bitmap bmp=xxx; 
BitmapDrawable bd=new BitmapDrawable(bmp);

因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

二、 Drawable转Bitmap
转成Bitmap对象后,可以将Drawable对象通过Android的SK库存成一个字节输出流,最终还可以保存成为jpg和png的文件。

Drawable d=xxx; 
BitmapDrawable bd = (BitmapDrawable) d;
Bitmap bm = bd.getBitmap();

最终bm就是我们需要的Bitmap对象了。

Drawable->Bitmap

public static Bitmap convertDrawable2BitmapByCanvas(Drawable drawable) {
  Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight(),
    drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888: Bitmap.Config.RGB_565);
  Canvas canvas = new Canvas(bitmap);
  // canvas.setBitmap(bitmap);
  drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
  drawable.getIntrinsicHeight());
  drawable.draw(canvas);
  return bitmap;
}

三、Bitmap转byte[]

public static byte[] convertBitmap2Bytes(Bitmap bm) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  return baos.toByteArray();
}

四、BitmapFactory图片解析工具类

  BitmapFactory自带了一套decodeXXX(params...)方法,用于解析获取图片;其中一个需要注意的细节是opts参数,因为比较常见的是大图,或者一系列的图片,而过多的图片就会有内存溢出的潜在可能性,这个opts参数即是用于设置解析图片流的一些参数的,比如设置宽高,图片缩放等等;
  下面是google官方关于BitmapFactory.Options的字段的解释;

Fields
public BitmapinBitmapIf set, decode methods that take the Options object will attempt to reuse this bitmap when loading content.
public intinDensityThe pixel density to use for the bitmap.
public booleaninDitherIf dither is true, the decoder will attempt to dither the decoded image.
public booleaninInputShareableThis field works in conjuction with inPurgeable.
public booleaninJustDecodeBoundsIf set to true, the decoder will return null (no bitmap), but the out...
public booleaninMutableIf set, decode methods will always return a mutable Bitmap instead of an immutable one.
public booleaninPreferQualityOverSpeedIf inPreferQualityOverSpeed is set to true, the decoder will try to decode the reconstructed image to a higher quality even at the expense of the decoding speed.
public Bitmap.ConfiginPreferredConfigIf this is non-null, the decoder will try to decode into this internal configuration.
public booleaninPremultipliedIf true (which is the default), the resulting bitmap will have its color channels pre-multipled by the alpha channel.
public booleaninPurgeableIf this is set to true, then the resulting bitmap will allocate its pixels such that they can be purged if the system needs to reclaim memory.
public intinSampleSizeIf set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory.
public booleaninScaledWhen this flag is set, if inDensity and inTargetDensity are not 0, the bitmap will be scaled to matchinTargetDensity when loaded, rather than relying on the graphics system scaling it each time it is drawn to a Canvas.
public intinScreenDensityThe pixel density of the actual screen that is being used.
public intinTargetDensityThe pixel density of the destination this bitmap will be drawn to.
public byte[]inTempStorageTemp storage to use for decoding.
public booleanmCancelFlag to indicate that cancel has been called on this object.
public intoutHeightThe resulting height of the bitmap, set independent of the state of inJustDecodeBounds.
public StringoutMimeTypeIf known, this string is set to the mimetype of the decoded image.
public intoutWidthThe resulting width of the bitmap, set independent of the state of inJustDecodeBounds.

挑几个通常情况下需要用到的字段
inJustDecodeBounds:boolean类型变量,设置为true时表示只获取图片的宽高;而不获取实际图片;
outHeight:此参数可以进行设置,值为图片的高度,可以通过设置inJustDecodeBounds为true获取图片的高度,然后再对这个高度进行设置,以获取理想高度的图片;
outWidght:获取图片宽度,值的含义同上;
inSampleSize:如果设置值大小1,表示获取一个小一点的图片,用于节省内存,比如:inSampleSize = 4 返回一个原图1/4宽高的图像(图片像素个数为原图1/16);

获取一个大图的步骤通常如下:
1。传入一个参数opts,设置opts.inJustDecodeBounds为true;
2。获取宽高信息,再通过outHeight、outWidth设置理想图片宽高;
3。重新设置opts.inJustDecodeBounds为false,表示需要获取图片;
4。设置inSampleSize,用于节省内存;

有时候可能还需要设置其它的参数,这里列举两个
inPreferredConfig = Bitmap.Config.ARGB_4444;默认是Bitmap.Config.ARGB_8888;
inPurgeablein、InputShareable 这两个参数需要同时使用;

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值