Android繪制進階之三:在位圖上(Bitmap)繪制位圖(Bitmap)

1,點擊按鈕,指定action和uri,設定結果碼(ResultCode).到達手機默認相冊的Gallery.

 clip_image001

 

代碼如下:
 
1. public void onClick(View v) {
2.         // TODO Auto-generated method stub
3.         Intent intent = new Intent(Intent.ACTION_PICK,
4.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//啟動照片Gallery
5.         startActivityForResult(intent, 0);
6.     }
2,選擇圖片,返回所選照片uri信息
代碼如下:
 
1. @Override
2.     protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
3.         // TODO Auto-generated method stub
4.         super.onActivityResult(requestCode, resultCode, intent);
5.         
6.         if (resultCode == RESULT_OK) {//操作成功
7.             Uri imgFileUri = intent.getData();//獲得所選照片的信息
 
3,處理圖片,設定合適的尺寸(正好適應屏幕)
設定BitmapFactory.Options 參數inJustDecodeBounds = true,即不創建bitmap,但可獲取bitmap的相關屬性。以寬高比例差距 最大者為基準。
代碼如下:
 
1. //由於返回的圖像可能太大而無法完全加載到內存中。系統有限制,需要處理。
2.             Display currentDisplay = getWindowManager().getDefaultDisplay();
3.             int defaultHeight = currentDisplay.getHeight();
4.             int defaultWidth = currentDisplay.getWidth();
5.             
6.             BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
7.             bitmapFactoryOptions.inJustDecodeBounds = true;///隻是為獲取原始圖片的尺寸,而不返回Bitmap對象
8.             //註上:If set to true, the decoder will return null (no bitmap), but the out… fields will still be set,
9.             //allowing the caller to query the bitmap without having to allocate the memory for its pixels
10.             try {
11.                 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgFileUri), null, bitmapFactoryOptions);
12.                 int outHeight = bitmapFactoryOptions.outHeight;
13.                 int outWidth = bitmapFactoryOptions.outWidth;
14.                 int heightRatio = (int) Math.ceil((float)outHeight/defaultHeight);
15.                 int widthRatio = (int) Math.ceil((float)outWidth/defaultWidth);
16.                 
17.                 if (heightRatio > 1 || widthRatio >1) {
18.                     if (heightRatio > widthRatio) {
19.                         bitmapFactoryOptions.inSampleSize = heightRatio;
20.                     } else {
21.                         bitmapFactoryOptions.inSampleSize = widthRatio;                 
22.                     }
23.                 }
4,inJustDecodeBounds = false,創建將此bitmap賦給第一個ImageView。
代碼如下:
 
1. bitmapFactoryOptions.inJustDecodeBounds = false;
2.                 bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgFileUri), null, bitmapFactoryOptions);
3.                 
4.                 mImageShow.setImageBitmap(bitmap);
5,繪制bitmap(選中的圖片),賦給第二個ImageView
代碼如下:
 
1. /*
2.                  * 在位圖上繪制位圖
3.                  */
4.                 
5.                 Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
6.                 
7.                 Canvas canvas = new Canvas(bitmapAltered);//bitmap提供瞭畫佈,隻在此提供瞭大小尺寸,偏移後並未有背景顯示出來
8.                 
9.                 
10.                 Paint paint = new Paint();
11.                 
12.                 canvas.drawBitmap(bitmap, 0, 0, paint);//繪制的圖片和之前的一模一樣
13.                 
14.                 mImageAltered.setImageBitmap(bitmapAltered);
 

 摘自  小新專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *