0

У меня есть кнопка и 2 анимации. Как сделать что бы они поочереди активировались при нажатие на одну и туже кнопку?

public class Main5Activity extends AppCompatActivity
{
    ImageView myImageView_1;
    Button btnRotate;
    RotateAnimation rotate2;
    RotateAnimation rotate1;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        myImageView_1 = findViewById(R.id.imageView_1);
        btnRotate = findViewById(R.id.button);

        final Animation animationRotateCenter1 = AnimationUtils.loadAnimation(this, R.anim.rotate_1);
        animationRotateCenter1.setFillAfter(true);

        final Animation animationRotateCenter2 = AnimationUtils.loadAnimation(this, R.anim.rotate_2);
        animationRotateCenter2.setFillAfter(true);

        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
            }
        });

    }
}

2 ответа 2

2

Например можно сделать переменную bool которая при проигрывании первой анимации будет становится true:

boolean firstPlayed= false;

берем нажатие на кнопку:

btnRotate.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v)
   {
     if(!firstPlayed) {
       firstPlayed = true
       // играем первую анимацию
      }else{
        firstPlayed = false
        // играем вторую анимацию
      }     
   }
});
1
  • Спасибо, я всю ночь искал как это сделать но знал как правильно построить)
    – Karpuha
    30 мая 2020 в 14:31
1

Как то так примерно.

public class Main5Activity extends AppCompatActivity{
    ImageView myImageView_1;
    Button btnRotate;
    RotateAnimation rotate2;
    RotateAnimation rotate1;
    boolean isFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main5);

        myImageView_1 = findViewById(R.id.imageView_1);
        btnRotate = findViewById(R.id.button);

        rotate1 = AnimationUtils.loadAnimation(this, R.anim.rotate_1);
        rotate1.setFillAfter(true);

        rotate2 = AnimationUtils.loadAnimation(this, R.anim.rotate_2);
        rotate2.setFillAfter(true);

        btnRotate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v){
                isFirst = !isFirst;
                view.startAnimation((isFirst)? rotate1: rotate2);
            }
        });

    }
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.