在同一时间运行2 animationDrawables [英] running 2 animationDrawables at the same time

查看:212
本文介绍了在同一时间运行2 animationDrawables的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好希望你能帮助ñ感谢4寻找

hey everyone hope u can help n thanks 4 looking

这是我的code 2纺纱硬币,但只有COIN1旋转....难道是因为他们都refrence相同的XML动画文件或其他什么东西?

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      
    }

或者我应该拥有所有的code硬币1和2 coinSpin1();

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

任何帮助将appriciated

any help would be appriciated

previous问题是约只有1硬币....哪些人帮助,但为什么当我添加第二个硬币和旋转2一次(而不是一前一后)第二硬币没有做任何事情。

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

我应该把所有的code在一个方法?

should i put all the code in one method?

推荐答案

在这里,你有我有运行和测试code。我会用也许 http://developer.android.com/reference/java /util/concurrent/ExecutorService.html 而不是Runnable,但这个工程。我的动画文件是:

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

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

这篇关于在同一时间运行2 animationDrawables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆