zoukankan      html  css  js  c++  java
  • 转换Bitmap对象

    把一个View转换bitmap对象

    1 public static Bitmap getViewBitmap(View view) {
    2         view.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
    3                 View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
    4         view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
    5         view.buildDrawingCache();
    6         Bitmap bitmap = view.getDrawingCache();
    7         return bitmap;
    8     }

    根据已有的Drawable创建一个新的Bitmap:

     1 private Bitmap bitmap;
     2 private void drawableToBitamp(Drawable drawable)
     3     {
     4         int w = drawable.getIntrinsicWidth();
     5         int h = drawable.getIntrinsicHeight();
     6         System.out.println("Drawable转Bitmap");
     7         Bitmap.Config config = 
     8                 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
     9                         : Bitmap.Config.RGB_565;
    10         bitmap = Bitmap.createBitmap(w,h,config);
    11         //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
    12         Canvas canvas = new Canvas(bitmap);   
    13         drawable.setBounds(0, 0, w, h);   
    14         drawable.draw(canvas);
    15     }

    直接从现有的Drawable中取出Bitmap:

    1 private Bitmap bitmap;
    2 
    3 private void drawableToBitamp(Drawable drawable)
    4     {
    5         BitmapDrawable bd = (BitmapDrawable) drawable;
    6         bitmap = bd.getBitmap();
    7     }

    Bitmap转Byte

    1 private byte[] Bitmap2Bytes(Bitmap bm){
    2 
    3     ByteArrayOutputStream baos = new ByteArrayOutputStream();
    4 
    5     bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    6 
    7     return baos.toByteArray();  
    8  }

    byte[] 转Bitmap:

     1 private Bitmap Bytes2Bimap(byte[] b){
     2 
     3                     if(b.length!=0){
     4 
     5                             return BitmapFactory.decodeByteArray(b, 0, b.length);
     6 
     7                     }
     8 
     9                     else {
    10 
    11                             return null;
    12 
    13                     }
    14 
    15           }
  • 相关阅读:
    P1092 虫食算
    P1040 加分二叉树
    cfER76 abcd
    cf599 div2 a/b1/b2/c
    AtCoder Contest 144 DE
    Round G 2019
    luogu3084 Photo 单调队列优化DP
    luogu4234 最小差值生成树
    luogu1373 小a和uim之大逃离
    luogu1070 道路游戏 单调队列
  • 原文地址:https://www.cnblogs.com/yimi-yangguang/p/6143488.html
Copyright © 2011-2022 走看看