1 簡介android
Android多點觸控在本質上須要LCD驅動和程序自己設計上支持,目前市面上HTC、Motorola和Samsung等知名廠商只要使用電容屏觸控原理的手機都可以支持多點觸控Multitouch技術,對於網頁縮放、手勢操做上有更好的用戶體驗。 在Android平臺上事件均使用了MotionEvent對象方式處理,好比開始觸控時會觸發ACTION_DOWN,而移動操做時爲 ACTION_MOVE,最終放開手指時觸發ACTION_UP事件。固然還有用戶無規則的操做可能觸發ACTION_CANCEL這個動做。canvas
須要注意的是:Android的多點觸控功能須要運行在Android 2.0版本以上。ide
首先Android開發網提醒你們多點觸控須要LCD驅動和應用軟件兩個支持才能實現,因此部分比較老的,好比Android 2.0之前或在北美上市的手機可能沒法支持多點觸控在固件上,因爲Apple專利緣由在歐洲和亞太地區的Android 2.0之後的新款機型固件均已經在屏幕驅動中支持,同時模擬器也沒法實現多點觸控的測試。post
2 實現步驟測試
1)第一種狀況是直接重載Activity中的onTouchEvent方法。this
對於onTouchEvent方法的參數MotionEvent,咱們能夠詳細處理來實現對多點觸控的瞭解,好比spa
event.getAction() //獲取觸控動做好比ACTION_DOWN event.getPointerCount(); //獲取觸控點的數量,好比2則多是兩個手指同時按壓屏幕 event.getPointerId(nID); //對於每一個觸控的點的細節,咱們能夠經過一個循環執行getPointerId方法獲取索引 event.getX(nID); //獲取第nID個觸控點的x位置 event.getY(nID); //獲取第nID個點觸控的y位置 event.getPressure(nID); //LCD能夠感應出用戶的手指壓力,固然具體的級別由驅動和物理硬件決定的 event.getDownTime() //按下開始時間 event.getEventTime() // 事件結束時間 event.getEventTime()-event.getDownTime()); //總共按下時花費時間
2)第二種狀況是實現一個OnTouchListener的方法,來設置View的偵聽屬性,而後實現onTouch(View view, MotionEvent event)的方法,就能夠獲取觸屏的感應事件了。翻譯
在該事件中,有兩個參數能夠用來獲取對觸摸的控制,這兩個參數分別爲:MotionEvent.getAction()和MotionEvent.ACTION_MASK,前者用於對單點觸控進行操做,後者用於對多點觸控進行操做,對於單點觸控,由MotionEvent.getAction()能夠獲得如下幾種事件:ACTION_DOWN、ACTION_UP,而對於多點觸控,由MotionEvent.ACTION_MASK,咱們能夠獲得:ACTION_POINTER_DOWN、ACTION_POINTER_UP,都是MotionEvent中的常量,能夠直接調用。而有些常量則是單點和多點共用的,如:ACTION_MOVE,所以在按下時,必須標記單點與多點觸控的區別。設計
3)注意:android2.2中onTouchEvent(MotionEvent event) 這裏能夠用event.getActionMasked()表示用於多點觸控檢測點。而在1.6和2.1中並無event.getActionMasked()這個方法,其實他就是把event.getAction()& MotionEvent.ACTION_MASK封裝了一下。code
3 案例
案例一
public class MultiTouchActivity extends Activity { <span style="color: rgb(0, 128, 0);"> /** Called when the activity is first created. */</span> <span style="color: rgb(0, 128, 0);"> @Override</span> public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } <span style="color: rgb(0, 128, 0);"> @Override</span> public boolean onTouchEvent(MotionEvent event){ int action = event.getAction(); switch(action){ case MotionEvent.ACTION_POINTER_1_DOWN: showMessage("第一個手指按下"); break; case MotionEvent.ACTION_POINTER_1_UP: showMessage("第一個手指擡起"); break; case MotionEvent.ACTION_POINTER_2_DOWN: showMessage("第二個手指按下"); break; case MotionEvent.ACTION_POINTER_2_UP: showMessage("第二個手指擡起"); break; case MotionEvent.ACTION_POINTER_3_DOWN: showMessage("第三個手指按下"); break; case MotionEvent.ACTION_POINTER_3_UP: showMessage("第三個手指擡起"); break; } return true; } private void showMessage(String s){ Toast toast = Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT); toast.show(); } }
實測效果以下:
狀況一:手指1按下沒有出現提示;手指1 擡起 也沒有出現提示;這是很顯然的,由於這時產生的消息是ACTION_DOWN 和 ACTION_UP。 狀況二:手指1按下沒有提示;手指2按下出現手指2按下的提示;手指2擡起 出現手指2擡起的提示。 狀況三:手指1按下沒有提示;手指2 按下 出現提示;這時手指1提起出現手指1提起的提示;手指1按下出現手指1按下的提示; 狀況四:你們能夠放三個手指去嘗試下,看看Android 是怎樣產生這些消息的。 根據實驗的結果,能夠獲得一句話:當屏幕上有一個手指時能夠完美的產生2點觸摸的消息;當屏幕上有2個手指時能夠完美的產生3點觸摸消息,以此類推……。所謂的完美就是指你能正確的獲得究竟是那個手指進行了操做。
案例二
public class Pointer2DrawActivity extends Activity implements OnTouchListener{ /** Called when the activity is first created. */ ImageView imgView; Bitmap bitmap; Canvas canvas; Paint paint; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); imgView = (ImageView)findViewById(R.id.imgView); Display currentDisplay = getWindowManager().getDefaultDisplay(); float dw = currentDisplay.getWidth(); float dh = currentDisplay.getHeight(); bitmap = Bitmap.createBitmap((int)dw, (int)dh, Config.ARGB_8888); canvas = new Canvas(bitmap); paint = new Paint(); paint.setColor(Color.GREEN); paint.setStrokeWidth((float) 10.00);//設置筆刷大小,本身的屏幕太犀利了 imgView.setImageBitmap(bitmap); imgView.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { int pointerCount = event.getPointerCount(); int pointerId = 0; int action = (event.getAction()&MotionEvent.ACTION_MASK) % 5;//統一單點和多點 switch(action){ case MotionEvent.ACTION_DOWN: if(pointerCount>1){ pointerId = (event.getAction()&MotionEvent.ACTION_POINTER_ID_MASK)>>> MotionEvent.ACTION_POINTER_ID_SHIFT; } break; case MotionEvent.ACTION_MOVE: if(pointerCount == 2){ float x = event.getX(1); float y = event.getY(1); canvas.drawPoint((int)x, (int)y, paint); imgView.invalidate(); } break; case MotionEvent.ACTION_UP: break; } return true; } }
案例三
public class GameView2X extends GameView implements SurfaceHolder.Callback { private float oldDist; private PointF midPoint = new PointF(); private boolean isZoom = false; public GameView2X(Context context, AttributeSet attrs) { super(context, attrs); } public boolean onTouchEvent(MotionEvent event) { switch (event.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: super.actionDown(event); break; case MotionEvent.ACTION_POINTER_UP: isZoom = false; break; /** * API原文是 A non-primary pointer has gone down. * 翻譯過來就是:非第一個點按下 */ case MotionEvent.ACTION_POINTER_DOWN: oldDist = spacing(event); midPoint(midPoint, event); isZoom = true; break; case MotionEvent.ACTION_MOVE: if (isZoom) { float newDist = spacing(event); /** * 表示新的距離比兩個手指剛觸碰的距離大 * ( +10個像素用來延遲一下放大,否則稍微動一點像素,也放大,感受也太快了。) */ if (newDist + 10 > oldDist) { super.getGameThread().getGameDraw() .checkXY((int) midPoint.x, (int) midPoint.y); super.getGameThread().getGameDraw().setIsZoom(true); } /** * 表示新的距離比兩個手指剛觸碰的距離小 */ if (newDist + 10 < oldDist) { super.getGameThread().getGameDraw().setIsZoom(false); GameDraw.newX = 0; GameDraw.newY = 0; } } super.actionMove(event); break; } return true; } private float spacing(MotionEvent event) { float x = event.getX(0) - event.getX(1); float y = event.getY(0) - event.getY(1); return FloatMath.sqrt(x * x + y * y); } private void midPoint(PointF point, MotionEvent event) { float x = event.getX(0) + event.getX(1); float y = event.getY(0) + event.getY(1); point.set(x / 2, y / 2); } }
案例四(圖片的放大和縮小)
public class TouchActivity extends Activity { private static final int NONE = 0; private static final int MOVE = 1; private static final int ZOOM = 2; private static final int ROTATION = 1; private int mode = NONE; private Matrix matrix = new Matrix(); private Matrix savedMatrix = new Matrix(); private PointF start = new PointF(); private PointF mid = new PointF(); private float s = 0; private float oldDistance; private int rotate = NONE; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); ImageView imageView = (ImageView)findViewById(R.id.imageView); imageView.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { ImageView imageView = (ImageView)view; switch (event.getAction()&MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: savedMatrix.set(matrix); start.set(event.getX(), event.getY()); mode = MOVE; rotate = NONE; break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_POINTER_UP: mode = NONE; break; case MotionEvent.ACTION_POINTER_DOWN: oldDistance = (float)Math.sqrt((event.getX(0)-event.getX(1))*(event.getX(0)-event.getX(1))+(event.getY(0)-event.getY(1))*(event.getY(0)-event.getY(1))); if (oldDistance > 10f) { savedMatrix.set(matrix); mid.set((event.getX(0)+event.getX(1))/2, (event.getY(0)+event.getY(1))/2); mode = ZOOM; } case MotionEvent.ACTION_MOVE: if (mode == MOVE) { if(rotate == NONE) { savedMatrix.set(matrix); mid.set(event.getX(), event.getY()); rotate = ROTATION; } else { matrix.set(savedMatrix); double a = Math.atan((mid.y-start.y)/(mid.x-start.x)); double b = Math.atan((event.getY()-mid.y)/(event.getX()-mid.x)); if ((b - a < Math.PI/2 && b - a > Math.PI / 18)||((b + Math.PI) % Math.PI - a < Math.PI/2 && (b + Math.PI) % Math.PI - a > Math.PI / 18)) { matrix.postScale((float)0.9, (float)0.9); } else if ((a - b < Math.PI / 2 && a - b > Math.PI / 18)||((a + Math.PI) % Math.PI - b < Math.PI/2 && (a + Math.PI) % Math.PI - b > Math.PI / 18)) { matrix.postScale((float)1.1, (float)1.1); } start.set(event.getX(), event.getY()); rotate = NONE; } } else if(mode == ZOOM) { float newDistance; newDistance = (float)Math.sqrt((event.getX(0)-event.getX(1))*(event.getX(0)-event.getX(1))+(event.getY(0)-event.getY(1))*(event.getY(0)-event.getY(1))); if(newDistance > 10f) { matrix.set(savedMatrix); matrix.postScale(newDistance/oldDistance, newDistance/oldDistance, mid.x, mid.y); oldDistance = newDistance; savedMatrix.set(matrix); } } break; } imageView.setImageMatrix(matrix); return true; } }); } }
main.xml文件以下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageView" android:layout_width="fill_parent" android:layout_height="fill_parent" android:src="@drawable/img" android:scaleType="matrix" > </ImageView> </LinearLayout>