美文网首页android 技术知识
Android Bitmap的基本知识(从资源文件中获取以及与S

Android Bitmap的基本知识(从资源文件中获取以及与S

作者: 追梦小乐 | 来源:发表于2017-04-17 20:56 被阅读58次

    本文转载于
    http://blog.csdn.net/u013096866/article/details/44516163
    http://blog.csdn.net/csh159/article/details/8835477
    http://blog.csdn.net/hezhipin610039/article/details/7899248/

    1、获取资源文件 r.drawable中的图片转换为drawable、bitmap的4种方法
    下面代码来源于:http://blog.csdn.net/u013096866/article/details/44516163
    1)、
    Resources resources = mContext.getResources();
    Drawable drawable = resources.getDrawable(R.drawable.a);

    2)、
    Resources r = this.getContext().getResources();
    Inputstream is = r.openRawResource(R.drawable.my_background_image);
    BitmapDrawable bmpDraw = new BitmapDrawable(is);
    Bitmap bmp = bmpDraw.getBitmap();

    3)、
    Bitmap bmp=BitmapFactory.decodeResource(r, R.drawable.icon);
    Bitmap newb = Bitmap.createBitmap( 300, 300, Config.ARGB_8888 );

    4)、
    InputStream is = getResources().openRawResource(R.drawable.icon);
    Bitmap mBitmap = BitmapFactory.decodeStream(is);

    2、Bitmap与string的互转
    下面代码来源于:http://blog.csdn.net/csh159/article/details/8835477
    /**
    * 图片转成string
    *
    * @param bitmap
    * @return
    */
    public static String convertIconToString(Bitmap bitmap)
    {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
    bitmap.compress(CompressFormat.PNG, 100, baos);
    byte[] appicon = baos.toByteArray();// 转为byte数组
    return Base64.encodeToString(appicon, Base64.DEFAULT);

        }  
      
        /**  
         * string转成bitmap  
         *   
         * @param st  
         */  
        public static Bitmap convertStringToIcon(String st)  
        {  
            // OutputStream out;  
            Bitmap bitmap = null;  
            try  
            {  
                // out = new FileOutputStream("/sdcard/aa.jpg");  
                byte[] bitmapArray;  
                bitmapArray = Base64.decode(st, Base64.DEFAULT);  
                bitmap =  
                        BitmapFactory.decodeByteArray(bitmapArray, 0,  
                                bitmapArray.length);  
                // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);  
                return bitmap;  
            }  
            catch (Exception e)  
            {  
                return null;  
            }  
        }  
    

    3、 Bitmap和Drawable相互转换的方法
    下面代码来源于:http://blog.csdn.net/hezhipin610039/article/details/7899248/
    1)、Bitmap---->Drawable
    Drawable drawable =new BitmapDrawable(bmp);

    2)、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;
    }

    3)、Bitmap → byte[]
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
    baos.toByteArray();

    4)、 byte[] → Bitmap
    private Bitmap Bytes2Bimap(byte[] b){
    if(b.length!=0){
    return BitmapFactory.decodeByteArray(b, 0, b.length);
    }
    else {
    return null;
    }
    }

    相关文章

      网友评论

        本文标题:Android Bitmap的基本知识(从资源文件中获取以及与S

        本文链接:https://www.haomeiwen.com/subject/xbhxzttx.html