1.触摸事件的处理方式:
一种回调,一种监听。重写组件的某个特定的方法实现回调事件处理,事件的发生者和处理者是同一对象。通过编写监听类绑定组件实现监听事件处理,事件的发生者和处理者不是同一对象。
2.常用的监听器:
监听的点击事件:View.OnClickListener
监听的长按事件:View.OnLongClickListener
监听的触摸事件:View.OnTouchListener
监听焦点改变事件:View.OnFocusChangeListener
与长按监听一起使用,生成菜单:View.OnCreateContextMenuListener
注意:监听事件与回调事件一起调用,会根据优先级来决定执行顺序。
3.触摸事件的返回值:若为false,对此次的触摸事件不感兴趣,这次事件本应实现的后续动作回传给上一级。具有传播性
若为true,对此次的触摸事件感兴趣,直接由它处理。不具有传播性
4.行为性:
action:0--------手指1按下
261-----手指2按下
517-------手指3按下
518------手指3抬起
262------手指2抬起
1--------手指1抬起
2---------移动
5.拿取手指行为:
int action=event.getAction();
int pointIndex=action>>8;
int action_code=action&0xff;
获取每根手指的坐标:
int x=event.getX(指引坐标);
int y=event.getY(指引坐标);
。。。
6.多点触摸的代码:
package com.zking.diypointtouch;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import java.util.Random;
/**
* Created by Administrator on 2017/7/9 0009.
*/
public class Circle {
public float x;
public float y;
public int r=50;
public int pointId;//获取手指的标明
public int red;
public int green;
public int blue;
Random random=new Random();
public Circle(float x, float y, int pointId) {
this.x = x;
this.y = y;
this.pointId = pointId;
red=random.nextInt(255);
green=random.nextInt(255);
blue=random.nextInt(255);
}
public Circle(float x, float y, int r, int pointId) {
this.x = x;
this.y = y;
this.r = r;
this.pointId = pointId;
}
public void draw(Canvas canvas, Paint paint){
paint.setColor(Color.rgb(red,green,blue));
paint.setStyle(Paint.Style.STROKE);
canvas.drawCircle(x,y,r,paint);
}
}
package com.zking.diypointtouch;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Administrator on 2017/7/9 0009.
*/
public class MyPoints extends View{
List<Circle> circles=new ArrayList<>();
private Circle c;
public MyPoints(Context context) {
super(context);
}
public MyPoints(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MyPoints(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
public MyPoints(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint=new Paint();
for (Circle c : circles) {
c.draw(canvas,paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
//实现多点触摸
//获取行为事件(方式)
int action=event.getAction();
//获取手指的下标
int pointIndex=action>>8;
//获取手指的Id
int pointId=event.getPointerId(pointIndex);
//获取手指的坐标
float x=event.getX(pointIndex);
float y=event.getY(pointIndex);
//获取手指的行为下代表数字
int action_code=action&0xff;
if(action_code>=5){
action_code-=5;
}
switch (action_code) {
case MotionEvent.ACTION_DOWN:
//实例化圆
c = new Circle(x,y,pointId);
//将圆加到集合中
circles.add(c);
break;
case MotionEvent.ACTION_MOVE:
//获取新的x,y
for (int i = 0; i <event.getPointerCount() ; i++) {
int id= event.getPointerId(i);
remove(id).x=event.getX(id);
remove(id).y=event.getY(id);
}
break;
case MotionEvent.ACTION_UP:
circles.remove(remove(pointId));
break;
}
//重绘
invalidate();
return true;
}
public Circle remove(int pointId){
for (Circle c : circles) {
if(c.pointId==pointId){
return c;
}
}
return null;
}
}