Programmer Dude
Programmer Dude

Reputation: 497

Trying To Animate an Image's Alpha

I'm building the design of an app I'm developing, and I'm trying to get an image to fade into the screen. However, the image remains invisible while it translates successfully. This is the code:

facebookLoginButton = (ImageView)findViewById(R.id.facebookLoginButton); // Start of facebookLoginButton code
facebookLoginButton.setTranslationY(250f);
facebookLoginButton.setImageAlpha(0);
facebookLoginButton.animate().translationYBy(-250f).alpha(1).setDuration(1500); // End of facebookLoginButton code

I know that the image moves successfully because when I remove the facebookLoginButton.setImageAlpha(0);, I see the image moving into the screen. How come the image stays invisible?

Note: There's no functionality to the app yet, which is why my button is an ImageView.

Upvotes: 0

Views: 408

Answers (1)

Bryan
Bryan

Reputation: 15145

The ViewPropertyAnimator returned by View.animate() follows the builder pattern. Each of the methods return the pending ViewPropertyAnimator, you must call start() to activate the animation.

Edit: The alpha is also not animating because setImageAlpha() sets the alpha value for the image within the View, not the View itself. While ViewPropertyAnimator animates the alpha of the View, not the image within the View. Although ImageView.setAlpha(int alpha) is deprecated, View.setAlpha(float alpha) is not, and you must use this method to set the alpha for the View. Then ViewPropertyAnimator can animate that value:

facebookLoginButton.setTranslation(250F);
facebookLoginButton.setAlpha(0.0F);
facebookLoginButton.animate()
            .translationYBy(-250F)
            .alpha(1.0F)
            .setDuration(1500)
            .start();

Upvotes: 2

Related Questions