触控事件
MotionEvent类:

    /**     * Constant for {@link #getActionMasked}: A pressed gesture has started, the     * motion contains the initial starting location.     * 

* This is also a good time to check the button state to distinguish * secondary and tertiary button clicks and handle them appropriately. * Use {@link #getButtonState} to retrieve the button state. *

*/
//单击触摸按下动作 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. *

* Use {@link #getActionIndex} to retrieve the index of the pointer that changed. *

* The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the * unmasked action returned by {@link #getAction}. *

* 多点触摸按下动作 */
public static final int ACTION_POINTER_DOWN = 5; /** * Constant for {@link #getActionMasked}: A non-primary pointer has gone up. *

* Use {@link #getActionIndex} to retrieve the index of the pointer that changed. *

* The index is encoded in the {@link #ACTION_POINTER_INDEX_MASK} bits of the * unmasked action returned by {@link #getAction}. *

* 多点离开动作 */
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;    }}
"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" />

通过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;        }}
"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" />

通过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;    }}

更多相关文章

  1. Android(安卓)原生获取地理位置
  2. Android平板获取唯一标识DeviceId
  3. android设备信息获取
  4. android获取内部外部存储空间
  5. android > TimePicker 时间设置控件
  6. Android(安卓)获取wifi信号强度
  7. Android中通过NTP服务器获取时间功能源码分析
  8. Android(安卓)一些小技巧
  9. Android(安卓)将从网络获取的数据缓存到私有文件

随机推荐

  1. 11 Weeks of Android: 联系人与身份
  2. Android(安卓)Handle用法
  3. Activity的四种启动模式(LaunchMode)
  4. android 开机log的制作过程
  5. Android(安卓)中View.onDraw(Canvas canv
  6. 利用Android画圆弧canvas.drawArc()实例
  7. android-adb调试与sqlite远程shell(andro
  8. Android系统应用层App启动过程分析
  9. (20120731)android面试总结(002)
  10. OS X环境下如何搭建编译Cocos2D-X v3.x的