Search code examples
androidmultithreadinganimationdrawable

running 2 animationDrawables at the same time


hey everyone hope u can help n thanks 4 looking

here is my code for 2 spinning coins, but only coin1 spins....is it because they both refrence the same xml animation file or something else?

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      
    }

or should i have all the code for coins 1 and 2 in coinSpin1();

any help would be appriciated

previous questions were about only 1 coin....which people have helped but why when i add a second coin and spin 2 at once (not one after the other) the second coin doesnt do anything

should i put all the code in one method?


Solution

  • Here you have code that I have run and tested. I would use perhaps http://developer.android.com/reference/java/util/concurrent/ExecutorService.html instead of Runnable, but this works. My animation file is:

    <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"/>
    

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