【问题标题】:Android Scroller simple exampleAndroid Scroller 简单示例
【发布时间】:2013-04-07 22:48:40
【问题描述】:

谁能给我一个关于 Scroller 类的简单例子?据我了解,它封装了滚动,所以我需要开始计算,然后手动更新必须 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());
    }

我的输出只有零。我的错在哪里?

【问题讨论】:

标签: java android scroll scroller


【解决方案1】:
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);
        }
    }
}

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

【讨论】:

    【解决方案2】:

    startScroll() 中的最后一个参数是持续时间。 可能你的for-loop 在滚动条完成之前完成:) 你也应该打电话给computeScrollOffset() 试试这个代码,它可以工作:

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

    【讨论】:

      猜你喜欢
      • 2017-03-27
      • 2015-06-21
      • 1970-01-01
      • 2016-02-19
      • 2018-06-13
      • 2011-03-13
      • 1970-01-01
      • 2016-02-02
      • 2014-03-14
      相关资源
      最近更新 更多