一、相关概念

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

二、Bitmap

1、从资源中获取Bitmap

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

2、Bitmap → byte[]

  1. 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. 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缩放

  1. 1 public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {
    2 int w = bitmap.getWidth();
    3 int h = bitmap.getHeight();
    4 Matrix matrix = new Matrix();
    5 float scaleWidth = ((float) width / w);
    6 float scaleHeight = ((float) height / h);
    7 matrix.postScale(scaleWidth, scaleHeight);
    8 Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    9 return newbmp;
    10 }

5、将Drawable转化为Bitmap

  1. 1 public static Bitmap drawableToBitmap(Drawable drawable) {
    2 // 取 drawable 的长宽
    3 int w = drawable.getIntrinsicWidth();
    4 int h = drawable.getIntrinsicHeight();
    5
    6 // 取 drawable 的颜色格式
    7 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
    8 : Bitmap.Config.RGB_565;
    9 // 建立对应 bitmap
    10 Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    11 // 建立对应 bitmap 的画布
    12 Canvas canvas = new Canvas(bitmap);
    13 drawable.setBounds(0, 0, w, h);
    14 // 把 drawable 内容画到画布中
    15 drawable.draw(canvas);
    16 return bitmap;
    17 }

6、获得圆角图片

  1. 1 public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    2 int w = bitmap.getWidth();
    3 int h = bitmap.getHeight();
    4 Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    5 Canvas canvas = new Canvas(output);
    6 final int color = 0xff424242;
    7 final Paint paint = new Paint();
    8 final Rect rect = new Rect(0, 0, w, h);
    9 final RectF rectF = new RectF(rect);
    10 paint.setAntiAlias(true);
    11 canvas.drawARGB(0, 0, 0, 0);
    12 paint.setColor(color);
    13 canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    14 paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    15 canvas.drawBitmap(bitmap, rect, rect, paint);
    16
    17 return output;
    18 }

7、获得带倒影的图片

  1. 1 public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    2 final int reflectionGap = 4;
    3 int w = bitmap.getWidth();
    4 int h = bitmap.getHeight();
    5
    6 Matrix matrix = new Matrix();
    7 matrix.preScale(1, -1);
    8
    9 Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
    10 h / 2, matrix, false);
    11
    12 Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
    13 Config.ARGB_8888);
    14
    15 Canvas canvas = new Canvas(bitmapWithReflection);
    16 canvas.drawBitmap(bitmap, 0, 0, null);
    17 Paint deafalutPaint = new Paint();
    18 canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
    19
    20 canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null);
    21
    22 Paint paint = new Paint();
    23 LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
    24 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
    25 0x00ffffff, TileMode.CLAMP);
    26 paint.setShader(shader);
    27 // Set the Transfer mode to be porter duff and destination in
    28 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    29 // Draw a rectangle using the paint with our linear gradient
    30 canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
    31 + reflectionGap, paint);
    32
    33 return bitmapWithReflection;
    34 }

三、Drawable

1、Bitmap转换成Drawable

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

2、Drawable缩放

  1. 1 public static Drawable zoomDrawable(Drawable drawable, int w, int h) {
    2 int width = drawable.getIntrinsicWidth();
    3 int height = drawable.getIntrinsicHeight();
    4 // drawable转换成bitmap
    5 Bitmap oldbmp = drawableToBitmap(drawable);
    6 // 创建操作图片用的Matrix对象
    7 Matrix matrix = new Matrix();
    8 // 计算缩放比例
    9 float sx = ((float) w / width);
    10 float sy = ((float) h / height);
    11 // 设置缩放比例
    12 matrix.postScale(sx, sy);
    13 // 建立新的bitmap,其内容是对原bitmap的缩放后的图
    14 Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
    15 matrix, true);
    16 return new BitmapDrawable(newbmp);
    17 }

android开发之Bitmap 、byte[] 、 Drawable之间的相互转换的更多相关文章

  1. byte[],bitmap,drawable之间的相互转换

    Byte[]转Bitmap BitmapFactory.decodeByteArray(data, 0, data.length); Bitmap转Byte[] ByteArrayOutputStre ...

  2. android开发之bitmap使用

    bitmap是android中重要的图像处理工具类,通过bitmap可以对图像进行剪切.旋转.缩放等操作,同时还可以指定格式和压缩质量保存图像文件. 一.拿到一个Bitmap对象 查看源码我们知道,B ...

  3. Android开发之Bitmap.Config.RGB_565

    在学习xutils框架的时候,看到sample代码中有一行这样的代码: bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565); Bi ...

  4. 【转】Android开发之Bitmap的内存优化详解

    本文来源:转载自: http://mobile.51cto.com/abased-410796.htm 在Android应用里,最耗费内存的就是图片资源.而且在Android系统中,读取位图Bitma ...

  5. Android开发之Bitmap二次采样

    Bitmap二次采样,听着好像是一个高大上的事,其实也就那么回事,今天我们就来看看Bitmap的二次采样问题. 1.为什么要二次采样 OK,那么首先我要 解决的一个问题就是为什么我们要二次采样? 不知 ...

  6. Android开发之Bitmap的高效加载

    BitmapFactory类提供了四类方法:decodeFile, decodeResource, decodeStream和decodeByteArray 分别用于支持从文件系统,资源,输入流以及字 ...

  7. Android图片二进制与Bitmap、Drawable之间的转换

    Android图片二进制与Bitmap.Drawable之间的转换 Java代码  public byte[] getBitmapByte(Bitmap bitmap){      ByteArray ...

  8. Android开发之InstanceState详解

    Android开发之InstanceState详解   本文介绍Android中关于Activity的两个神秘方法:onSaveInstanceState() 和 onRestoreInstanceS ...

  9. 【Android UI】Android开发之View的几种布局方式及实践

    引言 通过前面两篇: Android 开发之旅:又见Hello World! Android 开发之旅:深入分析布局文件&又是“Hello World!” 我们对Android应用程序运行原理 ...

随机推荐

  1. 在fc6上搭tftpd

    公司的开发环境依然停留在fc6上,,,,对..很旧,旧到想死. 我在没有进一步熟悉ubuntu的基础上,为了保持ABI一致. 只能依旧在FC6 上开发. 可是现在发现开发完成,我要在fc6上文件到wi ...

  2. java ScriptEngine 使用

    Java SE 6最引人注目的新功能之一就是内嵌了脚本支持.在默认情况下,Java SE 6只支持JavaScript,但这并不以为着Java SE 6只能支持JavaScript.在Java SE ...

  3. 【网络与系统安全】关于SSL/TSL协议的分析

    前言 TSL协议的前身是由网景(Netscape)公司于1994年研发的安全套接字(Secure Socket Layer)协议.它建立在TCP协议栈的传输层,用于保护面向连接的TCP通信.实际TSL ...

  4. php7下 xhprof安装与使用

    需要测试下 代码的性能,使用了 xhprof + xhgui 1. 下载xhprof, 这里下载吧 :https://github.com/longxinH/xhprof.git 2, 安装 cd x ...

  5. Write 语句

    ABAP Write 语句 转载▼http://blog.sina.com.cn/s/blog_5ccd375b0100ghhi.html   1.Write 叙述 ABAP/4 用来在屏幕上输出数据 ...

  6. Web Service概念辨析

    Web Service包含两个概念. 其一是Web Service标准体系,由SOAP.WSDL.UDDI三要素组成,是平台和语言无关的.在这个概念里和WCF做比较是错误的,因为前者是行业标准,后者是 ...

  7. ARDUINO使用GPRS发送GPS数据到OneNet测试

    功能: 测试把固定的GPS数据发送到OneNet平台 调试途中碰到的问题 ARDUINO不支持sprintf的double打印,只能转换为char字符串然后再%s打印 #include <Tim ...

  8. QT设置textEdit光标到末尾

    //移动光标到末尾 QTextCursor cursor = ui->receiveTextEdit->textCursor(); cursor.movePosition(QTextCur ...

  9. 关于mosquitto_internal.h:40:25:#include <uuid/uuid.h> 致命错误的解决

    一.安装mosquitto1.4的时候使用make的时候报以下错误: mosquitto_internal.h:40:25: 致命错误:openssl/ssl.h:没有那个文件或目录 #include ...

  10. ubuntu 安装 phpstorm

    phpstorm是用JAVA开发的,所以在安装之前需要先安装jdksudo apt-get install default-jdk从官网上下载phpstorm 的linux版本 http://www. ...