Android 可同时绘制多个动画

Android 可同时绘制多个动画,android,animation,drawable,Android,Animation,Drawable,我想在应用程序启动时同时启动不同的动画。我在onWindowFocusChanged函数中为一个动画按钮添加了代码,没有问题。但是,当我尝试设置另一个按钮的动画时,第二个按钮不会移动,固定在第一帧上。 有人知道我该怎么过去吗 一些代码: 在OnCreate中: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(

我想在应用程序启动时同时启动不同的动画。我在onWindowFocusChanged函数中为一个动画按钮添加了代码,没有问题。但是,当我尝试设置另一个按钮的动画时,第二个按钮不会移动,固定在第一帧上。 有人知道我该怎么过去吗

一些代码:

在OnCreate中:

public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);      
      setContentView(R.layout.main);
      Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
      mButtonPlay.setBackgroundResource(R.drawable.button_play);      
      buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
      Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
      mButtonOptions.setBackgroundResource(R.drawable.button_options);
      buttonAnimationOptions = (AnimationDrawable)mButtonPlay.getBackground();
  }
enter code here
在onWindowFocusChanged中:

@Override
  public void onWindowFocusChanged(boolean hasFocus) 
  {
      if (hasFocus)
      {
    buttonAnimationPlay.start();  
    buttonAnimationOptions.start();  
      }
      else
      {   
    buttonAnimationPlay.stop();  
    buttonAnimationOptions.stop();  
      }
  }

我终于明白了!代码中有个错误,真糟糕!这是个好主意:

  final Button mButtonPlay = (Button) findViewById(R.id.ButtonPlay);
  mButtonPlay.setBackgroundResource(R.drawable.button_play);
  buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();


  final Button mButtonOptions = (Button) findViewById(R.id.ButtonOptions);
  mButtonOptions.setBackgroundResource(R.drawable.button_play);
  buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
无论如何,我也尝试过以编程方式添加视图,现在也可以了。任何人都会使用其中一种,这是另一种:

RelativeLayout Relative = (RelativeLayout) findViewById(R.id.RelativeForButtons);
  RelativeLayout.LayoutParams relativeParamsPlay = 
      new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  RelativeLayout.LayoutParams relativeParamsOptions = 
      new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

  relativeParamsPlay.addRule(RelativeLayout.BELOW,R.id.Title);
  relativeParamsPlay.addRule(RelativeLayout.CENTER_IN_PARENT);
  final Button mButtonPlay = new Button(this);
  mButtonPlay.setBackgroundResource(R.drawable.button_play);
  buttonAnimationPlay = (AnimationDrawable)mButtonPlay.getBackground();
  mButtonPlay.setId(1);
  mButtonPlay.setText("PLAY");
  Relative.addView(mButtonPlay,relativeParamsPlay);   

  final Button mButtonOptions = new Button(this);
  mButtonOptions.setBackgroundResource(R.drawable.button_options);
  buttonAnimationOptions = (AnimationDrawable)mButtonOptions.getBackground();
  mButtonOptions.setId(2);
  mButtonOptions.setText("OPTIONS");
  relativeParamsOptions.addRule(RelativeLayout.BELOW, mButtonPlay.getId());
  relativeParamsOptions.addRule(RelativeLayout.CENTER_IN_PARENT);
  Relative.addView(mButtonOptions,relativeParamsOptions);   
所有这些都在OnCreate函数中。如果要启动动画,请使用如上所述的onWindowFocusChanged函数

希望有帮助!:)