Более одного AnimationDrawable одновременно

Я хотел бы запускать разные анимации одновременно с запуском приложения. Я добавил свой код в функцию 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();  
      }
  }

person Zappescu    schedule 19.05.2011    source источник


Ответы (1)


Ну наконец-то я понял! В коде была ошибка, какая хрень! Это хороший:

  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, как описано выше.

Надеюсь, поможет! :)

person Zappescu    schedule 25.05.2011