搜索
您的当前位置:首页图片的旋转和缩放android高级

图片的旋转和缩放android高级

来源:乌哈旅游


view plaincopy to clipboardprint?

1. package com.test.activity;

2. import android.app.Activity;

3. import android.graphics.Bitmap;

4. import android.graphics.BitmapFactory;

5. import android.graphics.Matrix;

6. import android.graphics.drawable.BitmapDrawable;

7. import android.os.Bundle;

8. import android.view.ViewGroup.LayoutParams;

9. import android.widget.ImageView;

10. import android.widget.LinearLayout;

11. import android.widget.ImageView.ScaleType;

12. public class MainActivity extends Activity {

13. public void onCreate(Bundle icicle) {

14. super.onCreate(icicle);

15. LinearLayout linLayout = new LinearLayout(this);

16. // 加载需要操作的图片,这里是eoeAndroid的logo图片

17. Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),

18. R.drawable.sss);

19. //获取这个图片的宽和高

20. int width = bitmapOrg.getWidth();

21. int height = bitmapOrg.getHeight();

22. //定义预转换成的图片的宽度和高度

23. int newWidth = 200;

24. int newHeight = 200;

25. //计算缩放率,新尺寸除原始尺寸

26. float scaleWidth = ((float) newWidth) / width;

27. float scaleHeight = ((float) newHeight) / height;

28. // 创建操作图片用的matrix对象

29. Matrix matrix = new Matrix();

30. // 缩放图片动作

31. matrix.postScale(scaleWidth, scaleHeight);

32. //旋转图片 动作

33. matrix.postRotate(45);

34. // 创建新的图片

35. Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0,

36. width, height, matrix, true);

37. //将上面创建的Bitmap转换成Drawable对象,使得其可以使用在ImageView, ImageButton中

38. BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

39. //创建一个ImageView

40. ImageView imageView = new ImageView(this);

41. // 设置ImageView的图片为上面转换的图片

42. imageView.setImageDrawable(bmd);

43. //将图片居中显示

44. imageView.setScaleType(ScaleType.CENTER);

45. //将ImageView添加到布局模板中

46. linLayout.addView(imageView,

47. new LinearLayout.LayoutParams(

48. LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT

49. )

50. );

51. // 设置为本activity的模板

52. setContentView(linLayout);

53. }

54. }

因篇幅问题不能全部显示,请点此查看更多更全内容

Top