Android 單指觸控拖拽,兩指觸控縮放


 1 import android.app.Activity;
 2 import android.os.Bundle;
 3 import android.util.Log;
 4 import android.view.MotionEvent;
 5 import android.view.View;
 6 import android.view.Window;
 7 import android.widget.FrameLayout;
 8 import android.widget.FrameLayout.LayoutParams;
 9 
10 public class MainActivity extends Activity {
11 
12     View view;
13     public static final int DRAG = 1;
14     public static final int SCALE = 2;
15     int mode = 1;
16     float lengthOld = 1;//初始化兩指之間原長
17     
18     //初始化圓點高寬,java代碼中操作的寬高都是像素值,dp*density轉成px  
19     int width = 40;
20     int height = 40;
21     
22     @Override
23     protected void onCreate(Bundle savedInstanceState) {
24         super.onCreate(savedInstanceState);
25         requestWindowFeature(Window.FEATURE_NO_TITLE);
26         setContentView(R.layout.activity_main);
27         Log.e("TAG", "像素密度:"+getResources().getDisplayMetrics().density);
28         view = findViewById(R.id.view);
29     }
30 
31     /**
32      * 在MainActivity中畫一個圓
33      * Activity重寫onTouchEvent,觸摸監聽
34      * getRawX(),getRawY()是獲取手機屏幕上的點到屏幕左上角頂點的距離
35      * getX(),getY()是獲取視圖坐標,子視圖在父視圖中的位置
36      */
37     @Override
38     public boolean onTouchEvent(MotionEvent event) {
39         //記錄坐標,圓點距離視圖頂點
40         int x = (int) event.getX();
41         int y = (int) event.getY();
42         
43         //多指觸控
44         //獲取事件源,並通過event.getActionMasked()加入多點觸控支持
45         switch (event.getAction() & event.getActionMasked()) {
46         case MotionEvent.ACTION_DOWN:
47             //單指移動
48             mode = DRAG;
49             break;
50         case MotionEvent.ACTION_POINTER_DOWN:
51             //多指縮放
52             Log.e("TAG", "多指移動");
53             mode = SCALE;
54             //計算兩指間開始的原長
55             lengthOld = calculation(event);
56             break;
57         case MotionEvent.ACTION_UP:
58             lengthOld = 1;//復位
59             break;
60         case MotionEvent.ACTION_MOVE:
61             if(mode == DRAG){
62                 //單指,直接設置坐標
63                 FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(width, height);
64                 params.setMargins(x, y, 0, 0);
65                 view.setLayoutParams(params);
66                 
67             }else{
68                 //兩指縮放,要計算縮放比例
69                 float scale = calculation(event) / lengthOld;
70                 width = (int) (view.getWidth() * scale);
71                 height = (int) (view.getHeight() * scale);
72                 
73                 FrameLayout.LayoutParams params = (LayoutParams) view.getLayoutParams();
74                 params.width = width;
75                 params.height = height;
76                 view.setLayoutParams(params);
77             }
78             break;
79         }
80         
81         return true;
82     }
83 
84     
85     private float calculation(MotionEvent event) {
86         
87         float x1 = event.getX();
88         float x2 = event.getX(1);
89         float y1 = event.getY();
90         float y2 = event.getY(1);
91         
92         return (float) Math.sqrt(Math.pow((x1-x2), 2)+Math.pow((y1-y2), 2));
93     }
94 }

xml中設置

 1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     android:layout_width="match_parent"
 3     android:layout_height="match_parent" >
 4 
 5     <View
 6         android:id="@+id/view"
 7         android:layout_width="20dp"
 8         android:layout_height="20dp"
 9         android:background="@drawable/oval" />
10 
11 </FrameLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM