一、相关概念
1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形 (ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
5.paint 画笔
二、Bitmap
1、从资源中获取Bitmap
Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon);
2、Bitmap → byte[]
public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); }
3、byte[] → Bitmap
public Bitmap Bytes2Bimap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } }
4、Bitmap缩放
public static Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; }
5、将Drawable转化为Bitmap
public static Bitmap drawableToBitmap(Drawable drawable) { // 取 drawable 的长宽 int w = drawable.getIntrinsicWidth(); int h = drawable.getIntrinsicHeight(); // 取 drawable 的颜色格式 Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565; // 建立对应 bitmap Bitmap bitmap = Bitmap.createBitmap(w, h, config); // 建立对应 bitmap 的画布 Canvas canvas = new Canvas(bitmap); drawable.setBounds(0, 0, w, h); // 把 drawable 内容画到画布中 drawable.draw(canvas); return bitmap; }
6、获得圆角图片
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect rect = new Rect(0, 0, w, h); final RectF rectF = new RectF(rect); paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundPx, roundPx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; }
7、获得带倒影的图片
public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { final int reflectionGap = 4; int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w, h / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(bitmap, 0, 0, null); Paint deafalutPaint = new Paint(); canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint); canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP); paint.setShader(shader); // Set the Transfer mode to be porter duff and destination in paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // Draw a rectangle using the paint with our linear gradient canvas.drawRect(0, h, w, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; }
三、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); }
转载至 http://dyh7077063.iteye.com/blog/970672
发表评论
-
2012/10/29
2012-10-29 18:34 0实现了 图片上下左右滑动 放大放小 显示姓名 及 单击 双击 ... -
10.25
2012-10-25 13:52 0因为自定义 长按控件后, 图片放大放小功能无法实现 所以我把它 ... -
10.23
2012-10-23 23:48 0if (allFilesIsNotDir) { // ... -
主要类
2012-10-19 10:02 0package com.picture; import ja ... -
上下滑动 左右切换 图片放大放小
2012-10-19 08:34 0大概思路已经实现了 你自己改改不太合理的地方把. 最下面那个1 ... -
2012/10/16 左右滑动,放大放小显示姓名
2012-10-16 16:49 0左右滑动,放大放小显示姓名 下午 16:46 -
2012/10/16 左右滑动,放大放小
2012-10-16 08:57 0早上8:53 -
遍历图片
2012-10-14 23:10 0package com.mp; import java. ... -
android
2012-10-11 19:01 0//通过url 获取图片 public static b ... -
Android Log详解
2012-10-08 15:53 0android.util.Log常用的方法有以下5个:Log. ... -
android ui 事件处理
2012-09-14 10:33 0view类包含了一大堆命名类似on<..>List ... -
绘图
2012-08-23 15:15 724首先,如何获取 res 中的资源 数据包package ... -
android Paint属性
2012-08-23 14:07 925/** * Paint类介绍 ... -
http请求方式
2012-08-21 16:12 0/** * * @param url 发送 ... -
sqlite 入门
2012-09-13 13:42 9001.将数据库文件存放在attets下的schema中 ... -
sqlite
2012-08-21 15:15 0要在Android系统中操作SQLite数据库,是通过Andr ... -
反编译教程
2012-08-21 14:06 1073一、反编译Apk得到Java源代码首先要下载两个工具:dex2 ... -
将apk安装到模拟器中
2012-08-21 13:49 8191. 将apk 放在E:\android-sdk-window ...
相关推荐
android 画图 bitmap drawable canvas paint
文档中描述了Drawable Bitmap 几种转换方法供初学者使用。方法都是经过实践验证的。
很多开发者表示,不知道Android的Drawable和Bitmap之间如何相关转换。下面Android123给大家两种比较简单高效的方法。 一、Bitmap转Drawable 代码如下: Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd...
主要介绍了Android Bitmap和Drawable的对比的相关资料,需要的朋友可以参考下
Android Drawable、Bitmap、byte、灰度 转换
Android Drawable和Bitmap的转换实例详解 通常我们需要通过代码去设置图片,就需要设置图片Bitmap和Drawable的转换,下面整理了几种方式 一、Bitmap转Drawable Bitmap bm=xxx; //xxx根据你的情况获取 ...
android中的drawable集合,包括Bitmap、shape、Layer、Transition等等
Drawable Bitmap之间的转化Drawable Bitmap之间的转化
读取本地drawable中较大的资源图片 从文件得到BitMap 从数组得到Bitmap 从流中得到Bitmap 图片透明度处理 获取源图片的BITMAP,压缩,本地图片 bitmap转byte[] 获取缩略图 保存图片 光晕效果 获取bitmap的字节大小 ...
Android Drawable、Bitmap、byte、灰度 之间的转换
很多网友刚刚开始学习Android平台,对于Drawable、Bitmap、Canvas和Paint它们之间的概念不是很清楚,其实它们除了Drawable外早在Sun的J2ME中就已经出现了,但是在Android平台中,Bitmap、Canvas相关的都有所变化。...
详细的介绍了android_Drawable、Bitmap、byte[]之间的转换 在android开发中非常实用
一般在android程序中把图片文件放在res/drawable目录下就可以通过R.drawable.id来使用,但在存储卡中的图片怎样引用呢?下面通过实现这个功能来介绍Bitmap的用法。 程序如下: import java.io.File; import android...
2、从资源中获取Bitmap Resources res=getResources(); Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic); 3、Bitmap → byte[] private byte[] Bitmap2Bytes(Bitmap bm){ ByteArrayOutputStream baos...
本类库可以对ImageView,View Background,Drawable和Bitmap添加风格与设置亮度、对比度
该文档教我们如何在android保持中保持bitmap数据以及读取的过程
android MitMap的放大,缩小,倒影,将drawable转换为BitMap等等