Android中Bitmap和Drawable 总结
一、相关概念
1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象
2、Canvas画布,绘图的目的区域,用于绘图
3、Bitmap位图,用于图的处理
4、Matrix矩阵
二、Bitmap
1、从资源中获取Bitmap
- Resourcesres=getResources();
- Bitmapbmp=BitmapFactory.decodeResource(res,R.drawable.icon);
2、Bitmap → byte[]
- publicbyte[]Bitmap2Bytes(Bitmapbm){
- ByteArrayOutputStreambaos=newByteArrayOutputStream();
- bm.compress(Bitmap.CompressFormat.PNG,100,baos);
- returnbaos.toByteArray();
- }
3、byte[] → Bitmap
- publicBitmapBytes2Bimap(byte[]b){
- if(b.length!=0){
- returnBitmapFactory.decodeByteArray(b,0,b.length);
- }else{
- returnnull;
- }
- }
4、Bitmap缩放
- publicstaticBitmapzoomBitmap(Bitmapbitmap,intwidth,intheight){
- intw=bitmap.getWidth();
- inth=bitmap.getHeight();
- Matrixmatrix=newMatrix();
- floatscaleWidth=((float)width/w);
- floatscaleHeight=((float)height/h);
- matrix.postScale(scaleWidth,scaleHeight);
- Bitmapnewbmp=Bitmap.createBitmap(bitmap,0,0,w,h,matrix,true);
- returnnewbmp;
- }
5、将Drawable转化为Bitmap
- publicstaticBitmapdrawableToBitmap(Drawabledrawable){
- //取drawable的长宽
- intw=drawable.getIntrinsicWidth();
- inth=drawable.getIntrinsicHeight();
- //取drawable的颜色格式
- Bitmap.Configconfig=drawable.getOpacity()!=PixelFormat.OPAQUE?Bitmap.Config.ARGB_8888
- :Bitmap.Config.RGB_565;
- //建立对应bitmap
- Bitmapbitmap=Bitmap.createBitmap(w,h,config);
- //建立对应bitmap的画布
- Canvascanvas=newCanvas(bitmap);
- drawable.setBounds(0,0,w,h);
- //把drawable内容画到画布中
- drawable.draw(canvas);
- returnbitmap;
- }
6、获得圆角图片
- publicstaticBitmapgetRoundedCornerBitmap(Bitmapbitmap,floatroundPx){
- intw=bitmap.getWidth();
- inth=bitmap.getHeight();
- Bitmapoutput=Bitmap.createBitmap(w,h,Config.ARGB_8888);
- Canvascanvas=newCanvas(output);
- finalintcolor=0xff424242;
- finalPaintpaint=newPaint();
- finalRectrect=newRect(0,0,w,h);
- finalRectFrectF=newRectF(rect);
- paint.setAntiAlias(true);
- canvas.drawARGB(0,0,0,0);
- paint.setColor(color);
- canvas.drawRoundRect(rectF,roundPx,roundPx,paint);
- paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));
- canvas.drawBitmap(bitmap,rect,rect,paint);
- returnoutput;
- }
7、获得带倒影的图片
- publicstaticBitmapcreateReflectionImageWithOrigin(Bitmapbitmap){
- finalintreflectionGap=4;
- intw=bitmap.getWidth();
- inth=bitmap.getHeight();
- Matrixmatrix=newMatrix();
- matrix.preScale(1,-1);
- BitmapreflectionImage=Bitmap.createBitmap(bitmap,0,h/2,w,
- h/2,matrix,false);
- BitmapbitmapWithReflection=Bitmap.createBitmap(w,(h+h/2),
- Config.ARGB_8888);
- Canvascanvas=newCanvas(bitmapWithReflection);
- canvas.drawBitmap(bitmap,0,0,null);
- PaintdeafalutPaint=newPaint();
- canvas.drawRect(0,h,w,h+reflectionGap,deafalutPaint);
- canvas.drawBitmap(reflectionImage,0,h+reflectionGap,null);
- Paintpaint=newPaint();
- LinearGradientshader=newLinearGradient(0,bitmap.getHeight(),0,
- bitmapWithReflection.getHeight()+reflectionGap,0x70ffffff,
- 0x00ffffff,TileMode.CLAMP);
- paint.setShader(shader);
- //SettheTransfermodetobeporterduffanddestinationin
- paint.setXfermode(newPorterDuffXfermode(Mode.DST_IN));
- //Drawarectangleusingthepaintwithourlineargradient
- canvas.drawRect(0,h,w,bitmapWithReflection.getHeight()
- +reflectionGap,paint);
- returnbitmapWithReflection;
- }
三、Drawable
1、Bitmap转换成Drawable
- Bitmapbm=xxx;//xxx根据你的情况获取
- BitmapDrawablebd=newBitmapDrawable(getResource(),bm);
- 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。
2、Drawable缩放
- publicstaticDrawablezoomDrawable(Drawabledrawable,intw,inth){
- intwidth=drawable.getIntrinsicWidth();
- intheight=drawable.getIntrinsicHeight();
- //drawable转换成bitmap
- Bitmapoldbmp=drawableToBitmap(drawable);
- //创建操作图片用的Matrix对象
- Matrixmatrix=newMatrix();
- //计算缩放比例
- floatsx=((float)w/width);
- floatsy=((float)h/height);
- //设置缩放比例
- matrix.postScale(sx,sy);
- //建立新的bitmap,其内容是对原bitmap的缩放后的图
- Bitmapnewbmp=Bitmap.createBitmap(oldbmp,0,0,width,height,
- matrix,true);
- returnnewBitmapDrawable(newbmp);
- }
Android中Bitmap和Drawable 总结相关推荐
- Android中Bitmap和Drawable
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Android中Bitmap和Drawable详解
一.相关概念 1.Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable) ...
- Android中Bitmap的分析与使用
下面总结一下Bitmap的分析与使用 Bitmap的创建 创建Bitmap的时候,Java不提供new Bitmap()的形式去创建,而是通过BitmapFactory中的静态方法去创建,如:Bitm ...
- 你都知道么?Android中21种drawable标签大全
前言 我们在drawable目录下可以创建很多自定义的资源,其中用的最多的应该就是selector和shape.目前在Android中有21种drawable标签,了解和利用这些标签对我们的开发有很大 ...
- Android中应用程序drawable图片资源占用内存的统计
在实际项目中,我们可能需要统计我们应用程序中drawable下的图片或文本在都载入的情况,最多会占用多少内存 ,以对其做定性分析. 关于Android中图片占用内存的计算,请参照<Android ...
- Android 中的各种 Drawable 你都知道了吗?
文章目录 Drawable BitmapDrawable NinePatchDrawable ShapeDrawable LayerDrawable StateListDrawable LevelLi ...
- android中bitmap压缩的几种方法详解
Android图片压缩(质量压缩和尺寸压缩) 在做项目中遇到一个头疼的问题,读取本地图片时,由于图片太大,奔溃,对于我这种456M内存的破手机哪里受得了几M的照片 我要总结的有这么几点: 1.尺寸压缩 ...
- Android中Bitmap缓存池
http://mobile.51cto.com/android-386446.htm 本文介绍了如何使用缓存来提高UI的载入输入和滑动的流畅性.使用内存缓存.使用磁盘缓存.处理配置改变事件等方法将会有 ...
- android中layout、drawable及styles的xml文件加载探索
最近搞一个SDK项目,既然提供给用户那么就不能太复杂.而android如果不用xml就会很复杂,比如布局,那么怎么办? 关于布局别人提供了一个方法,xml解析.具体做法将xml文件以数据文件的形式放在 ...
最新文章
- 基于 OpenCV 的图像阴影去除
- css属性cusor
- was更换java版本,WAS如何更换JDK
- 从零开始入门 K8s:深入剖析 Linux 容器
- oracle io lost,磁盘IO故障
- java 防止用户重复登录_JAVA 如何避免用户的重复登录
- Android四大组件---Activity
- 一稿过的Banner模板素材!做电商的你一定需要
- 让mysql timeStamp类型支持默认值0000-00-00 00:00:00
- mysql text 性能_MySQL - text 性能优化--记录一
- 用VMware克隆CentOS 6.4后HWaddr和UUID的设置
- Android加载服务器的静态图片文件
- Markdown部分语法使用
- 大菠萝已经完全变味了
- (5)完整的Django框架学习之自定义过滤器及标签
- 【计算机网络】第六话·数据的传输方式(上)
- 如何看pg数据库版本号_pg版本是什么-和pg版本相关的问题-阿里云开发者社区
- php短网址程序,博天短网址生成程序 PHP源码
- 内网渗透测试 MSF搭建socks代理
- APPLE Bonjour服务导致公司网络核心Cisco 6509崩溃的案例
热门文章
- 0 full gc时cpu idle_结合GC日志讲讲CMS垃圾收集器
- android进程守护 失效,保持Service不被Kill掉的方法--双Service守护 Android实现双进程守护 1...
- python技术分享_分享几个python的使用技巧
- HTML示例07---图像与超链接
- 集成unittest与html测试报告
- 自动化用例设计原则+web自动化框架
- 自学了三个月的软件测试,从小白到自动化测试工程师,我是如何从零拿到大厂offer的
- 面试必考题:基本95%的面试都会被问到的?
- 通过 YAPI + Python Flask 实现接口自动化测试
- 测试开发新手:从0到1开展性能测试必备的性能测试要点!