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 Bitmap | inBitmap | If set, decode methods that take the Options object will attempt to reuse this bitmap when loading content. | |||||||||
| public int | inDensity | The pixel density to use for the bitmap. | |||||||||
| public boolean | inDither | If dither is true, the decoder will attempt to dither the decoded image. | |||||||||
| public boolean | inInputShareable | This field works in conjuction with inPurgeable. | |||||||||
| public boolean | inJustDecodeBounds | If set to true, the decoder will return null (no bitmap), but the out... | |||||||||
| public boolean | inMutable | If set, decode methods will always return a mutable Bitmap instead of an immutable one. | |||||||||
| public boolean | inPreferQualityOverSpeed | If 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.Config | inPreferredConfig | If this is non-null, the decoder will try to decode into this internal configuration. | |||||||||
| public boolean | inPremultiplied | If true (which is the default), the resulting bitmap will have its color channels pre-multipled by the alpha channel. | |||||||||
| public boolean | inPurgeable | If 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 int | inSampleSize | If set to a value > 1, requests the decoder to subsample the original image, returning a smaller image to save memory. | |||||||||
| public boolean | inScaled | When 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 int | inScreenDensity | The pixel density of the actual screen that is being used. | |||||||||
| public int | inTargetDensity | The pixel density of the destination this bitmap will be drawn to. | |||||||||
| public byte[] | inTempStorage | Temp storage to use for decoding. | |||||||||
| public boolean | mCancel | Flag to indicate that cancel has been called on this object. | |||||||||
| public int | outHeight | The resulting height of the bitmap, set independent of the state of inJustDecodeBounds. | |||||||||
| public String | outMimeType | If known, this string is set to the mimetype of the decoded image. | |||||||||
| public int | outWidth | The 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 这两个参数需要同时使用;
Android Bitmap和Drawable互转及使用BitmapFactory解析图片流的更多相关文章
- Android Bitmap与DrawAble与byte[]与InputStream之间的转换工具类【转】
package com.soai.imdemo; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; ...
- Android Bitmap与String互转(转)
/** * 图片转成string * * @param bitmap * @return */ public static String convertIconToString(Bitmap bitm ...
- Android——BitMap(位图)相关知识总结贴
Android中文API(136) —— Bitmap http://www.apkbus.com/android-54644-1-1.html Android 4.0 r1 API—Bitmap(S ...
- Android,View转换bitmap,bitmap转换drawable
Android View转换Bitmap,Bitmap转换Drawable //测试设置bitmap View view1 = ViewGroup.inflate(context, R.layout. ...
- Android 图片Bitmap,drawable,res资源图片之间转换
一.知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等.一种逐像素的显示对象,其执行效率高,但缺点也 ...
- Bitmap byte[] InputStream Drawable 互转
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStrea ...
- Android Bitmap Drawable byte[] InputStream 相互转换方法
用android处理图片的时候,由于得到的资源不一样,所以经常要在各种格式中相互转化,以下介绍了 Bitmap Drawable byte[] InputStream 之间的转换方法: import ...
- Android图片二进制与Bitmap、Drawable之间的转换
Android图片二进制与Bitmap.Drawable之间的转换 Java代码 public byte[] getBitmapByte(Bitmap bitmap){ ByteArray ...
- Android中Bitmap,byte[],Drawable相互转化
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
随机推荐
- (转) 如何从 0 开始学 ruby on rails (漫步版)
原文:http://readful.com/post/12322300571/0-ruby-on-rails ruby 是一门编程语言,ruby on rails 是 ruby 的一个 web 框架, ...
- ServletContext详解 以及用法
ServletContext,是一个全局的储存信息的空间,服务器开始,其就存在,服务器关闭,其才释放.request,一个用户可有多个:session,一个用户一个:而servletContext,所 ...
- Qt 计算两个日前间隔天数
某一个大神写的 改写了一点 请无视注释 //时间计算法则 /********************************************************************** ...
- Monkey用真机做测试的步骤
1 必备条件 1) 手机需要先获取root权限: 2) 手机和电脑相连(电脑可以访问手机里面的文件) 2 操作步骤 1) 使用adb devices 命令查看电脑手机是否相连: 下图表示手机已连上电 ...
- 1. 两数之和【Leetcode中国,by java】
给定一个整数数组和一个目标值,找出数组中和为目标值的两个数. 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用. 示例: 给定 nums = [2, 7, 11, 15], target ...
- 【Python 开发】第二篇 :Python安装
一.python3.x安装 1)由于centos7原本就安装了Python2,而且这个Python2不能被删除,因为有很多系统命令,比如yum都要用到. 官网:https://www.python.o ...
- 【QT】宏
宏 Q_CORE_EXPORT _CORE_EXPORT 其实是一个宏,用来说明这是一个动态库导出类.QT是个跨平台的库,而不同的操作系统,不同的编译器,对动态库的导出说明是不一样的,比如,在wind ...
- 购物单:Excel的应用
题目描述: 小明刚刚找到工作,老板人很好,只是老板夫人很爱购物.老板忙的时候经常让小明帮忙到商场代为购物.小明很厌烦,但又不好推辞. 这不,XX大促销又来了!老板夫人开出了长长的购物单,都是有打折优惠 ...
- IntelliJ IDEA 2017.3/2018.1/.2 激活
传统的License Server方式已经无法注册IntelliJ IDEA2017.3的版本了. http://idea.lanyus.com,这个网站有破解补丁和注册码两种方式,另外http:// ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...