这是我用来接收触摸事件的代码,并将它们输入到x,y中,我的画布用于翻译:
VelocityTracker mVelocity;
Scroller scroller;
@Override
public boolean onTouchEvent(MotionEvent event) { //On touch
super.onTouchEvent(event);
mVelocity = VelocityTracker.obtain();
mVelocity.addMovement(event);
//Log.d("VELOCITY","("+mVelocity.getXVelocity()+","+mVelocity.getYVelocity()+")");
if(event.getActionMasked()==MotionEvent.ACTION_MOVE){ //If it's a drag, call scrollMove
mVelocity.computeCurrentVelocity(1);
scrollMove();
}
if(event.getActionMasked()==MotionEvent.ACTION_UP) { //or scrollFling
mVelocity.computeCurrentVelocity(1);
scrollFling();
}
mVelocity.recycle();
return true;
}
void scrollMove(){ //Canvas is constantly translating by (scrollMove.x,scrollMove.y) every time invalidate() is called
scrollMove.x += mVelocity.getXVelocity() * 10f;
scrollMove.y += mVelocity.getYVelocity() * 10f;
invalidate();
}
void scrollFling(){ //how does scroller.fling work?
scroller.fling(getScrollX(),getScrollY(),(int)-mVelocity.getXVelocity(),(int)-mVelocity.getYVelocity(),0,0,10000,10000);
invalidate();
}
@Override
public void computeScroll() {
super.computeScroll();
if(!scroller.isFinished()){
scroller.computeScrollOffset();
scrollMove.x += scroller.getCurrX();
scrollMove.y += scroller.getCurrY();
//postInvalidateOnAnimation();
postInvalidateOnAnimation(); //Alternated between all 3 of these
postInvalidate(); //??
invalidate(); //??
Log.d("FLING","Should be flinging"); //This is only called once
}
}因此,滚动正像我所期望的那样起作用,但抛出却不是。当滚动方法不断被调用以返回值和转换画布时,call只被调用一次,并返回函数调用中的任何最大值。
我认为scroller.fling / computeScroll是每一个帧的递归回调,只要view还没有结束,我就会收到关于在哪里翻译我的观点的信息?我做错了什么?
发布于 2018-10-16 02:46:36
(感谢irc.freenode/android-dev的@Roughy )
这里的第一个问题是将错误的值传递给.fling()
scroller.fling(startX, startY, velX, velY, minX, MAXX, MINY, MAXY);你翻来覆去。
第二个问题是,每次发生运动事件更改时都要调用mVelocity = VelocityTracker.obtain();,这会为每个运动帧创建一个新的速度跟踪器,您应该在MotionEvent.ACTION_DOWN上调用它一次,然后在ACTION_MOVE上调用mVelocity.addMovement(event);,然后调用ACTION_UP。
也打电话给mVelocity.recycle(); on ACTION_UP。
https://stackoverflow.com/questions/52827041
复制相似问题