gpt4 book ai didi

java - Android Scroller 简单示例

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:32:06 27 4
gpt4 key购买 nike

谁能给我一个关于 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());
}

我所有的输出都是零。我的错误在哪里?

最佳答案

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

关于java - Android Scroller 简单示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11030639/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com