关于线程的主要问题是它们应该允许您执行并行任务,但问题是它们不能保证这些任务在执行时实际看起来“并行度如何”。由于调度程序决定在任何给定时间执行哪个线程,您不能保证您的图像将正确淡入/淡出。虽然您可以使用多线程代码来完成这项任务,但我认为这不是一个好的选择。
最好的办法是在动画的每一帧上更新两个图像(淡入/淡出)。 A 开始淡出时发出信号,B 拾取该信号,然后开始淡入。您将获得平滑过渡,因为 A 和 B 在每一帧上都得到更新,您将没有任何线程的不确定性。对B 和C 执行相同的操作。
更新:你抓住了我! :)
我以为你会让我只给你一般信息就可以逃脱,但既然你让我走投无路,我别无选择,只能用谷歌搜索:)。好的,所以android库有一些animation classes,该库还为您提供了一种可以实际执行frame-by-frame animation的方式。
我会给你一个简单的技巧,但我确信有更好的方法来做到这一点:
动画拍摄了几张应该在一定时间内显示的图像,因此您所要做的就是交替使用这些图像。
<!-- Animation frames are AfadeOut01.png to AfadeOut03.png and BfadeIn01.png to BfadeIn03.png files inside the res/drawable/ folder, -->
<animation-list android:id="selected" android:oneshot="true">
<item android:drawable="@drawable/AfadeOut01" android:duration="50" />
<item android:drawable="@drawable/BfadeIn01" android:duration="50" />
<item android:drawable="@drawable/AfadeOut02" android:duration="50" />
<item android:drawable="@drawable/BfadeIn02" android:duration="50" />
<item android:drawable="@drawable/AfadeOut03" android:duration="50" />
<item android:drawable="@drawable/BfadeIn03" android:duration="50" />
</animation-list>
您必须加载 xml 动画并显示动画,执行以下操作:
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(/*resourceImageID e.g. AfadeOut03*/);
img.setBackgroundResource(/*backgroundResource*/);
// note that this loads the resource from an XML file, but
// instead of getting the resource from file you can generate
// it from a single image by performing the required modifications
// of the image and storing them in a resource.
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation
frameAnimation.setOneShot(true);// don't loop if not set in XML
frameAnimation.start();
好的,所以我知道这是一个肮脏的黑客,但它应该做你想要的:)。如果这对你来说太简单和不酷,那么你可以走原来的路线,试着弄清楚如何逐帧显示你的图像,等等。