1、Drawable → Bitmap

Java代码
  1. publicstaticBitmapdrawableToBitmap(Drawabledrawable){
  2. Bitmapbitmap=Bitmap
  3. .createBitmap(
  4. drawable.getIntrinsicWidth(),
  5. drawable.getIntrinsicHeight(),
  6. drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
  7. :Bitmap.Config.RGB_565);
  8. Canvascanvas=newCanvas(bitmap);
  9. //canvas.setBitmap(bitmap);
  10. drawable.setBounds(0,0,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
  11. drawable.draw(canvas);
  12. returnbitmap;
  13. }

2、从资源中获取Bitmap

Java代码
  1. Resourcesres=getResources();
  2. Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.pic);

3、Bitmap → byte[]

Java代码
  1. privatebyte[]Bitmap2Bytes(Bitmapbm){
  2. ByteArrayOutputStreambaos=newByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG,100,baos);
  4. returnbaos.toByteArray();
  5. }


4、 byte[] → Bitmap

Java代码
  1. privateBitmapBytes2Bimap(byte[]b){
  2. if(b.length!=0){
  3. returnBitmapFactory.decodeByteArray(b,0,b.length);
  4. }
  5. else{
  6. returnnull;
  7. }
  8. }

以上是我在实践中遇到的一些转换,以后遇到类似的就不用到处找了,希望对大家也有一点用处!

更多相关文章

  1. android NoSuchMethodError getDrawable
  2. Android(安卓)DynamicLoadApk 开源插件开发项目代码剖析
  3. android资源配置文件
  4. Android(安卓)EditText软键盘弹出时防止布局上移和关闭软键盘
  5. Android——动态广播、静态广播
  6. 背景图片显示问题 图片显示错误 android开发
  7. android 中ScrollView的使用
  8. Android(安卓)获取WIFI MAC地址的方法
  9. Android核心基础-3.Android(安卓)开发环境搭建

随机推荐

  1. linux报错之--->export `=' not a valid
  2. shell脚本let命令在windows下编辑后上传
  3. Linux下Tomcat开启查看GC信息
  4. 管道是否会阻止它的执行中途? [重复]
  5. VIM 编辑器使用基础介绍
  6. Re:从零开始的Linux之路(文件权限)
  7. Linux 删除文件夹和文件的命令
  8. 嵌入式Linux要学哪些东西?你真的造吗?
  9. gdb学习(二)[第二版]
  10. Linux命令备忘实例(10)——目录管理