Android中Bitmap和Drawable
来源:互联网 发布:js ip地址正则表达式 编辑:程序博客网 时间:2024/05/22 14:31
一、相关概念
1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
二、Bitmap
1、从资源中获取Bitmap
2、Bitmap → byte[]
3、byte[] → Bitmap
public Bitmap Bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}}
4、Bitmap缩放
5、将Drawable转化为Bitmap
6、获得圆角图片
7、获得带倒影的图片
三、Drawable
1、Bitmap转换成Drawable
2、Drawable缩放
- 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);
- }
Drawable to Bitmap 应该这么写
- * Drawable 转 bitmap
- * @param drawable
- * @return
- */
- public static Bitmap drawable2Bitmap(Drawable drawable){
- if(drawable instanceof BitmapDrawable){
- return ((BitmapDrawable)drawable).getBitmap() ;
- }else if(drawable instanceof NinePatchDrawable){
- 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);
- drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
- drawable.getIntrinsicHeight());
- drawable.draw(canvas);
- return bitmap;
- }else{
- return null ;
- }
- }
http://dyh7077063.iteye.com/blog/970672
0 0
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- Android中Bitmap和Drawable
- 迷惑:数据库和VFP的区别
- 设计模式总结之没有结束的结尾
- boss来学校演讲,希望不久的将来演讲的是我!
- js原生支持checkbox框半选状态
- 2013趋势科技暑假夏令营面试
- Android中Bitmap和Drawable
- 2014/5/28几点思考
- [Maven]install the jar file into local repository manualy
- 爪哇基础
- android camera(四):camera 驱动 GT2005
- 让你们见识我交神一般的搜索引擎
- arm-linux-ld命令
- tomcat部署web项目的3中方法
- struts2回顾之一:ModelDriven机制中的refreshModelBeforeResult属性