javaandroidobjectanimator

Android Studio Animation Using ObjectAnimation


I've been trying to animate a button but none of the animations work on it. I'm not trying to animate something after something has been clicked. I want to animate it when this function is called. Does anybody know why my button refuses to animate?

private void eliminate() {
    for (int i = 0; i == 0; i++) {
        int randomJ = getRandomNumberInRange(0, 2);
        int randomI = getRandomNumberInRange(0, 2);

        if (!buttons[randomI][randomJ].getText().toString().equals("")) {
            buttons[randomI][randomJ].setText("");

            objectAnimator = ObjectAnimator.ofFloat(buttons[randomI][randomJ], "rotation", 180);
            objectAnimator2 = ObjectAnimator.ofFloat(buttons[randomI][randomJ], "alpha", 1);

            objectAnimator.setDuration(5000);

        } else {
            i -= 1;
        }
    }
}

Solution

  • It seems like you have not added the .start() method to your animation. Below is a simple example I made with a layout that has one TextView element with the id "textview":

     TextView animateTextView = (TextView) findViewById(R.id.textview);
    
     ObjectAnimator textViewAnimator = ObjectAnimator.ofFloat(animateTextView, "translationY",0f,500f);
     textViewAnimator.setDuration(2000);
     textViewAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
     textViewAnimator.start();
    

    You can read more in the documentation here or see a good example here