running 2 animationDrawables at the same time

陌路散爱 提交于 2019-12-12 04:43:46

问题


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?


回答1:


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



回答2:


Both of you coinAnima get instantiated on the same view (R.id.imageView1) Maybe coniAnima2 should get imageView2?

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


来源:https://stackoverflow.com/questions/22322200/running-2-animationdrawables-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!