在CeShiActivity.java文件中
public class CeShiActivity extends AppCompatActivity {
private YuanHuView button;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ceshiactivity);
button = (YuanHuView) findViewById(R.id.button5);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d("xinxi", "你点击了YuanHuView");
}
});
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {//重写activity的dispatchTouchEvent,return true或者是return false,则该activity上的任何view将不会响应任何触屏事件
Log.d("xinxi", "CeShiActivity--dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("xinxi", "CeShiActivity--触发ACTION_DOWN了");
break;
case MotionEvent.ACTION_MOVE:
Log.d("xinxi", "CeShiActivity--触发ACTION_MOVE了");
break;
case MotionEvent.ACTION_UP:
Log.d("xinxi", "CeShiActivity--触发ACTION_UP了");
break;
}
return super.onTouchEvent(event);
}
}
在ZViewGroup.java文件中
public class ZViewGroup extends RelativeLayout {
public ZViewGroup(Context context) {
super(context);
}
public ZViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ZViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
Log.d("xinxi", "ZViewGroup--onInterceptTouchEvent");
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
Log.d("xinxi", "ZViewGroup--dispatchTouchEvent");
return super.dispatchTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("xinxi", "ZViewGroup--触发ACTION_DOWN了");
break;
case MotionEvent.ACTION_MOVE:
Log.d("xinxi", "ZViewGroup--触发ACTION_MOVE了");
break;
case MotionEvent.ACTION_UP:
Log.d("xinxi", "ZViewGroup--触发ACTION_UP了");
break;
}
return super.onTouchEvent(event);
}
}
在YuanHuView.java文件中
public class YuanHuView extends View {
private Paint yuan;
private Paint wenZi;
private Paint yuanHu;
private float huDu=120;
public YuanHuView(Context context) {
super(context);
}
public YuanHuView(Context context, AttributeSet attrs) {
super(context, attrs);
yuan=new Paint();
yuan.setColor(Color.GREEN);
yuan.setStyle(Paint.Style.FILL);
wenZi=new Paint();
wenZi.setColor(Color.BLACK);
wenZi.setStyle(Paint.Style.FILL);
yuanHu=new Paint();
yuanHu.setColor(Color.RED);
yuanHu.setStrokeWidth(40);
yuanHu.setStyle(Paint.Style.STROKE);
}
public YuanHuView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawArc(getMeasuredWidth()/2-260,getMeasuredHeight()/2-260,getMeasuredWidth()/2+260,getMeasuredHeight()/2+260,0,huDu,false,yuanHu);
canvas.drawCircle(getMeasuredWidth()/2,getMeasuredHeight()/2,200,yuan);
canvas.drawText("我是文字",getMeasuredWidth()/2-10,getMeasuredHeight()/2-10,wenZi);
}
public void setHuDu(float huDu){
this.huDu=huDu;
invalidate();//执行该方法后,将会重新调用onDraw方法
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
Log.d("xinxi", "YuanHuView--dispatchTouchEvent");
return super.dispatchTouchEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.d("xinxi", "YuanHuView--触发ACTION_DOWN了");
break;
case MotionEvent.ACTION_MOVE:
Log.d("xinxi", "YuanHuView--触发ACTION_MOVE了");
break;
case MotionEvent.ACTION_UP:
Log.d("xinxi", "YuanHuView--触发ACTION_UP了");
break;
}
return super.onTouchEvent(event);
}
}
在所有的dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent都返回super的话,结果如下:
04-12 17:15:44.795 824-824/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:15:44.795 824-824/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:15:44.795 824-824/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:15:44.795 824-824/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:15:44.795 824-824/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_UP了
04-12 17:15:44.805 824-824/com.example.liang.lianxi D/xinxi: 你点击了YuanHuView
在所有的dispatchTouchEvent,onTouchEvent都返回super,onInterceptTouchEvent返回false的话,结果同上:
04-12 17:19:35.165 6452-6452/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:19:35.175 6452-6452/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:19:35.175 6452-6452/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:19:35.175 6452-6452/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:19:35.175 6452-6452/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 17:19:35.245 6452-6452/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:19:35.245 6452-6452/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:19:35.245 6452-6452/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:19:35.245 6452-6452/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:19:35.255 6452-6452/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_UP了
04-12 17:19:35.265 6452-6452/com.example.liang.lianxi D/xinxi: 你点击了YuanHuView
在所有的dispatchTouchEvent,onTouchEvent都返回super,onInterceptTouchEvent返回true的话,结果为:
04-12 17:27:52.835 22763-22763/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:27:52.845 22763-22763/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:27:52.845 22763-22763/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:27:52.845 22763-22763/com.example.liang.lianxi D/xinxi: ZViewGroup--触发ACTION_DOWN了
04-12 17:27:52.845 22763-22763/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_DOWN了
04-12 17:27:52.935 22763-22763/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:27:52.935 22763-22763/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_UP了
其他都返回super,只有ZViewGroup的dispatchTouchEvent方法返回true的话,结果为:
04-12 17:31:28.455 27897-27897/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:31:28.455 27897-27897/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:31:28.515 27897-27897/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:31:28.515 27897-27897/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
其他都返回super,只有ZViewGroup的dispatchTouchEvent方法返回false的话,结果为:
04-12 17:33:42.095 789-789/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:33:42.095 789-789/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:33:42.095 789-789/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_DOWN了
04-12 17:33:42.195 789-789/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:33:42.195 789-789/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_UP了
其他都返回super,只有ZViewGroup的onTouchEvent方法返回true的话,结果为:
04-12 17:36:24.425 5125-5125/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:36:24.425 5125-5125/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:36:24.425 5125-5125/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:36:24.425 5125-5125/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:36:24.425 5125-5125/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 17:36:24.545 5125-5125/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:36:24.545 5125-5125/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:36:24.545 5125-5125/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:36:24.545 5125-5125/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:36:24.555 5125-5125/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_UP了
04-12 17:36:24.565 5125-5125/com.example.liang.lianxi D/xinxi: 你点击了YuanHuView
其他都返回super,只有ZViewGroup的onTouchEvent方法返回false的话,结果为:
04-12 17:40:38.315 13699-13699/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:40:38.315 13699-13699/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:40:38.315 13699-13699/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:40:38.315 13699-13699/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:40:38.315 13699-13699/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_UP了
04-12 17:40:38.445 13699-13699/com.example.liang.lianxi D/xinxi: 你点击了YuanHuView
其他都返回super,只有YuanHuView的dispatchTouchEvent方法返回true的话,结果为:
04-12 17:45:59.835 23834-23834/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:45:59.835 23834-23834/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:45:59.835 23834-23834/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:45:59.835 23834-23834/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:45:59.895 23834-23834/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:45:59.895 23834-23834/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:45:59.895 23834-23834/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:45:59.895 23834-23834/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
其他都返回super,只有YuanHuView的dispatchTouchEvent方法返回false的话,结果为:
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: ZViewGroup--触发ACTION_DOWN了
04-12 17:54:38.775 3957-3957/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_DOWN了
04-12 17:54:38.835 3957-3957/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 17:54:38.835 3957-3957/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_UP了
其他都返回super,只有YuanHuView的onTouchEvent方法返回true的话,结果为:
04-12 18:00:39.215 14397-14397/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 18:00:39.215 14397-14397/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 18:00:39.215 14397-14397/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 18:00:39.215 14397-14397/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 18:00:39.215 14397-14397/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 18:00:39.345 14397-14397/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 18:00:39.345 14397-14397/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 18:00:39.345 14397-14397/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 18:00:39.345 14397-14397/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 18:00:39.345 14397-14397/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_UP了
其他都返回super,只有YuanHuView的onTouchEvent方法返回false的话,结果为:
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: ZViewGroup--dispatchTouchEvent
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: ZViewGroup--onInterceptTouchEvent
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: YuanHuView--dispatchTouchEvent
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: YuanHuView--触发ACTION_DOWN了
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: ZViewGroup--触发ACTION_DOWN了
04-12 18:03:39.355 22402-22402/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_DOWN了
04-12 18:03:39.445 22402-22402/com.example.liang.lianxi D/xinxi: CeShiActivity--dispatchTouchEvent
04-12 18:03:39.445 22402-22402/com.example.liang.lianxi D/xinxi: CeShiActivity--触发ACTION_UP了
心得:当view的onTouchEvent事件return true,view就获取了事件,这是需要在onTouchEvent方法中对事件的类型进行分类处理,如果不进行处理的话,那么这个view将不会触发任何事件,比如单击,双击,长按等等,因为你的onTouchEvent事件return true了,而不是return super.onTouchEvent(event),所以需要在onTouchEvent方法中进行事件的重新绑定,代码如下:
public class ZButton extends Button {
private OnClickListener onClickListener;
public ZButton(Context context) {
super(context);
}
public ZButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ZButton(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void setOnClickListener(OnClickListener l) {
onClickListener=l;
}
@Override
public boolean onTouchEvent(MotionEvent event) {//当view的onTouchEvent事件return true,view就获取了事件,这是需要
// 在onTouchEvent方法中对事件的类型进行分类处理,如果不进行处理的话,那么这个view将不会触发任何事件,比如单击,
// 双击,长按等等,因为你的onTouchEvent事件return true了,而不是return super.onTouchEvent(event),所以需要在onTouchEvent
//方法中进行事件的重新绑定
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//onClickListener.onClick(this);自己进行单击事件的绑定
callOnClick();//调用系统的单击事件绑定
break;
}
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
return super.dispatchTouchEvent(event);
}
}
网友评论