标签:
触控事件
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;
}
}
标签:
原文地址:http://blog.csdn.net/dtj_74/article/details/51907325