歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android實現圖片順時逆時旋轉及拖拽顯示效果

Android實現圖片順時逆時旋轉及拖拽顯示效果

日期:2017/3/1 10:39:55   编辑:Linux編程

1、首先說一下兩個類:

Matrix

Class Overview

The Matrix class holds a 3x3 matrix for transforming coordinates. Matrix does not have a constructor, so it must be explicitly initialized using either reset() - to construct an identity matrix, or one of the set..() functions (e.g. setTranslate, setRotate, etc.).

矩陣類擁有3 x3的坐標變換矩陣。沒有一個構造函數矩陣,所以它必須顯式初始化的使用或重置()-如何構建一個矩陣,或者一個場景……()的功能(例如,setRotate setTranslate等。)

Matrix的操作,總共分為translate(平移),rotate(旋轉),scale(縮放)和skew(傾斜)四種,每一種變換在Android的API裡都提供了set,post和pre三種操作方式,除了translate,其他三種操作都可以指定中心點。set是直接設置Matrix的值,每次set一次,整個Matrix的數組都會變掉。post是後乘,當前的矩陣乘以參數給出的矩陣。可以連續多次使用post,來完成所需的整個變換。

接下來我們用到了兩個方法:

平移方法:兩個參數分別是要移到的x、y坐標

boolean postTranslate(float dx, float dy) Postconcats the matrix with the specified translation.和旋轉方法:第一個參數是旋轉多少度,正數是順時針,負數是逆時針;第二三參數是按某個點旋轉的x、y坐標;

boolean postRotate(float degrees, float px, float py) Postconcats the matrix with the specified rotation.

PointF

Class Overview

PointF holds two float coordinates

PointF有兩個浮點坐標

我們要用到該類的一個方法:設置點的x和y坐標

final void set(float x, float y) Set the point's x and y coordinates2、接下來是案例:

首先看一下效果圖:

旋轉拖拽後

布局很簡單在此不再給出!直接看java代碼:

[java]
  1. public class MovePictureActivity extends Activity implements OnClickListener {
  2. private Button button1, button2;
  3. private ImageView image;
  4. PointF startPoint = new PointF();// 有兩PointF浮坐標
  5. Matrix matrix = new Matrix();
  6. @Override
  7. public void onCreate(Bundle savedInstanceState) {
  8. super.onCreate(savedInstanceState);
  9. setContentView(R.layout.main);
  10. init();
  11. }
  12. private void init() {
  13. button1 = (Button) findViewById(R.id.button1);
  14. button2 = (Button) findViewById(R.id.button2);
  15. button1.setOnClickListener(this);
  16. button2.setOnClickListener(this);
  17. image = (ImageView) findViewById(R.id.image);
  18. image.setOnTouchListener(new ImageViewOnTouchListener());// 為image綁上觸摸事件監聽
  19. }
  20. @Override
  21. public void onClick(View v) {
  22. switch (v.getId()) {
  23. case R.id.button1:
  24. matrix.postRotate(90, image.getWidth() / 2, image.getHeight() / 2);// 順時針旋轉90度,並且以image.getWidth()/2、image.getHeight()/2為中心旋轉;
  25. break;
  26. case R.id.button2:
  27. matrix.postRotate(-90, image.getWidth() / 2, image.getHeight() / 2);// 逆時針旋轉90度
  28. break;
  29. }
  30. image.setImageMatrix(matrix);
  31. }
  32. private class ImageViewOnTouchListener implements OnTouchListener {
  33. @Override
  34. public boolean onTouch(View v, MotionEvent event) {
  35. switch (event.getAction() & MotionEvent.ACTION_MASK) {// 這裡取出來的是event.getAction()返回的值的低八位,MotionEvent.ACTION_MASK是255,
  36. case MotionEvent.ACTION_DOWN:
  37. startPoint.set(event.getX(), event.getY());
  38. break;
  39. case MotionEvent.ACTION_MOVE:// 移動過程,該事件會不斷被觸發
  40. float dx = event.getX() - startPoint.x;
  41. float dy = event.getY() - startPoint.y;
  42. matrix.postTranslate(dx, dy);
  43. startPoint.set(event.getX(), event.getY());
  44. break;
  45. }
  46. image.setImageMatrix(matrix);
  47. return true;
  48. }
  49. }
  50. }
為image綁定監聽事件,
[java]
  1. image.setOnTouchListener(new ImageViewOnTouchListener());// 為image綁上觸摸事件監聽

View.OnTouchListener

該接口:

Interface definition for a callback to be invoked when a touch event is dispatched to this view. The callback will be invoked before the touch event is given to the view.

接口定義作為一個回調函數被調用時被派遣去觸摸事件這一觀點。回調函數被調用之前會觸摸事件是給你盡情的觀看。

Copyright © Linux教程網 All Rights Reserved