Android繪制進階之五:位圖的處理

圖片的處理包括以下:
1, 縮放
2, 傾斜
3, 旋轉
4, 縮放+
5, 平移
6, 鏡像
代碼如下:
 
1. package com.mike.activity;
2. 
3. import java.io.FileNotFoundException;
4. 
5. import android.app.Activity;
6. import android.content.Intent;
7. import android.graphics.Bitmap;
8. import android.graphics.BitmapFactory;
9. import android.graphics.BitmapFactory.Options;
10. import android.graphics.Canvas;
11. import android.graphics.Color;
12. import android.graphics.Matrix;
13. import android.graphics.Paint;
14. import android.net.Uri;
15. import android.os.Bundle;
16. import android.util.Log;
17. import android.view.Display;
18. import android.view.View;
19. import android.view.View.OnClickListener;
20. import android.widget.Button;
21. import android.widget.ImageView;
22. 
23. public class ImageDemoActivity extends Activity implements OnClickListener {
24.     /** Called when the activity is first created. */
25. 
26.     public static String TAG = "IMAGE";
27.     public static int REQUEST_CODE = 0;
28.     private ImageView mImageShow;
29.     private ImageView mImageAltered;
30. 
31.     @Override
32.     public void onCreate(Bundle savedInstanceState) {
33.         super.onCreate(savedInstanceState);
34.         setContentView(R.layout.main);
35. 
36.         Button imageSelectBtn = (Button) findViewById(R.id.imageSelectBtn);
37.         mImageShow = (ImageView) findViewById(R.id.imageShow);
38.         mImageAltered = (ImageView) findViewById(R.id.imageAltered);
39.         imageSelectBtn.setOnClickListener(this);
40.     }
41. 
42.     public void onClick(View v) {
43.         // TODO Auto-generated method stub
44.         Intent intent = new Intent(Intent.ACTION_PICK,
45.                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 啟動照片Gallery
46.         startActivityForResult(intent, 0);
47.     }
48. 
49.     @Override
50.     protected void onActivityResult(int requestCode, int resultCode,
51.             Intent intent) {
52.         // TODO Auto-generated method stub
53.         super.onActivityResult(requestCode, resultCode, intent);
54. 
55.         if (resultCode == RESULT_OK) {// 操作成功
56.             Uri imgFileUri = intent.getData();// 獲得所選照片的信息
57.             Log.d(TAG, "imgFileUri is :" + imgFileUri);
58.             // 由於返回的圖像可能太大而無法完全加載到內存中。系統有限制,需要處理。
59.             Display currentDisplay = getWindowManager().getDefaultDisplay();
60.             int defaultHeight = currentDisplay.getHeight();
61.             int defaultWidth = currentDisplay.getWidth();
62. 
63.             BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options();
64.             bitmapFactoryOptions.inJustDecodeBounds = false;// /隻是為獲取原始圖片的尺寸,而不返回Bitmap對象
65.             // 註上:If set to true, the decoder will return null (no bitmap), but
66.             // the out… fields will still be set,
67.             // allowing the caller to query the bitmap without having to
68.             // allocate the memory for its pixels
69.             try {
70.                 Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
71.                         .openInputStream(imgFileUri), null,
72.                         bitmapFactoryOptions);
73.                 int outHeight = bitmapFactoryOptions.outHeight;
74.                 int outWidth = bitmapFactoryOptions.outWidth;
75.                 int heightRatio = (int) Math.ceil((float) outHeight
76.                         / defaultHeight);
77.                 int widthRatio = (int) Math.ceil((float) outWidth
78.                         / defaultWidth);
79. 
80.                 if (heightRatio > 1 || widthRatio > 1) {
81.                     if (heightRatio > widthRatio) {
82.                         bitmapFactoryOptions.inSampleSize = heightRatio;
83.                     } else {
84.                         bitmapFactoryOptions.inSampleSize = widthRatio;
85.                     }
86.                 }
87. 
88.                 bitmapFactoryOptions.inJustDecodeBounds = false;
89.                 bitmap = BitmapFactory.decodeStream(getContentResolver()
90.                         .openInputStream(imgFileUri), null,
91.                         bitmapFactoryOptions);
92. 
93.                 mImageShow.setImageBitmap(bitmap);
94. 
95.                 // /*
96.                 // * 在位圖上繪制位圖
97.                 // */
98.                 //
99.                 // Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(),
100.                 // bitmap.getHeight(), bitmap.getConfig());
101.                 //
102.                 // Canvas canvas = new
103.                 // Canvas(bitmapAltered);//bitmap提供瞭畫佈,隻在此提供瞭大小尺寸,偏移後並未有背景顯示出來
104.                 //
105.                 //
106.                 // Paint paint = new Paint();
107.                 //
108.                 // canvas.drawBitmap(bitmap, 0, 0, paint);//繪制的圖片和之前的一模一樣
109.                 //
110.                 // mImageAltered.setImageBitmap(bitmapAltered);
111. 
112.                 /*
113.                  * 使用矩陣
114.                  */
115. 
116.                 Bitmap bitmapAltered = Bitmap.createBitmap(bitmap.getWidth(),
117.                         bitmap.getHeight(), bitmap.getConfig());// 缺點是限定畫佈大小,處理後的圖片可能被截斷,可引入矩陣構造方法,動態設定畫佈大小 www.aiwalls.com
118.                 Canvas canvas = new Canvas(bitmapAltered);// bitmap提供瞭畫佈,隻在此提供瞭大小尺寸,偏移後並未有背景顯示出來,
119.                 Paint paint = new Paint();
120. 
121.                 Matrix matrix = new Matrix();
122. 
123.                 // 1:縮放
124.                 // matrix.setValues(new float[] {//涉及線性代數的知識,可以翻翻相乘的基礎知識
125.                 // 1,0,0,//算出x的值
126.                 // 0,1,0,//算出y的值
127.                 // 0,0,1//二維圖用不到
128.                 // });
129. 
130.                 // 2:傾斜
131.                 // matrix.setValues(new float[] {//涉及線性代數的知識,可以翻翻相乘的基礎知識
132.                 // 1,.5f,0,//算出x的值,x,y值相關
133.                 // 0,1,0,//算出y的值
134.                 // 0,0,1//二維圖用不到
135.                 // });
136. 
137.                 // 3:旋轉
138.                 // matrix.setRotate(45);//設置旋轉角度,(0,0)點,順時針旋轉45度
139. 
140.                 // 4,縮放plus:使用Matrix類方法//需要修改畫佈寬度>1.5倍* 原始寬度
141.                 // matrix.setScale(1.5f, 1);
142. 
143.                 // 5,平移
144.                 // matrix.setTranslate(10, 0);
145. 
146.                 // 6,鏡像
147.                 // eg1:x軸鏡像
148.                 // matrix.setScale(-1, 1);//向左繪制,原始圖片沿y軸對稱
149.                 // matrix.postTranslate(bitmap.getWidth(), 0);//註意是post即後移動
150.                 // eg2:y軸鏡像
151.                 matrix.setScale(1, -1);// 向上繪制,原始圖片沿x軸對稱,圖像在第四象限
152.                 matrix.postTranslate(0, bitmap.getHeight());// 註意是post即後移動
153.                 
154.                 
155.                 //~optimize~因為指定畫佈大小bitmapAltered,所以處理後的圖片可能被截斷,可以通過矩陣動態改變bitmapAltered大小
156.                 //eg: matrix.setRotate(degrees, px, py)
157.                 //bitmapAltered = Bitmap.createBitmap(source, x, y, width, height, m, filter);//顯然矩陣影響其大小
158. 
159.                 //
160. 
161.                 // 註:for more details :Wikipedia Transformation Matrix
162.                 // http://en.wikipedia.org/wiki/Transformation_matrix
163. 
164.                 canvas.drawBitmap(bitmap, matrix, paint);// 繪制的圖片和之前的一模一樣
165. 
166.                 mImageAltered.setImageBitmap(bitmapAltered);
167. 
168.             } catch (FileNotFoundException e) {
169.                 // TODO Auto-generated catch block
170.                 e.printStackTrace();
171.             }
172. 
173.         }
174.     }
175. }

 
摘自 小新專欄

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。