一、相关概念

1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵

二、Bitmap

1、从资源中获取Bitmap

  1. Resources res = getResources();
  2. Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);


2、Bitmap → byte[]

  1. public byte[] Bitmap2Bytes(Bitmap bm) {
  2. ByteArrayOutputStream baos = new ByteArrayOutputStream();
  3. bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
  4. return baos.toByteArray();
  5. }

3、byte[] → Bitmap

  1. public Bitmap Bytes2Bimap(byte[] b) {
  2. if (b.length != 0) {
  3. return BitmapFactory.decodeByteArray(b, 0, b.length);
  4. } else {
  5. return null;
  6. }
  7. }

4、Bitmap缩放

5、将Drawable转化为Bitmap

6、获得圆角图片

7、获得带倒影的图片

三、Drawable

1、Bitmap转换成Drawable

 Bitmap bm=xxx; //xxx根据你的情况获取
BitmapDrawable bd= new BitmapDrawable(getResource(), bm);
因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

2、Drawable缩放

     public static Drawable zoomDrawable(Drawable drawable, int w, int h)        {
int width = drawable.getIntrinsicWidth();
int height = drawable.getIntrinsicHeight();
// drawable转换成bitmap
Bitmap oldbmp = drawableToBitmap(drawable);
// 创建操作图片用的Matrix对象
Matrix matrix = new Matrix();
// 计算缩放比例
float sx = ((float) w / width);
float sy = ((float) h / height);
// 设置缩放比例
matrix.postScale(sx, sy);
// 建立新的bitmap,其内容是对原bitmap的缩放后的图
Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
matrix, true);
return new BitmapDrawable(newbmp);
}
 // 获取bitmap占用的字节数
protected int sizeOf(Bitmap data) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB_MR1) {
return data.getRowBytes() * data.getHeight();
} else {
return data.getByteCount();
}
} // 3.以RGB_565方式读入图片
public Bitmap readBitMap(Context context, int resId) {
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
} // 4.获取ImageView和其中drawable的大小
// 获取ImageView和其中drawable大小需在onWindowFocusChanged获取,在oncreate中返回的结果是0
public void onWindowFocusChanged(boolean hasFocus) {
ImageView imageView = (ImageView) findViewById(R.id.test1);
Log.v("Testresult", "width= " + imageView.getWidth() + " height= "
+ imageView.getHeight());
Log.v("Testresult", "drawawidth= "
+ imageView.getDrawable().getBounds().width()
+ " drawableheight= "
+ imageView.getDrawable().getBounds().height());
}
 

Android中Bitmap和Drawable,等相关内容的更多相关文章

  1. Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  2. 【Android】[转] Android中Bitmap,byte[],Drawable相互转化

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  3. Android中Bitmap和Drawable

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  4. Android中Bitmap和Drawable详解

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  5. [转载]Android中Bitmap和Drawable

    一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...

  6. Android中 Bitmap和Drawable相互转换的方法

    1.Drawable->Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.d ...

  7. Android中Bitmap、Drawable、byte[]转换

    public byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); ...

  8. Android中Bitmap, Drawable, Byte,ID之间的转化

    Android中Bitmap, Drawable, Byte,ID之间的转化 1.  Bitmap 转化为 byte ByteArrayOutputStream out = new ByteArray ...

  9. android中Bitmap的放大和缩小的方法

    android中Bitmap的放大和缩小的方法 时间 2013-06-20 19:02:34  CSDN博客原文  http://blog.csdn.net/ada168855/article/det ...

随机推荐

  1. dubbo zookeeper模型

    本文摘自dubbo的官方文档,原文请参见: http://alibaba.github.io/dubbo-doc-static/User+Guide-zh.htm#UserGuide-zh-Zooke ...

  2. 使用nodejs搭建服务器显示HTML页面

    首先安装express 在命令行输入:npm install express -g 安装完成后可以查看安装情况:npm ls -g 然后创建server.js文件 var express = requ ...

  3. protobuf 向前兼容向后兼容

    http://blog.163.com/jiang_tao_2010/blog/static/12112689020114305013458/ 不错的protobuf.. protobuf的编码方式: ...

  4. WCF服务的创建和发布到IIS

    一. WCF服务的创建 有两种创建方式: 1.WCF服务库 2.WCF服务应用程序 如下图所示: 这里选择WCF服务库.注意事项: 1.WCF服务库是一个类库项目,这里选择.net 3.5版本(版本高 ...

  5. console中应用MFC类的方法

    1.添加#include <afx.h>或者<afxwin.h> 这时会报错1>c:\program files\microsoft visual studio 8\vc ...

  6. WPF之监听组合键事件

    private void KeyDown(object sender, KeyEventArgs e) { if (e.Key== Key.Up && Keyboard.Modifie ...

  7. POJ3080——Blue Jeans(暴力+字符串匹配)

    Blue Jeans DescriptionThe Genographic Project is a research partnership between IBM and The National ...

  8. JMS基础(1)

    1.  消息中间件: 将信息以消息的形式,从一个应用程序传送到另一个或多个应用程序. 主要特点: (1) 消息异步接收: 消息发送者不需要等待消息接收者的响应 (2) 消息可靠接收: 确保消息在中间件 ...

  9. 你想建设一个能承受500万PV/每天的网站吗?

    (如果感觉有帮助,请帮忙点推荐,添加关注,谢谢!你的支持是我不断更新文章的动力.本博客会逐步推出一系列的关于大型网站架构.分布式应用.设计模式.架构模式等方面的系列文章) 你想建设一个能承受500万P ...

  10. jquery uploadify上传文件插件导致浏览器崩溃问题解决方法

    自谷歌浏览器更新到(版本39.0.2171.99 )后,访问上传文件界面浏览器就崩溃了,而其他的浏览器不会出现问题. 出现这种问题的原因就是谷歌浏览器缓存问题,但将访问该jsp页面路径添加上时间戳后无 ...