安卓:滚轮动画? [英] Android: Scroller Animation?

查看:253
本文介绍了安卓:滚轮动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android开发的新手,我只是想知道滚轮部件(android.widget.Scroller)一点点。它是如何动画的看法?可以在动画对象,如果存在的话,可以访问?如果是这样,怎么样?我读过的源$ C ​​$ C,但找不到任何的线索,也许我太新?

我只是想做一些操作后,滚轮滚动完成,像

  m_scroller.getAnimation()setAnimationListener(...)。
 

解决方案

该滚轮部件实际上并没有做太多的工作,在所有的你的。它不引发任何回调,但没有任何动画,它只是响应各种方法调用。

那么有什么好处呢?好了,它所有的计算如的一扔给你,这是很方便的。那么,你通常做的是建立一个Runnable反复询问滚轮,我应该我的滚动位置是现在?我们做丢了吗?然后你重新发布了可运行在一个处理程序(通常是视图),直到一扔就完成了。

下面是从一个片段我的工作,现在一个例子:

 私有类护圈实现Runnable {
    私人最终滚轮滚动;

    私人诠释lastX = 0;

    抛油环(){
        滚动=新的滚轮(getActivity());
    }

    无效的开始(INT initialVelocity){
        INT initialX = scrollingView.getScrollX();
        INT MAXX = Integer.MAX_VALUE的; //或在您的code一些适当的最大值
        scroller.fling(initialX,0,initialVelocity,0,0,MAXX,0,10);
        Log.i(TAG,+ initialX +在开始猛冲,速度为+ initialVelocity +);

        lastX = initialX;
        。getView()后(本);
    }

    公共无效的run(){
        如果(scroller.isFinished()){
            Log.i(TAG,滚动完毕后,用一扔做);
            返回;
        }

        布尔多= scroller.computeScrollOffset();
        INT X = scroller.getCurrX();
        INT差异= lastX  -  X;
        如果(差异!= 0){
            scrollingView.scrollBy(差异,0);
            lastX = X;
        }

        如果(更多){
            。getView()后(本);
        }
    }

    布尔isFlinging(){
        返回scroller.isFinished()!;
    }

    无效forceFinished(){
        如果(!scroller.isFinished()){
            scroller.forceFinished(真正的);
        }
    }
}
 

使用Scroller.startScroll应该是相似的细节。

I'm a newbie in Android development, and I would just like to know a little bit about the Scroller widget (android.widget.Scroller). How does it animate the view? Can the Animation object, if it exists, be accessed? If so, how? I've read the source code, but could find no clues, or maybe I'm too new?

I just wanted to do some operations after a Scroller finishes scrolling, something like

m_scroller.getAnimation().setAnimationListener(...);

解决方案

The Scroller widget doesn't actually do much of the work at all for you. It doesn't fire any callbacks, it doesn't animate anything, it just responds to various method calls.

So what good is it? Well, it does all of the calculation for e.g. a fling for you, which is handy. So what you'd generally do is create a Runnable that repeatedly asks the Scroller, "What should my scroll position be now? Are we done flinging yet?" Then you repost that runnable on a Handler (usually on the View) until the fling is done.

Here's an example from a Fragment I'm working on right now:

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);
        }
    }
}

The details of using Scroller.startScroll should be similar.

这篇关于安卓:滚轮动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆