一、相关概念

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());
}
 

最新文章

  1. csipsimple 出现单通情况
  2. VB默认属性、动态数组、Range对象的默认属性的一点不成熟的想法
  3. Nginx限速遇到的问题
  4. 项目管理工具~SVN
  5. 单线程vs多线程
  6. Learning c section 1
  7. Jquery 鼠标事件解析
  8. Android/iOS微信6.3.5同时发布更新 支持群视频聊天、群公告
  9. 记linux下使用create_ap 创建热点失败及解决(涉及rfkill)
  10. Cache
  11. install gcc under suse
  12. Linux内核系列—C语言中内嵌汇编 asm __volatile__,asm__volatile_【转】
  13. iOS - instancetype
  14. Swift_3_功能
  15. Java框架spring 学习笔记(九):Spring的bean管理(@Required、@Component、@Autowired、@Resource注解)
  16. HDU 1212 Big Number(C++ 大数取模)(java 大数类运用)
  17. H5学习的例子
  18. 根据条件返回相应值 decode(条件,值1,翻译值1,值2,翻译值2,...值n,翻译值n,缺省值)
  19. js 之 this的用法
  20. addEvent兼容版

热门文章

  1. CentOS安装crontab及使用方法
  2. XSD - &lt;schema&gt; 元素
  3. ural 1221
  4. Akka官方文档翻译:Cluster Specification
  5. Samza文档翻译 : Concepts
  6. unity3d与eclipse集成开发android应用
  7. 【leetcode】4Sum(middle)
  8. struts2 标签库 介绍
  9. HDU2110+母函数
  10. Android中如何取消调转界面后EditText默认获取聚焦问题