The important thing in life is to have a great aim , and the determination

          常用鏈接

          統(tǒng)計(jì)

          IT技術(shù)鏈接

          保險(xiǎn)相關(guān)

          友情鏈接

          基金知識(shí)

          生活相關(guān)

          最新評(píng)論

          Drawable、Bitmap、byte[]之間的轉(zhuǎn)換 (android轉(zhuǎn))

          android在處理一寫圖片資源的時(shí)候,會(huì)進(jìn)行一些類型的轉(zhuǎn)換,現(xiàn)在有空整理一下:

          1、Drawable → Bitmap 的簡(jiǎn)單方法
          ((BitmapDrawable)res.getDrawable(R.drawable.youricon)).getBitmap();


          2、Drawable → Bitmap
          Java代碼
          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→Drawable   的簡(jiǎn)單方法
          BitmapDrawable bitmapDrawable = (BitmapDrawable)bitmap;     
          Drawable drawable = (Drawable)bitmapDrawable;     
             
             
          Bitmap bitmap = new Bitmap (...);     
          Drawable drawable = new BitmapDrawable(bitmap);   


          3、從資源中獲取Bitmap
          Java代碼
          Resources res=getResources();  
            
          Bitmap bmp=BitmapFactory.decodeResource(res, R.drawable.pic);  





          4、Bitmap → byte[]
          Java代碼
          private byte[] Bitmap2Bytes(Bitmap bm){  
              ByteArrayOutputStream baos = new ByteArrayOutputStream();    
              bm.compress(Bitmap.CompressFormat.PNG, 100, baos);    
              return baos.toByteArray();  
          }  


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

          posted on 2011-02-17 09:35 鴻雁 閱讀(2242) 評(píng)論(0)  編輯  收藏 所屬分類: IT技術(shù)相關(guān)

          主站蜘蛛池模板: 大洼县| 尉氏县| 澄迈县| 渑池县| 西充县| 开鲁县| 津市市| 洛南县| 明溪县| 黔江区| 铁岭市| 青浦区| 沙河市| 哈巴河县| 印江| 晋中市| 宁晋县| 杂多县| 加查县| 肥城市| 旅游| 罗江县| 含山县| 金塔县| 明光市| 泗阳县| 调兵山市| 尼玛县| 塘沽区| 昆山市| 越西县| 安塞县| 车险| 读书| 龙井市| 江陵县| 古蔺县| 台山市| 华宁县| 班戈县| 石嘴山市|