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) ...
随机推荐
- 在Sqlserver中生成随机数据
百度了各种随机生成,集中摘录如下: 一.循环写入千万级测试数据 DECLARE @i int ) BEGIN INSERT INTO A_User(username,password,addtime, ...
- Webservice HTTP
由于项目需要:自己写了一个WebserviceDemo,把遇到的问题记下来. 方式一 :使用代理类来访问Webservice,此方式不讲解,感觉复杂(神坑). (生成的代理路径 C:\Users\ad ...
- 浅析JVM内存区域及垃圾回收
一.JVM简介 JVM,全称Java Virtual Machine,即Java虚拟机.以Java作为编程语言所编写的应用程序都是运行在JVM上的.JVM是一种用于计算设备的规范,它是一个虚构出来的计 ...
- Qt-QML-Slider-滑块-Style
感觉滑块这个东西,可以算是一个基本模块了,在我的项目中也有这个模块,今天我将学一下一下滑块的使用以及美化工作. 想学习滑块,那就要先建立一个滑块,新建工程什么的这里就省略了,不会的可以看我前面的几篇文 ...
- Linux命令应用大词典-第2章 获取帮助
2.1 help:查看内部Shell命令帮助信息 2.2 man:显示在线手册页 2.3 manpath:查看和设置man手册页的查询路径 2.4 info:阅读info格式的文件 2.5 pinfo ...
- yun rpm
RPM:RedHat Package Manager的简称,是一种数据库记录的方式的管理机制.当需要安装的软件的依赖软件都已经安装,则继续安装,否则不予安装. 特点:1.已经编译并打包完成2.软件的信 ...
- 并行程序模拟(Concurrency Simulator, ACM/ICPC World Finals 1991,Uva210)
任务介绍 你的任务是模拟n个程序的并行运算.(按照输入编号为1~n)的并行执行. 代码实现 #define LOCAL #include<bits/stdc++.h> using name ...
- CMD Markdown basic & Math Cheatsheet
CMD Markdown basic & Math Cheatsheet I am using CMD Markdown both at work and for study.You can ...
- Machine Learning笔记整理 ------ (四)线性模型
1. 线性模型 基本形式:给定由d个属性描述的样本 x = (x1; x2; ......; xd),其中,xi是x在第i个属性上的取值,则有: f(x) = w1x1 + w2x2 + ...... ...
- eos开发指南
十分钟教你开发EOS智能合约 在CSDN.柏链道捷(PDJ Education).HelloEOS.中关村区块链产业联盟主办的「EOS入门及最新技术解读」专场沙龙上,柏链道捷(PDJ Educatio ...