我有10张图片,想要制作一个动画,在图片之间进行交叉淡入淡出。我研究了内置Drawable来实现这样的效果,但是没有成功。
有AnimationDrawable,可以在几张图片之间切换,但是不能动画切换。
还有TransitionDrawable,可以在两张图片之间进行交叉淡入淡出,但是只能用于两张图片。
我在Google上寻找解决方案,但是没有找到。所以我想自己实现一个drawable来实现这样的效果。你们有什么建议吗?
提前感谢。
不确定你是否已经找到了答案,但我曾经遇到过同样的问题,最终基于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上获取。
好的。时间过去很久了,你可能已经解决了这个问题,但你可以使用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();
}
}
setEnterFadeDuration
方法。 - Rafael Lima