下面是TouchDelegate的官方描述
Helper class to handle situations where you want a view to have a larger touch area than its actual view bounds. The view whose touch area is changed is called the delegate view. This class should be used by an ancestor of the delegate. To use a TouchDelegate, first create an instance that specifies the bounds that should be mapped to the delegate and the delegate view itself.
The ancestor should then forward all of its touch events received in its onTouchEvent(MotionEvent)
to onTouchEvent(MotionEvent)
.
用法:
1. 这个类要被delegate view的父使用;
2. 这个父要在onTouchEvent()处理自己的所有触摸事件
注意:
点击view的实际尺寸区域,会按照正常的逻辑处理view触摸事件;点击扩展的区域,程序会通过TouchDelegate的onTouchEvent()走到view的处理逻辑,区别是后面这个处理的点击坐标和前面不同,扩展区域的点击坐标是不变的,无论点在扩展区域的任何地方,坐标都是(getWidget() / 2, getHeight() / 2),如果MOVE到扩展区域外,则坐标会是一个不变的负数(-(slot * 2), -(slot * 2))
TouchDelegate的使用
View parent = (View)v.getParent();
parent.post(new Runnable() {
@Override
public void run() {
Rect delegateArea = new Rect();
ChildView delegate = v;
delegate.getHitRect(delegateArea);
delegateArea.bottom += 200;
TouchDelegate expandedArea = new TouchDelegate(delegateArea,
delegate);
if (View.class.isInstance(delegate.getParent())) {
((View) delegate.getParent())
.setTouchDelegate(expandedArea);
}
}
});
parent的处理
@Override
public boolean onTouchEvent(MotionEvent event){
return super.onTouchEvent(event);
}