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]
- public class MovePictureActivity extends Activity implements OnClickListener {
- private Button button1, button2;
- private ImageView image;
- PointF startPoint = new PointF();// 有兩PointF浮坐標
- Matrix matrix = new Matrix();
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
-
- private void init() {
- button1 = (Button) findViewById(R.id.button1);
- button2 = (Button) findViewById(R.id.button2);
- button1.setOnClickListener(this);
- button2.setOnClickListener(this);
- image = (ImageView) findViewById(R.id.image);
- image.setOnTouchListener(new ImageViewOnTouchListener());// 為image綁上觸摸事件監聽
- }
-
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- case R.id.button1:
- matrix.postRotate(90, image.getWidth() / 2, image.getHeight() / 2);// 順時針旋轉90度,並且以image.getWidth()/2、image.getHeight()/2為中心旋轉;
- break;
- case R.id.button2:
- matrix.postRotate(-90, image.getWidth() / 2, image.getHeight() / 2);// 逆時針旋轉90度
- break;
- }
- image.setImageMatrix(matrix);
- }
-
- private class ImageViewOnTouchListener implements OnTouchListener {
- @Override
- public boolean onTouch(View v, MotionEvent event) {
- switch (event.getAction() & MotionEvent.ACTION_MASK) {// 這裡取出來的是event.getAction()返回的值的低八位,MotionEvent.ACTION_MASK是255,
- case MotionEvent.ACTION_DOWN:
- startPoint.set(event.getX(), event.getY());
- break;
- case MotionEvent.ACTION_MOVE:// 移動過程,該事件會不斷被觸發
- float dx = event.getX() - startPoint.x;
- float dy = event.getY() - startPoint.y;
- matrix.postTranslate(dx, dy);
- startPoint.set(event.getX(), event.getY());
- break;
- }
- image.setImageMatrix(matrix);
- return true;
- }
-
- }
- }
為image綁定監聽事件,
[java]
- 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.
接口定義作為一個回調函數被調用時被派遣去觸摸事件這一觀點。回調函數被調用之前會觸摸事件是給你盡情的觀看。