zoukankan      html  css  js  c++  java
  • Android中Bitmap、Drawable、byte[]转换

    原文地址:   http://www.cnblogs.com/fbsk/archive/2011/10/11/2207530.html

    1.Drawable—>Bitmap

    Resources res=getResources();
    Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.sample_0);

    2.Bitmap---->Drawable

    Drawable drawable =new BitmapDrawable(bmp);

    另外在网上搜了些,也不知到底谁是原创的

    1、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;

    }

    2、从资源中获取Bitmap
    Resources res=getResources();

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

    3、Bitmap → byte[]
    private byte[] Bitmap2Bytes(Bitmap bm){

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

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

        return baos.toByteArray();   }

    4、 byte[] → Bitmap

       private Bitmap Bytes2Bimap(byte[] b){

                        if(b.length!=0){

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

                        }

                        else {

                                return null;

                        }

              }

  • 相关阅读:
    在上传文件时限制上传文件的大小,并捕捉超过文件大小限制的
    javascript 获取标签具体位置
    终端服务器超出了最大允许连接数
    常用SQL语句书写技巧
    Javascript实现截图功能(代码)
    JavaScript实现类,有多种方法。
    DBCC CHECKDB 数据库或表修复
    Lucene的例子
    控制同一exe程序打开多次
    IIS6 MMC检测到此管理单元发生一个错误,建议您关闭并重新启动mmc
  • 原文地址:https://www.cnblogs.com/yangzhenyu/p/2214582.html
Copyright © 2011-2022 走看看