Android獲得圖片資源的三種方式

jopen 12年前發布 | 42K 次閱讀 Android Android開發 移動開發

、        使用BitmapFactory解析圖片

            // --> 使用BitmapFactory解析圖片
           public void myUseBitmapFactory(Canvas canvas){
           // 定義畫筆
              Paint paint = new Paint();
           // 獲取資源流
              Resources rec = getResources();
              InputStream in = rec.openRawResource(R.drawable.haha);
           // 設置圖片
              Bitmap bitmap =BitmapFactory.decodeStream(in);
           // 繪制圖片
              canvas.drawBitmap(bitmap, 0,20, paint);         
           }

 

、        使用BitmapDrawable解析圖片

       // --> 使用BitmapDrawable解析圖片
           public void myUseBitmapDrawable(Canvas canvas){
           // 定義畫筆
              Paint paint = new Paint();
           // 獲得資源
              Resources rec = getResources();
           // BitmapDrawable
              BitmapDrawable bitmapDrawable = (BitmapDrawable) rec.getDrawable(R.drawable.haha);
           // 得到Bitmap
              Bitmap bitmap = bitmapDrawable.getBitmap();
           // 在畫板上繪制圖片
              canvas.drawBitmap(bitmap, 20,120,paint);
           }

 

三、        使用InputStream和BitmapDrawable繪制

       // --> 使用InputStream和BitmapDrawable解析圖片
           public void myUseInputStreamandBitmapDrawable(Canvas canvas){
           // 定義畫筆
              Paint paint = new Paint();
           // 獲得資源
              Resources rec = getResources();
           // InputStream得到資源流
              InputStream in = rec.openRawResource(R.drawable.haha);
           // BitmapDrawable 解析數據流
              BitmapDrawable bitmapDrawable =  new BitmapDrawable(in);
           // 得到圖片
              Bitmap bitmap = bitmapDrawable.getBitmap();
           // 繪制圖片
              canvas.drawBitmap(bitmap, 100, 100,paint);
           }

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!