Reputation: 3692
I have a Relative Layout
, which contains some views that correspond to some metadata on an image. I have onTapListener for the image which works like this:
image.setOnPhotoTapListener(new OnPhotoTapListener() {
@Override
public void onPhotoTap(View view, float x, float y) {
if (visible) {
//fade-out the contents of relative layout
visible = false;
} else {
//fade-in the contents of relative layout
visible = true;
}
}
});
What I want is, to fade-out the contents of relative layout if it is visible or fade-in the contents if it is not visible. I basically want to use alpha animation for these effects.
How do I write the animation function for the two cases? I am very new to Android animations, so any help would be highly appreciated.
Upvotes: 1
Views: 591
Reputation: 36035
You merely have to perform the animation on the entire View.
If you're supporting at least API 11 then it's even easier because you get to use ObjectAnimators (If you're supporting 10 or less than you can use NineOldAndroids).
For Alpha animations, you first get your root view, then create an ObjectAnimator that points to the "alpha" property.
View root = findViewById(R.id.rootId);
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(root, "alpha", 0f, 1f);
fadeIn.setDuration(500);
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(root, "alpha", 1f, 0f);
fadeOut.setDuration(500);
// Whenever you want to fade in.
if (fadeOut.isRunning()) {
fadeOut.cancel(); // Cancel the opposite animation if it is running or else you get funky looks
}
fadeIn.start();
// Whenever you want to fade out.
if (fadeIn.isRunning()) {
fadeIn.cancel();
}
fadeOut.start();
fadeIn
will animate the target (root) from 0 to 1 for 500 milliseconds. fadeOut
will animate the target from 1 to 0 for 500 milliseconds.
Upvotes: 2