java - 如何仅在带有 ImageView 小部件的 xml 动画期间使用不同的可绘制对象 (android)

标签 java android xml animation

如何使 ImageView 仅对动画使用特定的可绘制对象,然后在动画完成后恢复到之前的可绘制对象。我的尝试如下,但它不起作用,因为它在动画期间从未显示“alt_die_”可绘制对象。在动画期间和之后显示的唯一可绘制对象是常规的“die_”可绘制对象。注意:我是 android studios 的新手,所以如果我没有使用正确的术语,我很抱歉。

        String result;
        AnimationSet down_rotate  = new AnimationSet(false);
        AnimationSet up_rotate    = new AnimationSet(false);  

        // Fill the dice widgets with the images to be show during animation 
        for (int index = 0; index < 3; index++) {

            String image_name = "alt_die_" + dice_values.get(index) + ".png";
            try {
                InputStream stream = getAssets().open(image_name);
                Drawable d = Drawable.createFromStream(stream, null);
                dice_images.get(index).setImageDrawable(d);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // Animate the dice by rolling and bouncing the dice
        down_rotate.addAnimation  (bounce_down());
        down_rotate.addAnimation  (rotate());
        die_image_3.startAnimation(down_rotate);
        die_image_2.startAnimation(down_rotate);
        up_rotate.addAnimation    (bounce_up());
        up_rotate.addAnimation    (rotate());
        die_image_1.startAnimation(up_rotate);

        // Fill the dice widgets with the images to be shown after the animation            
        for (int index = 0; index < 3; index++) {   
            String image_name = "die_" + dice_values.get(index) + ".png";
            try {
                InputStream stream = getAssets().open(image_name);
                Drawable d = Drawable.createFromStream(stream, null);
                dice_images.get(index).setImageDrawable(d);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }            

最佳答案

我无法在移动设备上格式化代码,但我有一个想法,

将上面代码中的第一个循环放在方法中,例如

Public void showDefaultDrawable () {

for () {
show alt_die
 }

 }

在第二个循环中创建一个 if 来捕获循环的结尾并调用 showDefaultDrawable 方法,例如

for () {
show die
if (index == 3) {
showDefaultDrawable ();
}
}

然后你可以在需要的时候调用默认方法

关于java - 如何仅在带有 ImageView 小部件的 xml 动画期间使用不同的可绘制对象 (android),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44619514/

相关文章:

java - 使用 Liferay 和 Eclipse 创建插件时出错

java - 安卓半径按钮

java - 提取后台链接,jsoup

java - Twitter linkify android 所有@mentions

android - 折叠工具栏布局 |滚动和布局问题 2

xml - 我们不能在 XML 中放入未转义的 & 符号是有原因的吗

java - 无法使用 Selenium Java 中的 sendKeys() 将文件上传到 "browse"按钮

android - Android 应用程序的推送通知服务未在 Google Play 上发布

xml - HDP 沙盒上的 Hive ADD JAR

c# - 如何使用 C# 将 XML 文件转换为 CSV 文件