java Android Scroller 简单示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11030639/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:30:06  来源:igfitidea点击:

Android Scroller simple example

javaandroidscrollscroller

提问by Artem Caritas

Can anyone gives me simple example about Scroller class? As I understand, it encapsulates scrolling, so I need start calculating and then manually update must ScrollView to new positions. So I just try

谁能给我一个关于 Scroller 类的简单例子?据我了解,它封装了滚动,所以我需要开始计算然后手动更新 must ScrollView 到新位置。所以我只是尝试

 Scroller scroller = new Scroller(getApplicationContext());
    scroller.startScroll(0, 0, 10, 10, 500);
    for (int i = 0; i < 100; i++) {
        Log.d("scroller", scroller.getCurrX()+" "+ scroller.getCurrY());
    }

All I have in output is just zeros. Where is my mistake?

我在输出中所拥有的只是零。我的错误在哪里?

回答by Thkru

private class Flinger implements Runnable {
    private final Scroller scroller;

    private int lastX = 0;

    Flinger() {
        scroller = new Scroller(getActivity());
    }

    void start(int initialVelocity) {
        int initialX = scrollingView.getScrollX();
        int maxX = Integer.MAX_VALUE; // or some appropriate max value in your code
        scroller.fling(initialX, 0, initialVelocity, 0, 0, maxX, 0, 10);
        Log.i(TAG, "starting fling at " + initialX + ", velocity is " + initialVelocity + "");

        lastX = initialX;
        getView().post(this);
    }

    public void run() {
        if (scroller.isFinished()) {
            Log.i(TAG, "scroller is finished, done with fling");
            return;
        }

        boolean more = scroller.computeScrollOffset();
        int x = scroller.getCurrX();
        int diff = lastX - x;
        if (diff != 0) {
            scrollingView.scrollBy(diff, 0);
            lastX = x;
        }

        if (more) {
            getView().post(this);
        }
    }

    boolean isFlinging() {
        return !scroller.isFinished();
    }

    void forceFinished() {
        if (!scroller.isFinished()) {
            scroller.forceFinished(true);
        }
    }
}

Taken from https://stackoverflow.com/a/6219382/1351347

摘自https://stackoverflow.com/a/6219382/1351347

回答by agamov

Last parameter in startScroll()is duration. Probably your for-loop finishes before the scroller gets anything done :) And also you should call computeScrollOffset()Try this code, it works:

最后一个参数startScroll()是持续时间。可能您的for-loop 在滚动条完成任何操作之前就完成了 :) 而且您还应该调用computeScrollOffset()Try this code,它可以工作:

Scroller scroller = new Scroller(getApplicationContext());
scroller.startScroll(0, 0, 100, 100, 500);
while (!scroller.isFinished()) {
    Log.d("scroller", scroller.getCurrX() + " " + scroller.getCurrY());
    scroller.computeScrollOffset();
}