標籤:
觸控事件
MotionEvent類:
//單擊觸摸按下動作 public static final int ACTION_DOWN = 0; /** * Constant for {@link #getActionMasked}: A pressed gesture has finished, the * motion contains the final release location as well as any intermediate * points since the last down or move event. * 單擊觸摸離開動作 */ public static final int ACTION_UP = 1; /** * Constant for {@link #getActionMasked}: A change has happened during a * press gesture (between {@link #ACTION_DOWN} and {@link #ACTION_UP}). * The motion contains the most recent point, as well as any intermediate * points since the last down or move event. * 觸摸點移動動作 */ public static final int ACTION_MOVE = 2; /** * Constant for {@link #getActionMasked}: The current gesture has been aborted. * You will not receive any more points in it. You should treat this as * an up event, but not perform any action that you normally would. * 觸摸動作取消 */ public static final int ACTION_CANCEL = 3; /** * Constant for {@link #getActionMasked}: A movement has happened outside of the * normal bounds of the UI element. This does not provide a full gesture, * but only the initial location of the movement/touch. * 觸摸動作超出邊界 */ public static final int ACTION_OUTSIDE = 4; /** * Constant for {@link #getActionMasked}: A non-primary pointer has gone down. * <p> * Use {@link #getActionIndex} to retrieve the index of the pointer that changed. * </p><p> * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the * unmasked action returned by {@link #getAction}. * </p> * 多點觸摸按下動作 */ public static final int ACTION_POINTER_DOWN = 5; /** * Constant for {@link #getActionMasked}: A non-primary pointer has gone up. * <p> * Use {@link #getActionIndex} to retrieve the index of the pointer that changed. * </p><p> * The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the * unmasked action returned by {@link #getAction}. * </p> * 多點離開動作 */ public static final int ACTION_POINTER_UP = 6;
MotionEvent提供的方法:
getTop():擷取到的是View自身的頂邊到其父布局頂邊的距離。getLeft():擷取到的是View自身的左邊到其父布局左邊的距離。getRight():擷取到的是View自身的右邊到其父布局右邊的距離。getBottom():擷取到的是View自身的底邊到其父布局底邊的距離。getX():擷取點擊事件距離控制項左邊的距離getY():擷取點擊事件距離控制項頂邊的距離getRawX():擷取點擊事距離整個螢幕左邊的距離,即絕對座標。getRawY():擷取點擊事距離整個螢幕頂邊的距離,即絕對座標。
**實現滑動的基本原理(核心思想):
當觸摸View時候,系統記錄當前觸摸點的座標,當手指移動時候,系統記錄下移動的觸摸點座標,從而擷取到相對於前一次座標點的位移量,並且通過位移量修改view的座標,這樣不斷重複,從而實現滑動效果。**
實現滑動的常用方法:
layout方法:
package com.example.drawdemo;import android.content.Context;import android.graphics.Color;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;public class MyView5 extends View { private int lastX; private int lastY; public MyView5(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(); } public MyView5(Context context, AttributeSet attrs) { super(context, attrs); initView(); } public MyView5(Context context) { super(context); initView(); } private void initView() { setBackgroundColor(Color.BLUE); } // 視圖座標方式 public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = x; lastY = y; break; case MotionEvent.ACTION_MOVE: // 計算位移量 int offsetX = x - lastX; int offsetY = y - lastY; layout(getLeft() + offsetX, getTop() + offsetY, getRight() + offsetX, getBottom() + offsetY); break; default: break; } return true; }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.drawdemo.MainActivity" > <com.example.drawdemo.MyView5 android:layout_width="100dp" android:layout_height="100dp" /></RelativeLayout>
通過LayoutParams進行滑動。
LayoutParams 儲存了一個View的布局參數,因此在程式中,通過改變LayoutParams修改一個布局的位置參數,從而達到改變View位置的效果。
而且我們可以使用getLayoutparams()來擷取一個View的LayoutParams.計算位移量的方法與在Layout方法中計算offset也是一樣的,當擷取位移量後,就可以通過setLayoutParams來改變Layoutparams.
當然擷取父容易布局,會有不同的布局(如果父容易是LinearLatyou,那麼就要使用LinearLayout.LayoutParams.如果是RelativeLayout,那麼就要使用RelativeLayout.LayoutParams.),所以我們直接使用ViewGroup.MarginLayoutParams來實現這個功能。
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();// LinearLayout.LayoutParams layoutParams =(LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLeft() + offsetX; layoutParams.topMargin = getTop() + offsetY;
這個使用更加方便,不用考錄父容器布局。
package com.example.drawdemo;import android.content.Context;import android.graphics.Color;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.ViewGroup;public class MyView6 extends View { private int lastX; private int lastY; public MyView6(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub ininView(); } public MyView6(Context context, AttributeSet attrs) { super(context, attrs); ininView(); // TODO Auto-generated constructor stub } public MyView6(Context context) { super(context); ininView(); // TODO Auto-generated constructor stub } private void ininView() { setBackgroundColor(Color.GREEN); } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // 記錄觸摸點座標 lastX = (int) event.getX(); lastY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: // 計算位移量 int offsetX = x - lastX; int offsetY = y - lastY; ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams) getLayoutParams();// LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) getLayoutParams(); layoutParams.leftMargin = getLeft() + offsetX; layoutParams.topMargin = getTop() + offsetY; setLayoutParams(layoutParams); break; } return true; }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.drawdemo.MainActivity" > <com.example.drawdemo.MyView6 android:layout_width="100dp" android:layout_height="100dp" /></RelativeLayout>
通過scrollTo與scorllBy進行滑動。
android 系統提供這個兩個方式進行改變一個View的位置
與前面的方法相同,也是計算位移量。
package com.example.drawdemo;import android.content.Context;import android.graphics.Color;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;public class MyView7 extends View{ private int lastX; private int lastY; public MyView7(Context context) { super(context); ininView(); } public MyView7(Context context, AttributeSet attrs) { super(context, attrs); ininView(); } public MyView7(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); ininView(); } private void ininView() { setBackgroundColor(Color.YELLOW); } @Override public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getX(); lastY = (int) event.getY(); break; case MotionEvent.ACTION_MOVE: int offsetX = x - lastX; int offsetY = y - lastY; ((View) getParent()).scrollBy(-offsetX, -offsetY); break; } return true; }}
Android觸控事件