首先通过自定义的View的案例,通过打印的Log来学习下,事件是如何分发的?
通过自定义个电子签名的View,来讲解
如下为自定义电子签名板的HandWritingView:
package com.yang.wx.androidtest.view7;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class HandWritingView extends View{
private static final String TAG = "HandWritingView01";
//声明只画笔
private Paint mPaint;
//起始位置
private float lastX,lastY;
private Path path;
private Bitmap mBitmap;
private Canvas mCanvas;
public HandWritingView(Context context) {
this(context,null);
}
public HandWritingView(Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
private void initView(){
setLayerType(View.LAYER_TYPE_SOFTWARE,null);
//画笔
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG|Paint.DITHER_FLAG);
mPaint.setAntiAlias(true);
mPaint.setStrokeWidth(5f);
mPaint.setColor(Color.parseColor("#FF4081"));
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);//使画笔更加圆润
mPaint.setStrokeCap(Paint.Cap.ROUND);//同上
path = new Path();
//保存签名的画布
post(new Runnable() {
@Override
public void run() {
//拿到控件的宽和高
mBitmap = Bitmap.createBitmap(getWidth(),getHeight(), Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}
});
}
/**
* 绘制
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if(null!=mBitmap){
canvas.drawColor(Color.WHITE); //绘制背景白色
mCanvas.drawPath(path,mPaint);//将路径绘制在mBitmap上
canvas.drawBitmap(mBitmap,0,0,null);//将mBitmap绘制在canvas上
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.i(TAG,"dispatchTouchEvent:ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG,"dispatchTouchEvent:ACTION_MOVE");
// return false;
break;
case MotionEvent.ACTION_UP:
Log.i(TAG,"dispatchTouchEvent:ACTION_UP");
break;
}
return super.dispatchTouchEvent(event);
}
/**
* 触摸事件和 触摸绘制
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
float x = event.getX();
float y = event.getY();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.i(TAG,"onTouchEvent:ACTION_DOWN");
lastX = x;
lastY = y;
path.moveTo(lastX,lastY);
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG,"onTouchEvent:ACTION_MOVE");
float dx = Math.abs(x-lastX);
float dy = Math.abs(y-lastY);
if(dx>=3 || dy >=3){
//利用二阶贝塞尔曲线,使绘制路劲更加圆滑
path.quadTo(lastX,lastY,(lastX+x)/2,(lastY+y)/2);
}
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_UP:
Log.i(TAG,"onTouchEvent:ACTION_UP");
path.reset();
break;
}
invalidate();
return true;
}
//获取画好的Bitmap
public Bitmap getmBitmap(){
return mBitmap;
}
//清空画布
public void clearCanvas(){
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mCanvas.drawPaint(mPaint);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
path.reset();
invalidate();
}
}
其中在dispatchTouchEvent和onTouchEvent方法打印了LOG
然后写了一个Activity调用该view:
该activity的layout如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.yang.wx.androidtest.view7.HandWritingView
android:id="@+id/handwritview"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- <com.yang.wx.androidtest.view7.HandWritingView
android:id="@+id/handwritview2"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignBottom="@+id/handwritview"
android:layout_height="40dip">
<Button
android:id="@+id/clear_bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清空画布"/>
</LinearLayout>
</RelativeLayout>
activity中在dispatchTouchEvent和onTouchEvent方法打印了LOG如下:
然后运行代码,查看日志:
当按下的时候,首先调用的是最外层Activity中的dispatchTouchEvent的方法,接着调用自定义view中的dispatchTouchEvent方法,再调用自定义view中的onTouchEvent方法,后面的MOVE和UP流程一样的,这说明两点,第一,VIew事件分发,首先消费Activity,再分发view,第二,事件分发首先调用dispatchTouchEvent发放,再调用onTouchEvent方法。
好的我们接着改下代码,将Activity里的dispatchTouchEvent当滑动的时候返回false,看看有什么结果呢?
运行结果如下:
看到了没,滑动的时候,只打印了Activty里的日志,自定义view里没有打印日志,说明,Activity中dispatchTouchEvent将MOVE时候将事件拦截了,不往后面分发了,所以MOVE的时候,在自定义View中dispatchTouchEvent和onTouchEvent两个方法就不被调用。
当我们将Activity中dispatchTouchEvent MOVE的时候还设置为true,在自定义VIew中MOVE时候dispatchTouchEvent ,返回false,相信自定义View中onTouchEvent,方法,就不会被调用了。
好的,接下来我们再加一个自定义view,继承原来的view,如下代码:
package com.yang.wx.androidtest.view7;
import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
public class HandWritingView2 extends HandWritingView{
private static final String TAG = "HandWritingView02";
public HandWritingView2(Context context) {
this(context,null);
}
public HandWritingView2(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.i(TAG,"dispatchTouchEvent:ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG,"dispatchTouchEvent:ACTION_MOVE");
// return false;
break;
case MotionEvent.ACTION_UP:
Log.i(TAG,"dispatchTouchEvent:ACTION_UP");
break;
}
return super.dispatchTouchEvent(event);
}
/**
* 触摸事件和 触摸绘制
* @param event
* @return
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_DOWN:
Log.i(TAG,"onTouchEvent:ACTION_DOWN");
break;
case MotionEvent.ACTION_MOVE:
Log.i(TAG,"onTouchEvent:ACTION_MOVE");
break;
case MotionEvent.ACTION_UP:
Log.i(TAG,"onTouchEvent:ACTION_UP");
break;
}
return super.onTouchEvent(event);
}
}
再运行代码,打印log,如下:
其分发过程如下:HangWritingActivity——>HangWritingView02——>HangWritingView01