是否有监听的AnimatedVectorDrawables动画结束的方式 [英] Is there a way to listen for animation end in AnimatedVectorDrawables

查看:923
本文介绍了是否有监听的AnimatedVectorDrawables动画结束的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个AnimatedVectorDrawable,它的工作原理pretty好了,现在我正在寻找一种方法来改变动画或隐藏视图为完成后。我希望有一个监听器,但它并不像有。有人可以帮忙吗?

I have created an AnimatedVectorDrawable, it works pretty well, now I am looking for a way to change the animation or hide the view after it finishes. I was hoping there was a listener but it doesn't look like there is. Can someone help?

修改

于是,我找到了一个解决办法,但不是一个非常优雅的方式。我所做的就是创建一个线程,如果动画正在运行查询。

So I found a workaround, but not a very elegant way. What I did was create a thread and poll if the animation is running.

new Runnable() {
    public void run() {
        while(mLoop) {
            if(mAnimatedVectorDrawable.isRunning()) {
                Thread.sleep(mPollingInterval);
            } else {
                mLoop = false;
                // TODO what ever
            }
        }
    }
};

如果有人发现更好的解决方案,请分享。

If somebody finds a better solution, please share.

推荐答案

我的第一反应是取源$ C ​​$ C,添加一些回调,并创建自定义绘制出来。当然,这将意味着没有XML支持。

My first instinct was to take the source code, add some callbacks, and create a custom drawable out of it. Of course, that would have meant no xml support.

原来, AnimatedVectorDrawable 使用 VectorDrawable的私有方法(S)。因此,这种方法是行不通的。

It turns out that AnimatedVectorDrawable uses VectorDrawable's private method(s). So, this approach won't work.

我们可以创建一个围绕 AnimatedVectorDrawable 一个简单的包装类,并添加回调:

We could create a simple wrapper class around AnimatedVectorDrawable and add callbacks:

public class AVDWrapper {

    private Handler mHandler;
    private Animatable mDrawable;
    private Callback mCallback;
    private Runnable mAnimationDoneRunnable = new Runnable() {

        @Override
        public void run() {
            if (mCallback != null)
                mCallback.onAnimationDone();
        }
    };

    public interface Callback {
        public void onAnimationDone();
        public void onAnimationStopped();
    }

    public AVDWrapper(Animatable drawable, 
                            Handler handler, Callback callback) {
        mDrawable = drawable;
        mHandler = handler;
        mCallback = callback;
    }

    // Duration of the animation
    public void start(long duration) {
        mDrawable.start();
        mHandler.postDelayed(mAnimationDoneRunnable, duration);
    }

    public void stop() {
        mDrawable.stop();
        mHandler.removeCallbacks(mAnimationDoneRunnable);

        if (mCallback != null)
            mCallback.onAnimationStopped();
    }
}

您code看起来像:

final Drawable drawable = circle.getDrawable();
final Animatable animatable = (Animatable) drawable;

AVDWrapper.Callback callback = new AVDWrapper.Callback() {
        @Override
        public void onAnimationDone() {
            tick.setAlpha(1f);
        }

        @Override
        public void onAnimationStopped() {
          // Okay
        }
    };

AVDWrapper avdw = new AVDWrapper(animatable, mHandler, callback);
//animatable.start();
avdw.start(2000L);

tick.setAlpha(0f);
//tick.animate().alpha(1f).setStartDelay(2000).setDuration(1).start();

// One wrapper is sufficient if the duration is same
final Drawable drawable2 = tick.getDrawable();
final Animatable animatable2 = (Animatable) drawable2;
animatable2.start();

不过,这是你与 setStartDelay 做什么。所以,我不知道怎么用,这将是

But, this is exactly what you are doing with setStartDelay. So I don't know how useful this will be.

编辑:这一切都还可以扩展AnimatedVectorDrawable内部实现。但是,你会完全失去对XML的支持。

All this can also be implemented inside an extended AnimatedVectorDrawable. But, you'll lose xml support altogether.

这篇关于是否有监听的AnimatedVectorDrawables动画结束的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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