• Drawable、Bitmap、byte[]之间的转换


    android在处理一写图片资源的时候,会进行一些类型的转换:

    1 Drawable → Bitmap 的简单方法

    1 ((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();

    2 Drawable → Bitmap

    1 public static Bitmap drawableToBitmap(Drawable drawable) {  
    2           
    3     Bitmap bitmap = Bitmap.createBitmap(rawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);  
    4     Canvas canvas = new Canvas(bitmap);  
    5     //canvas.setBitmap(bitmap);  
    6     drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());  
    7     drawable.draw(canvas);  
    8     return bitmap;  
    9 }  

    3 Bitmap→Drawable的简单方法

    1 BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;     
    2 Drawable drawable = (Drawable)bitmapDrawable;     
    3     
    4 Bitmap bitmap = new Bitmap (...);     
    5 Drawable drawable = new BitmapDrawable(bitmap); 

    4 从资源中获取Bitmap

    1 Resources res = getResources();  
    2 Bitmap bmp = BitmapFactory.decodeResource(res, R.drawable.pic);

    5 Bitmap → byte[]

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

    6 byte[] → Bitmap

    private Bitmap Bytes2Bimap(byte[] b) {  
        if (b.length!=0) {  
            return BitmapFactory.decodeByteArray(b, 0, b.length);  
        } else {  
            return null;  
        }  
    }
  • 相关阅读:
    rabbitmqctl常用命令-3
    Count and Say
    Spiral Matrix II
    Minimum Path Sum
    Plus One
    Rotate Image
    Permutations
    Search a 2D Matrix
    Binary Tree Level Order Traversal II
    Binary Tree Level Order Traversal
  • 原文地址:https://www.cnblogs.com/eustoma/p/4358008.html
Copyright © 2020-2023  润新知