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

          導航

          <2011年2月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272812345
          6789101112

          留言簿(7)

          隨筆分類

          隨筆檔案

          文章檔案

          相冊

          閱讀排行榜

          評論排行榜

          常用鏈接

          統計

          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();&nbsp;   
              bm.compress(Bitmap.CompressFormat.PNG, 100, baos);&nbsp;   
              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技術相關

          主站蜘蛛池模板: 依兰县| 英山县| 乌恰县| 东兰县| 富宁县| 乌兰县| 理塘县| 怀柔区| 大丰市| 普定县| 印江| 姜堰市| 云梦县| 百色市| 尖扎县| 车致| 昌吉市| 大埔县| 祥云县| 鹤山市| 株洲市| 无锡市| 太仆寺旗| 锡林浩特市| 林州市| 即墨市| 和顺县| 永泰县| 华坪县| 庐江县| 汉阴县| 宜章县| 宜兰市| 安溪县| 龙山县| 浦城县| 湖南省| 昭平县| 花垣县| 化州市| 驻马店市|