当前位置:操作系统 > 安卓/Android >>

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.&

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,