【问题标题】:Android -- How to switch views with 3 ImageViews?Android -- 如何使用 3 个 ImageView 切换视图?
【发布时间】:2010-02-25 21:26:24
【问题描述】:

我需要帮助想出一种在 Android 中使用某种类型的视图或布局组合来执行以下序列的方法:

我有 3 个图像对象...比如说对象 A、B 和 C...

[所有对象不可见且分层,一个在另一个之上...A/B/C,就像在相对布局中一样]-->[淡入对象 A]-->[显示 A 200ms]-- >[同时淡出对象A和淡入对象B]-->[显示对象B 200ms]-->[同时淡出对象B和淡入对象C]-->[对象C无限期保留在屏幕上]

我已经尝试过线程、AsyncTasks、处理程序、自定义布局、AnimationListeners 等的所有组合,但我尝试过的一切都失败了。

如果 ViewSwitcher 可以获取超过 2 个视图...请帮助。

瑞恩

【问题讨论】:

    标签: java android multithreading


    【解决方案1】:

    关于线程的主要问题是它们应该允许您执行并行任务,但问题是它们不能保证这些任务在执行时实际看起来“并行度如何”。由于调度程序决定在任何给定时间执行哪个线程,您不能保证您的图像将正确淡入/淡出。虽然您可以使用多线程代码来完成这项任务,但我认为这不是一个好的选择。

    最好的办法是在动画的每一帧上更新两个图像(淡入/淡出)。 A 开始淡出时发出信号,B 拾取该信号,然后开始淡入。您将获得平滑过渡,因为 AB 在每一帧上都得到更新,您将没有任何线程的不确定性。对BC 执行相同的操作。

    更新:你抓住了我! :)
    我以为你会让我只给你一般信息就可以逃脱,但既然你让我走投无路,我别无选择,只能用谷歌搜索:)。好的,所以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>
    

    您必须加载 x​​ml 动画并显示动画,执行以下操作:

     // 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();
    

    好的,所以我知道这是一个肮脏的黑客,但它应该做你想要的:)。如果这对你来说太简单和不酷,那么你可以走原来的路线,试着弄清楚如何逐帧显示你的图像,等等。

    【讨论】:

    • 好吧,这确实有道理。谢谢你。那么,您的建议是创建一个从 View/ImageView/SurfaceView 扩展的类并直接在三个位图上执行 alpha 动画?我将如何控制整个动画的速度或持续时间?如果我将其设置为重复调用 onDraw 或 Invalidate,我将如何控制频率?再次感谢您的回复。
    • @borg17of20 啊...好吧,现在你当场抓住了我:)...我实际上并不熟悉用于动画的 android 库,但幸运的是 google 是我的朋友,我是能够找到一些链接:code.google.com/android/reference/android/view/animation/…developer.android.com/reference/android/graphics/drawable/… 我会更新我的答案以获取更多信息。
    • 您指向 AnimationDrawable 的链接帮助我找到了这个:code.davidjanes.com/blog/2009/11/23/…,看起来很有希望。
    • @borg17of20 RE: David Jones 博客 - 我应该删除我的自证其罪更新吗? LOL David Jones 的博客似乎演示了幻灯片动画,我认为这不会让您在两个图像之间进行交叉淡入淡出……相反,我认为它会使每张图像淡出并淡入以下图像。
    • @Lirik 再次感谢您的回复。好的,据我所知,更新中描述的动画需要 3 个对象 A 的图像和对象 B 的 3 个图像,处于 alpha 通量或褪色的某个阶段(例如,每个 alpha %100、%50 和 0%)。我现在拥有的是三个不同对象的三个图像@100% alpha。为了使上述建议的解决方案发挥作用,我必须从我的源图像创建更多照片。我在这个假设中正确吗?无论如何,我想你已经回答了我最初的问题。我不明白为什么它不起作用,我只需要手动进行 alpha 调整。谢谢。
    【解决方案2】:

    这些东西实际上并不需要线程,它们需要一个动画。更具体地说是 AlphaAnimation。查看Alpha Animation 页面了解如何使用它的详细信息。这很简单,您只需设置您想要发生的事情以及需要多长时间。

    【讨论】:

    • 不幸的是,图像的时间安排非常重要,我无法通过线程或使用内置动画实现方式来正确处理。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2017-07-24
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多