0

嘿大家希望你能帮助 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 个(不是一个接一个)时,第二个硬币没有做任何事情

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

4

2 回答 2

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();
}
}
于 2014-03-11T19:41:33.983 回答
0

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

coinAnima2 = (ImageView) findViewById(R.id.imageView2);
于 2014-03-11T10:08:05.187 回答