多點觸摸技術
阿新 • • 發佈:2017-05-31
view 技術 actionbar hist cnblogs turn true ren oat
1 //多點觸摸 放大,縮小 2 public class MainActivity extends ActionBarActivity { 3 4 @Override 5 protected void onCreate(Bundle savedInstanceState) { 6 super.onCreate(savedInstanceState); 7 setContentView(R.layout.fragment_main); 8 9 } 10 11 @Override 12 publicboolean onTouchEvent(MotionEvent event) { 13 // TODO Auto-generated method stub 14 // 兩點觸摸 15 if (event.getPointerCount() == 2) { 16 if (event.getAction() == MotionEvent.ACTION_MOVE) { 17 int historySize = event.getHistorySize(); 18 if(historySize == 0) { 19 return true; 20 } 21 // 獲取第一個手指當前的縱坐標 22 float currentY1 = event.getY(0); 23 // 獲取第一個手指當前最新的歷史的縱坐標 24 float historyY1 = event.getHistoricalY(0, historySize - 1); 25 // 獲取第二個手指當前的縱坐標26 float currentY2 = event.getY(2); 27 // 獲取第二個手指當前最新的歷史的縱坐標 28 float historyY2 = event.getHistoricalY(1, historySize - 1); 29 // 兩手指當前縱坐標的距離 30 float distance = Math.abs(currentY1 - currentY2); 31 // 兩手指最新的歷史縱坐標的距離 32 float historyDistance = Math.abs(historyY1 - historyY2); 33 34 if (distance > historyDistance) { 35 Log.i("-====>", "放大"); 36 } else if (distance < historyDistance) { 37 Log.i("-====>", "縮小"); 38 } else { 39 Log.i("-====>", "移動"); 40 } 41 } 42 } 43 return super.onTouchEvent(event); 44 } 45 46 }
多點觸摸技術