为AnimationDrawable添加过渡效果

12

我有10张图片,想要制作一个动画,在图片之间进行交叉淡入淡出。我研究了内置Drawable来实现这样的效果,但是没有成功。

有AnimationDrawable,可以在几张图片之间切换,但是不能动画切换。

还有TransitionDrawable,可以在两张图片之间进行交叉淡入淡出,但是只能用于两张图片。

我在Google上寻找解决方案,但是没有找到。所以我想自己实现一个drawable来实现这样的效果。你们有什么建议吗?

提前感谢。

2个回答

22

不确定你是否已经找到了答案,但我曾经遇到过同样的问题,最终基于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的代码可在Github上获取


我甚至不记得我做了什么 :-) 但是你的解决方案看起来很好,很干净,所以让我们把它标记为“答案”! - Redwarp
有没有办法让它只执行一次循环? - Andy
目前还没有限制循环次数的方法。虽然可以添加,但我认为(从记忆中)可以使用原始的TransitionDrawable类来实现这一点。 - Chris Blunt

14

好的。时间过去很久了,你可能已经解决了这个问题,但你可以使用setEnterFadeDuration()方法来设置AnimationDrawable的淡入持续时间。例如:

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方法。 - Rafael Lima

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接