同时运行2个animationDrawables

时间:2014-03-11 09:57:19

标签: android multithreading animationdrawable

嘿大家都希望你能帮助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的所有代码

任何帮助都是适当的

以前的问题只涉及一个硬币....人们已经帮助但为什么当我添加第二枚硬币并一次旋转2(而不是一个接一个)时,第二枚硬币没有做任何事情

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

2 个答案:

答案 0 :(得分: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 class:

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

答案 1 :(得分:0)

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

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