且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Android中Bitmap,byte[],Drawable相互转化

更新时间:2022-09-22 09:57:07

一、相关概念

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

二、Bitmap

1、从资源中获取Bitmap

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


2、Bitmap → byte[]

      [] Bitmap2Bytes(Bitmap bm) {
         ByteArrayOutputStream baos =  ByteArrayOutputStream();
         bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
          baos.toByteArray();
     }


3、byte[] → Bitmap

 

      Bitmap Bytes2Bimap([] b) {
          (b.length != 0) {
              BitmapFactory.decodeByteArray(b, 0, b.length);
         }  {
              ;
         }
     }

 

4、Bitmap缩放

       Bitmap zoomBitmap(Bitmap bitmap,  width,  height) {
          w = bitmap.getWidth();
          h = bitmap.getHeight();
         Matrix matrix =  Matrix();
          scaleWidth = (() width / w);
          scaleHeight = (() height / h);
         matrix.postScale(scaleWidth, scaleHeight);
         Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, );
          newbmp;
     }

 

5、将Drawable转化为Bitmap

   Bitmap drawableToBitmap(Drawable drawable) {
                   w = drawable.getIntrinsicWidth();
          h = drawable.getIntrinsicHeight();
 
                  Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                 : Bitmap.Config.RGB_565;
                  Bitmap bitmap = Bitmap.createBitmap(w, h, config);
                  Canvas canvas =  Canvas(bitmap);
         drawable.setBounds(0, 0, w, h);
                  drawable.draw(canvas);
          bitmap;
     }

 

6、获得圆角图片 

       Bitmap getRoundedCornerBitmap(Bitmap bitmap,  roundPx) {
          w = bitmap.getWidth();
          h = bitmap.getHeight();
         Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
         Canvas canvas =  Canvas(output);
           color = 0xff424242;
          Paint paint =  Paint();
          Rect rect =  Rect(0, 0, w, h);
          RectF rectF =  RectF(rect);
         paint.setAntiAlias();
         canvas.drawARGB(0, 0, 0, 0);
         paint.setColor(color);
         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
         paint.setXfermode( PorterDuffXfermode(Mode.SRC_IN));
         canvas.drawBitmap(bitmap, rect, rect, paint);
 
          output;
     }

 

7、获得带倒影的图片


       Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
           reflectionGap = 4;
          w = bitmap.getWidth();
          h = bitmap.getHeight();
 
         Matrix matrix =  Matrix();
         matrix.preScale(1, -1);
 
         Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, h / 2, w,
                 h / 2, matrix, );
 
         Bitmap bitmapWithReflection = Bitmap.createBitmap(w, (h + h / 2),
                 Config.ARGB_8888);
 
         Canvas canvas =  Canvas(bitmapWithReflection);
         canvas.drawBitmap(bitmap, 0, 0, );
         Paint deafalutPaint =  Paint();
         canvas.drawRect(0, h, w, h + reflectionGap, deafalutPaint);
 
         canvas.drawBitmap(reflectionImage, 0, h + reflectionGap, );
 
         Paint paint =  Paint();
         LinearGradient shader =  LinearGradient(0, bitmap.getHeight(), 0,
                 bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
                 0x00ffffff, TileMode.CLAMP);
         paint.setShader(shader);
                  paint.setXfermode( PorterDuffXfermode(Mode.DST_IN));
                  canvas.drawRect(0, h, w, bitmapWithReflection.getHeight()
                 + reflectionGap, paint);
 
          bitmapWithReflection;
     }

 

 三、Drawable

1、Bitmap转换成Drawable

 Bitmap bm=xxx;  BitmapDrawable bd=  BitmapDrawable(getResource(), bm); 
 因为BtimapDrawable是Drawable的子类,最终直接使用bd对象即可。

2、Drawable缩放


       Drawable zoomDrawable(Drawable drawable,  w,  h) {
          width = drawable.getIntrinsicWidth();
          height = drawable.getIntrinsicHeight();
                  Bitmap oldbmp = drawableToBitmap(drawable);
                  Matrix matrix =  Matrix();
                   sx = (() w / width);
          sy = (() h / height);
                  matrix.postScale(sx, sy);
                  Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height,
                 matrix, );
           BitmapDrawable(newbmp);
     }

本文转自dyh7077063的博客http://dyh7077063.iteye.com/blog/970672













本文转自wauoen51CTO博客,原文链接: http://blog.51cto.com/7183397/1604493,如需转载请自行联系原作者