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技術相關

          主站蜘蛛池模板: 黄山市| 玛纳斯县| 陇西县| 石家庄市| 临夏县| 桃园市| 贵德县| 邢台县| 张家口市| 颍上县| 海林市| 松溪县| 南郑县| 平武县| 攀枝花市| 定安县| 民勤县| 米脂县| 白玉县| 安远县| 乳源| 独山县| 南安市| 五常市| 昆明市| 全椒县| 个旧市| 宝应县| 根河市| 合水县| 东乌珠穆沁旗| 新竹县| 诸城市| 全州县| 新巴尔虎左旗| 合水县| 宕昌县| 巴林左旗| 金乡县| 孝感市| 怀集县|