首页 > 其他分享 >Android Bitmap和Drawable

Android Bitmap和Drawable

时间:2022-12-09 17:02:33浏览次数:45  
标签:canvas int bitmap drawable new Android Bitmap Drawable

一、相关概念1、Drawable就是一个可画的对象,其可能是一张位图(BitmapDrawable),也可能是一个图形(ShapeDrawable),还有可能是一个图层(LayerDrawable),我们根据画图的需求,创建相应的可画对象2、Canvas画布,绘图的目的区域,用于绘图3、Bitmap位图,用于图的处理4、Matrix矩阵二、Bitmap1、从资源中获取BitmapJava代码 Resources res = getResources(); Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon); Resources res = getResources();Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.icon); 2、Bitmap → byte[]Java代码 public byte[] Bitmap2Bytes(Bitmap bm) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bm.compress(Bitmap.CompressFormat.PNG, 100, baos); return baos.toByteArray(); } public byte[] Bitmap2Bytes(Bitmap bm) {ByteArrayOutputStream baos = new ByteArrayOutputStream();bm.compress(Bitmap.CompressFormat.PNG, 100, baos);return baos.toByteArray();} 3、byte[] → Bitmap Java代码 public Bitmap Bytes2Bimap(byte[] b) { if (b.length != 0) { return BitmapFactory.decodeByteArray(b, 0, b.length); } else { return null; } } public Bitmap Bytes2Bimap(byte[] b) {if (b.length != 0) {return BitmapFactory.decodeByteArray(b, 0, b.length);} else {return null;}}4、Bitmap缩放Java代码 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; } 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转化为BitmapJava代码 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; } 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;// 建立对应 bitmapBitmap 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、获得圆角图片 Java代码 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; } 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、获得带倒影的图片Java代码 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; } 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 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;} 三、Drawable1、Bitmap转换成DrawableJava代码 Bitmap bm=xxx; //xxx根据你的情况获取 BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。 Bitmap bm=xxx; //xxx根据你的情况获取BitmapDrawable bd= new BitmapDrawable(getResource(), bm); 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。2、Drawable缩放Java代码 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); }


标签:canvas,int,bitmap,drawable,new,Android,Bitmap,Drawable
From: https://blog.51cto.com/u_15907753/5926285

相关文章

  • Android类库打包方法探究
    众所周知,Android应用使用ADT打包成apk,apk中包含了运行程序所需要的一切,包括:class、asset、res、AndroidManifest.xml等。而对于类库项目(libraryproject),ADT生成的jar......
  • android打开系统联系人界面
    在android应用程序的开发中,经常要实现的一个功能是调用系统自带的联系人界面,做到选择人之后,取得相应的名称和号码,并返回。android2.0前后实现方式上也有一定的差别:主要是2.0......
  • android静默安装探讨(申请加精)
     1)在网上搜寻该问题的解决方法,且查阅android开发文档,没有发现可以实现该功能的显示API调用,网络上很多人请教同样的问题,但都没有能够实现解答;说是android为了用户的安全,......
  • android之屏幕方向改变以及onConfigurationChanged事件
    注意:onConfigurationChanged事件并不是只有屏幕方向改变才可以触发,其他的一些系统设置改变也可以触发,比如打开或者隐藏键盘。当我们的屏幕方向发生改变时,就可以触发onConfig......
  • Android Studio工程目录结构分析(一)
    AndroidStudio工程目录我们这里以一个简单的HelloWorld工程来分析:1、.gradle和.idea     这两个目录下放置的都是AndroidStudio自动生成的一些文......
  • Android内存优化(使用SparseArray和ArrayMap代替HashMap)
    在Android开发时,我们使用的大部分都是Java的api,比如HashMap这个api,使用率非常高,但是对于Android这种对内存非常敏感的移动平台,很多时候使用一些java的api并不能达到更好的性......
  • Android内存优化——常见内存泄露及优化方案
    如果一个无用对象(不需要再使用的对象)仍然被其他对象持有引用,造成该对象无法被系统回收,以致该对象在堆中所占用的内存单元无法被释放而造成内存空间浪费,这中情况就是内存泄露......
  • RCAR M3N Android P 编译
    1.环境搭建1.1 Nomodulenamed'Crypto'pip3installcrypto -ihttps://pypi.tuna.tsinghua.edu.cn/simple/  1.2 /bin/sh:1:lz4c:notfoundsudoapt-g......
  • Android 10机型安装Xposed框架教程VCAM
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......
  • Android 动画实现 从基础到自定义
    OverridetheentrypointofanimageIntroducedinGitLabandGitLabRunner9.4.Readmoreaboutthe extendedconfigurationoptions.Beforeexplainingtheav......