美文网首页
Android Drawable、Bitmap、byte[]之间

Android Drawable、Bitmap、byte[]之间

作者: WotYang | 来源:发表于2018-05-21 12:00 被阅读0次
    1. byte[ ] 转 Bitmap
    BitmapFactory.decodeByteArray(byte, 0, byte.length)
    
    2. Bitmap 转 byte[ ]
    ByteArrayOutputStream baos = new ByteArrayOutputStream();  
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);  
    
    3. Bitmap 转 Drawable
    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.pic); 
    
    4. Drawable 转 Bitmap
    if (drawable instanceof BitmapDrawable) {
           return ((BitmapDrawable) drawable).getBitmap();
    } else if (drawable instanceof NinePatchDrawable) {
          Bitmap bitmap = Bitmap
             .createBitmap(
             drawable.getIntrinsicWidth(),
             drawable.getIntrinsicHeight(),
             drawable.getOpacity() != PixelFormat.OPAQUE ? 
             Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
          Canvas canvas = new Canvas(bitmap);
          drawable.setBounds(0, 0, drawable.getIntrinsicWidth(),
          drawable.getIntrinsicHeight());
          drawable.draw(canvas);
          return bitmap;
    } else {
          return null;
    }
    

    相关文章

      网友评论

          本文标题:Android Drawable、Bitmap、byte[]之间

          本文链接:https://www.haomeiwen.com/subject/xcsfjftx.html