简体   繁体   English

Scroller.fling()无法正常工作

[英]Scroller.fling() isn't working as expected

This is code I'm using to receive touch events, and feed them into x,y that my canvas is using for translation : 这是我用来接收触摸事件并将它们馈送到我的画布用于翻译的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
    }
}

Link to a video of behavior 链接到行为视频

So scrolling is working as I would expect it to be, but flinging is not. 因此,滚动正在按我期望的那样工作,但滚动不起作用。 While the scroll method is constantly getting called to return values and translate the canvas, fling is only called once, and returns whatever max value i put into the function call. 在不断调用scroll方法以返回值并转换画布的同时,fling仅被调用一次,并返回我放入函数调用中的任何最大值。

I thought scroller.fling / computeScroll was a recursive callback every frame, and as long as the fling isn't over, I'm going to receive information on where to translate my view? 我以为scroller.fling / computeScroll是每帧的递归回调,并且只要未结束,我就将收到有关将视图转换到何处的信息。 What am I doing wrong? 我究竟做错了什么?

(thanks to @Roughy from irc.freenode/android-dev) (感谢irc.freenode / android-dev中的@Roughy)

The first problem here is that you're passing the wrong values to .fling() 这里的第一个问题是您将错误的值传递给.fling()

    scroller.fling(startX, startY, velX, velY, minX, MAXX, MINY, MAXY);

You flipped the min/max around. 您翻转了最小/最大。

The second issue is that you're calling mVelocity = VelocityTracker.obtain(); 第二个问题是您正在调用mVelocity = VelocityTracker.obtain(); every time you have a movement event change, which creates a new velocity tracker for every movement frame, you should call it only once on MotionEvent.ACTION_DOWN , and then call mVelocity.addMovement(event); 每次你有一个移动事件的变化,这对于每一个动作帧创建一个新的速度跟踪的时候,你应该把它只有一次在MotionEvent.ACTION_DOWN ,然后调用mVelocity.addMovement(event); on ACTION_MOVE, and ACTION_UP. 在ACTION_MOVE和ACTION_UP上。

Call mVelocity.recycle(); 调用mVelocity.recycle(); on ACTION_UP too. 也是在ACTION_UP上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM