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

          常用鏈接

          統計

          IT技術鏈接

          保險相關

          友情鏈接

          基金知識

          生活相關

          最新評論

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

          android在處理一寫圖片資源的時候,會進行一些類型的轉換,現在有空整理一下:

          1、Drawable → Bitmap 的簡單方法
          ((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   的簡單方法
          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 鴻雁 閱讀(2248) 評論(0)  編輯  收藏 所屬分類: IT技術相關

          主站蜘蛛池模板: 右玉县| 喜德县| 灵石县| 吉首市| 乐亭县| 温泉县| 满洲里市| 蓬溪县| 新竹市| 广平县| 原平市| 安丘市| 诸城市| 信丰县| 乌海市| 马公市| 团风县| 都昌县| 河曲县| 东乡族自治县| 南丹县| 余干县| 安新县| 永德县| 锡林郭勒盟| 永泰县| 定西市| 西峡县| 白水县| 广灵县| 梓潼县| 措勤县| 石家庄市| 三门峡市| 黔江区| 谢通门县| 怀柔区| 通榆县| 辽阳县| 闽侯县| 抚顺县|