1,點擊按鈕,指定action和uri,設定結果碼(ResultCode).到達手機默認相冊的Gallery.
代碼如下:
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,
3. Intent intent) {
4. // TODO Auto-generated method stub
5. super.onActivityResult(requestCode, resultCode, intent);
6.
7. if (resultCode == RESULT_OK) {// 操作成功 www.aiwalls.com
8. 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
9. // the out… fields will still be set,
10. // allowing the caller to query the bitmap without having to
11. // allocate the memory for its pixels
12. try {
13. Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
14. .openInputStream(imgFileUri), null,
15. bitmapFactoryOptions);
16. int outHeight = bitmapFactoryOptions.outHeight;
17. int outWidth = bitmapFactoryOptions.outWidth;
18. int heightRatio = (int) Math.ceil((float) outHeight
19. / defaultHeight);
20. int widthRatio = (int) Math.ceil((float) outWidth
21. / defaultWidth);
22.
23. if (heightRatio > 1 || widthRatio > 1) {
24. if (heightRatio > widthRatio) {
25. bitmapFactoryOptions.inSampleSize = heightRatio;
26. } else {
27. bitmapFactoryOptions.inSampleSize = widthRatio;
28. }
29. }
4,inJustDecodeBounds = false,創建將此bitmap賦給第一個ImageView。
代碼如下:
1. bitmapFactoryOptions.inJustDecodeBounds = false;
2. bitmap = BitmapFactory.decodeStream(getContentResolver()
3. .openInputStream(imgFileUri), null,
4. bitmapFactoryOptions);
5.
6. mImageShow.setImageBitmap(bitmap);
5,繪制bitmap(選中的圖片),賦給第二個ImageView
a, 繪制正面Bitmap
b,在此Bitmap上繪制文本
c,創建Matrix對象,指定旋轉角度。
d,創建新的bitmap對象,即canvas即將要繪制的傾斜區域
e,繪制
代碼如下:
1. // /*
2. // * 在位圖上繪制位圖
3. // */
4. //
5.
6. Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(),
7. bitmap.getHeight(), bitmap.getConfig());
8.
9. Canvas canvas = new Canvas(bitmapAltered);// bitmap提供瞭畫佈,隻在此提供瞭大小尺寸,偏移後並未有背景顯示出來
10.
11. Paint paint = new Paint();
12.
13. canvas.drawBitmap(bitmap, 0, 0, paint);
14.
15. paint.setColor(Color.RED);
16.
17. canvas.drawText("hello", 10, 10, paint);
18.
19. //先繪制正面圖片,再旋轉
20. Matrix matrix = new Matrix();
21.
22. matrix.setRotate(45, 0, 0);
23. //創建一個傾斜的繪制區域
24. Bitmap bitmapRotated = Bitmap.createBitmap(bitmapAltered, 0, 0, bitmapAltered.getWidth(), bitmapAltered.getHeight(), matrix, false);
25. canvas.drawBitmap(bitmapAltered, matrix, paint);
26. mImageAltered.setImageBitmap(bitmapRotated);
摘自 小新專欄