【问题标题】:running 2 animationDrawables at the same time同时运行2个animationDrawables
【发布时间】:2014-03-11 09:57:19
【问题描述】:

大家好,希望你能帮助n谢谢4看

这是我的 2 个旋转硬币的代码,但只有 coin1 旋转....是因为它们都引用了相同的 xml 动画文件还是其他什么?

public class MainActivity extends Activity {
    static AnimationDrawable frameAnimation;
    static AnimationDrawable frameAnimation2;
    ImageView coinAnima2;
    ImageView coinAnima;


    public boolean currentSpin = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        coinAnima = (ImageView) findViewById(R.id.imageView1);
        coinAnima2 = (ImageView) findViewById(R.id.imageView2);


        Button bt = (Button) findViewById(R.id.button1);
          bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //spinCoin();

            spinCoin1();
            spinCoin2();
            }
        });

    }
    //end of onCreate

    public void spinCoin1(){
        coinAnima = (ImageView) findViewById(R.id.imageView1);
        coinAnima.setBackgroundResource(R.anim.coin_spin_heads); 

            new Thread(new Runnable() {
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    frameAnimation = (AnimationDrawable) coinAnima.getBackground();
                                    frameAnimation.start();
                                }
                         });


                            try {
                                Thread.sleep(5000);
                              } catch (InterruptedException e) {
                                e.printStackTrace();
                              }    

                              frameAnimation.stop();

                        //end of run
                        }

            //starts the thread        
             }).start();

    //end of method      
    }

    public void spinCoin2(){
        coinAnima2 = (ImageView) findViewById(R.id.imageView2);
        coinAnima2.setBackgroundResource(R.anim.coin_spin_heads); 

            new Thread(new Runnable() {
                        public void run() {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    frameAnimation2 = (AnimationDrawable) coinAnima2.getBackground();
                                    frameAnimation2.start();
                                }
                         });


                            try {
                                Thread.sleep(5000);
                              } catch (InterruptedException e) {
                                e.printStackTrace();
                              }    

                              frameAnimation2.stop();

                        //end of run
                        }

            //starts the thread        
             }).start();

    //end of method      
    }


    //end of class      
    }

或者我应该在 coinSpin1() 中拥有硬币 1 和 2 的所有代码;

任何帮助都会得到帮助

以前的问题是关于只有 1 个硬币....人们提供了帮助,但是为什么当我添加第二个硬币并一次旋转 2 个时(不是一个接一个),第二个硬币没有任何作用

我应该将所有代码放在一个方法中吗?

【问题讨论】:

    标签: android multithreading animationdrawable


    【解决方案1】:

    这里有我已经运行和测试过的代码。我可能会使用 http://developer.android.com/reference/java/util/concurrent/ExecutorService.html 而不是 Runnable,但这有效。我的动画文件是:

    <animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
    android:id="@+id/selected"
    android:oneshot="false" >
    <item
        android:drawable="@drawable/wheel_10"
        android:duration="50"/>
    <item
        android:drawable="@drawable/wheel_11"
        android:duration="50"/>
    <item
        android:drawable="@drawable/wheel_12"
        android:duration="50"/>
    <item
        android:drawable="@drawable/wheel_13"
        android:duration="50"/>
    <item
        android:drawable="@drawable/wheel_14"
        android:duration="50"/>
    <item
        android:drawable="@drawable/wheel_15"
        android:duration="50"/>
    

    和java类:

    public class Coin extends Activity {
        static AnimationDrawable frameAnimation;
        static AnimationDrawable frameAnimation2;
        ImageView coinAnima2;
        ImageView coinAnima;
    
        public boolean currentSpin = false;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.coin);
                coinAnima = (ImageView) findViewById(R.id.imageView1);
                coinAnima2 = (ImageView) findViewById(R.id.imageView2);
                Button bt = (Button) findViewById(R.id.button1);
                bt.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    spinCoin1();
               }
            });
       }
    
    public void spinCoin1() {
        coinAnima = (ImageView) findViewById(R.id.imageView1);
        coinAnima.setBackgroundResource(R.animator.coinanim);
        coinAnima2 = (ImageView) findViewById(R.id.imageView2);
        coinAnima2.setBackgroundResource(R.animator.coinanim);
    
        new Thread(new Runnable() {
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        frameAnimation = (AnimationDrawable) coinAnima.getBackground();
                        frameAnimation.start();
    
                        frameAnimation2 = (AnimationDrawable) coinAnima2.getBackground();
                        frameAnimation2.start();
                    }
                });
    
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                }
    
                frameAnimation.stop();
                frameAnimation2.stop();
            }
        }).start();
    }
    }
    

    【讨论】:

    • vote 1 von,这正是我的想法……我在 coinSpin1() 和 coinSpin2() 或 coinSpin() 中的 botih 之间折腾,但从两个单独开始.. ..感谢您的帮助,我将它们放在一起,现在可以正常工作了...如果您想查看最终结果,请转到 play.google.com/store/apps/details?id=game.data 再次感谢 mate vote 1 @冯
    【解决方案2】:

    你们都在同一个视图上实例化 coinAnima (R.id.imageView1) 也许 coniAnima2 应该得到 imageView2?

    coinAnima2 = (ImageView) findViewById(R.id.imageView2);
    

    【讨论】:

    • 抱歉打错了,不是我复制时的问题,我注意到它并在eclipse中更改了它但忘了在这里....对不起大家
    • 您可以实例化同一个视图,但使用两个不同的变量来保存引用。我会将我所有的动画代码放在同一个线程中(在你的情况下,相同的方法)。今晚晚些时候我会尝试给出一个代码示例,除非你到那时已经解决了。
    • @Von 你帮了大忙,谢谢队友!我还在学习,但谢谢你...别担心 atm 但明天我会尝试发布我的修复...如果你愿意,那就来看看吧。我想我可能在使用 coinSpin1() 和 coinpin2() 时出错了。我试着把它们放在一起,看看它是如何运行的
    • android:background="@drawable/coinheads" android:src="@drawable/cointails" 这是它必须设置为 android:background 而不是 android:src 的问题。当我第一次运行代码时我很喜欢这一点,但在我检查了一次 cpl 的 xml 布局中一定错过了它
    猜你喜欢
    • 2010-10-20
    • 1970-01-01
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多