我正在尝试实现一个像水果忍者或割绳子的cut action
。
我的视图应该拦截这些cuts
(让我们称之为swipes)事件。我设置了一个简单的onSwipeTouchListener
,它工作得很好,但只有当Motionevent.ACTION_DOWN
被制作(滑动开始)在视图内,这是可悲的!
我不知道为什么系统不调度视图内部的ACTION_MOVE
事件,除非ACTION_DOWN
也在视图内部。
非常感谢!
我所能做的就是强制调度覆盖dispatchTouchEvent(MotionEvent)方法从活动:
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
dim = new Dimensions();
dim.set(ev);
super.dispatchTouchEvent(ev);
if(ev.getAction()==MotionEvent.ACTION_MOVE)
{
if(dim.isInside(sg)){
if(!ActionDownPassed) {
ev.setAction(MotionEvent.ACTION_DOWN);
ActionDownPassed = true;
}
}else if(ActionDownPassed){
ev.setAction(MotionEvent.ACTION_UP);
}
sg.dispatchTouchEvent(ev);
}
if(ev.getAction()==MotionEvent.ACTION_UP) ActionDownPassed = false;
return false;
}
它工作,但是我不认为这是一个好主意。
PS: Dimensions类:
public class Dimensions {
public int width;
public int height;
public Dimensions() {}
public Dimensions(int w, int h) {
width = w;
height = h;
}
public Dimensions(Dimensions p) {
this.width = p.width;
this.height = p.height;
}
public final void set(int w, int h) {
width = w;
height = h;
}
public final void set(Dimensions d) {
this.width = d.width;
this.height = d.height;
}
public final void set(MotionEvent e){
this.width = (int) e.getX();
this.height = (int) e.getY();
}
public final boolean equals(int w, int h) {
return this.width == w && this.height == h;
}
public final boolean equals(Object o) {
return o instanceof Dimensions && (o == this || equals(((Dimensions)o).width, ((Dimensions)o).height));
}
public boolean isInside(View v){
int[] a = new int[2] ;
v.getLocationOnScreen(a);
int w = v.getWidth();
int h = v.getHeight();
if( a[0] <= this.width
&& this.width <= (a[0] + w)
&& a[1] <= this.height
&& this.height <= (a[1] + h) )
return true;
else return false;
}
}