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;

                        }

              }

  • 相关阅读:
    AspNetCore打造一个“最安全”的api接口
    efcore分表分库原理解析
    对于经常接触的分页你确定你真的会吗
    Monitor的扩展支持string的超时锁
    Excel导出
    搭建私有Git服务器-GitLab
    C# 爬取网易Buff进行购买
    .Net Core 使用弹性和瞬态故障处理库Polly
    JS Table表格添加多选框
    JS 用户头像展示
  • 原文地址:https://www.cnblogs.com/wxmdevelop/p/6183420.html
Copyright © 2011-2022 走看看