【问题标题】:Add transition to an AnimationDrawable向 AnimationDrawable 添加过渡
【发布时间】:2012-01-27 12:03:47
【问题描述】:

我有一组 10 张图像,我想创建一个动画,在它们之间进行淡入淡出。我一直在研究内置的 Drawable 来实现这样的事情,但在那方面没有运气。 有 AnimationDrawable,可以在图片之间切换,但它不会为切换设置动画。 有TransitionDrawable,在两张图片之间交叉淡入淡出,但不超过两张。

地狱。

我在 Google 上寻找了一些解决方案,但没有运气。所以我正在考虑实现我自己的drawable来实现这样的事情。大家有什么指点吗?

提前致谢。

【问题讨论】:

    标签: android animation drawable


    【解决方案1】:

    不确定您是否找到了答案,但我遇到了同样的问题,最终基于 TransitionDrawable 构建了我自己的类。

    用法:

    CyclicTransitionDrawable ctd = new CyclicTransitionDrawable(new Drawable[] { 
      drawable1, 
      drawable2, 
      drawable3, 
      ... 
    });
    
    imageView.setImageDrawable(ctd);
    
    ctd.startTransition(1000, 3000) // 1 second transition, 3 second pause between transitions.
    

    CyclicTransitionDrawable 的代码是available on Github

    【讨论】:

    • 好吧,我什至不记得我做了什么 :-) 但是您的解决方案看起来不错而且很干净,所以让我们将其标记为答案!
    • 有没有办法让它只循环一次?
    • 目前无法限制循环次数。虽然可以添加,但我认为(从记忆中)您可以使用原始的 TransitionDrawable 类来实现这一点。
    【解决方案2】:

    嗯。很长一段时间过去了,您可能已经解决了这个问题,但是您得到了 AnimationDrawable 的 setEnterFaceDuration()。示例:

    mBackgroundAnimation = new AnimationDrawable();
    mBackgroundAnimation.addFrame(getResources().getDrawable(R.drawable.background1), 5000); 
    // ... rest of the frames
    mBackgroundAnimation.addFrame(getResources().getDrawable(R.drawable.background6), 5000);
    mBackgroundAnimation.setEnterFadeDuration(1000);
    mBackgroundAnimation.setOneShot(false);
    

    使用此代码,您可以轻松地循环浏览 1..N 个图像,每个图像保持 5 秒(5000 毫秒)并带有淡入动画。现在,我要做的是设置我的根 RelativeLayout 的背景

    mLayoutRoot.setBackground(mBackgroundAnimation);
    mLayoutRoot.post(new AnimationStarterThread());
    

    还有 AnimationStarterThread 类

    private class AnimationStarterThread implements Runnable {
        public void run() {
            if(mBackgroundAnimation != null)
                 mBackgroundAnimation.start();
        }
    }
    

    【讨论】:

    • AnimationDrawable 没有方法setEnterFadeDuration
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-12
    • 2019-04-04
    • 2017-11-11
    相关资源
    最近更新 更多