zoukankan      html  css  js  c++  java
  • 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;  
        }  
    }
  • 相关阅读:
    XML基础(二)
    XML基础(一)
    转载:数据库应用开发工具Toad使用笔记
    Oracle数据库获取一行记录中某几个字段的最大值/最小值函数
    设置Android让EditText不自动获取焦点
    android TextView selector点击样式改变
    Jenkins build java app under redhat
    Yum
    git windows
    jenkins redhat installation using war file .
  • 原文地址:https://www.cnblogs.com/eustoma/p/4358008.html
Copyright © 2011-2022 走看看