zoukankan      html  css  js  c++  java
  • Android中 Bitmap和Drawable相互转换的方法

    1.Drawable—>Bitmap

    Resources res=getResources();

    Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);
    Resources res=getResources();
    private byte[] Bitmap2Bytes(Bitmap bm){

    2.Bitmap---->Drawable

    Drawable drawable =new BitmapDrawable(bmp);


    3、Drawable → Bitmap

    public static Bitmap drawableToBitmap(Drawable drawable) {

           

            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);

            //canvas.setBitmap(bitmap);

            drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());

            drawable.draw(canvas);

            return bitmap;

    }

    4、从资源中获取Bitmap

    Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);

    5、Bitmap → byte[]

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);

        return baos.toByteArray();   }

    6、 byte[] → Bitmap

       private Bitmap Bytes2Bimap(byte[] b){

                        if(b.length!=0){

                                return BitmapFactory.decodeByteArray(b, 0, b.length);

                        }

                        else {

                                return null;

                        }

              }

  • 相关阅读:
    mysql binlog参数设置
    poj 2774 最长公共子--弦hash或后缀数组或后缀自己主动机
    Base64编码和解码算法
    怎样给你的Android 安装文件(APK)减肥
    JAXB 注解
    编程获取linux的CPU使用的内存使用情况
    那么温暖http合约,入门。
    什么是关账?
    经营活动现金净流量与总股本之比和经营活动现金净流量与净资产之比
    P2P风险淮安样本:5000万连锁漩涡牵出银行内案
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/6183420.html
Copyright © 2011-2022 走看看