import android.app.Activity;   


1. import android.graphics.Bitmap;
2. import android.graphics.BitmapFactory;
3. import android.graphics.Matrix;
4. import android.graphics.drawable.BitmapDrawable;
5. import android.os.Bundle;
6. import android.view.ViewGroup.LayoutParams;
7. import android.widget.ImageView;
8. import android.widget.LinearLayout;
9. import android.widget.ImageView.ScaleType;
10.
11. public class bitmaptest extends Activity {
12. public void onCreate(Bundle icicle) {
13. super.onCreate(icicle);
14. "eoeAndroid教程: 缩放和旋转图片 -by:IceskYsl");
15. new LinearLayout(this);
16.
17. // 加载需要操作的图片,这里是eoeAndroid的logo图片
18. Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),
19. R.drawable.eoe_android);
20.
21. //获取这个图片的宽和高
22. int width = bitmapOrg.getWidth();
23. int height = bitmapOrg.getHeight();
24.
25. //定义预转换成的图片的宽度和高度
26. int newWidth = 200;
27. int newHeight = 200;
28.
29. //计算缩放率,新尺寸除原始尺寸
30. float scaleWidth = ((float) newWidth) / width;
31. float scaleHeight = ((float) newHeight) / height;
32.
33. // 创建操作图片用的matrix对象
34. new Matrix();
35.
36. // 缩放图片动作
37. matrix.postScale(scaleWidth, scaleHeight);
38.
39. //旋转图片 动作
40. 45);
41.
42. // 创建新的图片
43. 0, 0,
44. true);
45.
46. //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中
47. new BitmapDrawable(resizedBitmap);
48.
49. //创建一个ImageView
50. new ImageView(this);
51.
52. // 设置ImageView的图片为上面转换的图片
53. imageView.setImageDrawable(bmd);
54.
55. //将图片居中显示
56. imageView.setScaleType(ScaleType.CENTER);
57.
58. //将ImageView添加到布局模板中
59. linLayout.addView(imageView,
60. new LinearLayout.LayoutParams(
61. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT
62. )
63. );
64.
65. // 设置为本activity的模板
66. setContentView(linLayout);
67. }
68. }