版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、本文章来给大家介绍Android中Bitmap和Drawable用法详解,有需要了解的同学可进入参考。一、相关概念1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable,也可能是一个图形(ShapeDrawable,还有可能是一个图层(LayerDrawable,我们根据画图的需求,创建相应的可画对象2、Canvas画布,绘图的目的区域,用于绘图3、Bitmap位图,用于图的处理4、Matrix矩阵二、Bitmap1、从资源中获取BitmapResources res = getResources(;2、Bitmap byte代码如下public byte Bi
2、tmap2Bytes(Bitmap bm ByteArrayOutputStream baos = new ByteArrayOutputStream(;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;Bitmap缩放代码如下public static Bitmap zoomBitmap(Bitmap bitmap, i
3、nt 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
4、、将Drawable转化为Bitmap代码如下public static Bitmap drawableToBitmap(Drawable drawable / 取 drawable 的长宽int w = drawable.getIntrinsicWidth(;int h = drawable.getIntrinsicHeight(;/ 取 drawable 的颜色格式/ 建立对应 bitmapBitmap bitmap = Bitmap.createBitmap(w, h, config;/ 建立对应 bitmap 的画布Canvas canvas = new Canvas(bitmap;d
5、rawable.setBounds(0, 0, w, h;/ 把 drawable 内容画到画布中drawable.draw(canvas;return bitmap;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
6、;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(M
7、ode.SRC_IN;canvas.drawBitmap(bitmap, rect, rect, paint;return output;6、获得圆角图片代码如下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
8、= 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 + reflect
9、ionGap, 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 Tra
10、nsfer mode to be porter duff and destination inpaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN;/ Draw a rectangle using the paint with our linear gradientcanvas.drawRect(0, h, w, bitmapWithReflection.getHeight(+ reflectionGap, paint;return bitmapWithReflection;三、Drawable代码如下Bitmap bm=xxx; /xxx根
11、据你的情况获取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转换成bitmapBitmap oldbmp = drawableToBitmap(drawable;/ 创建操作图片用的Matrix对象Matrix matrix = new Matrix(;/ 计算缩放比例float sx = (float w / width;float sy = (float h / h
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 高中化学2026教资面试试讲全真模拟试卷及答案
- 房地产设计课件
- 初中英语2026教资面试易错题题库及解析
- 高中政治2026教资面试历年真题题库及答案
- 输血时护理核对程序及流程
- 房地产测绘课件
- 分层圆模板课件
- 防踩塌培训课件
- 八年级科学能量转换基础巩固教案
- 房产税教学课件
- 2026年译林版五年级英语上册期中测试卷(含答案)-中等难度第10套-含答案解析
- 2026年《口腔颌面部外伤诊疗指南》
- 临床护理实践指南手术室器械传递
- 医院手术室无影灯安装方案
- 2026年新修订《中国共产党发展党员工作细则》逐条全文精讲(权威完整版)
- 墙面抹灰空鼓开裂修补方案
- 2026秋新教材粤教粤科版小学科学五年级上册(全册)教学设计(附目录p194)
- 林木采伐专项施工方案
- 康复护理评定的常用方法
- 2026年价格鉴证师《法学基础知识与价格政策法规》真题及答案解析
- 2026新教材语文 统编版语文五年级上册 第六单元 《口语交际:父母之爱》教学课件
评论
0/150
提交评论