Remove or hide Android AnimationDrawable when animation ends - android

I'm creating a trivia game which plays a simple two frame animation before the answer is revealed. The animation fills the screen so that the answer isn't revealed until the animation is done. Everything works except that I can't get the animation to go away and reveal the answer. What I'm trying to do is call the Relative Layout that the animation ImageView is in and then do ImageView_layout.setVisibility(View.GONE);. I'm not getting any errors, but the relative layout ain't going away. Here's what I've got:
Animation CorrAniShow = AnimationUtils.loadAnimation(this, R.anim.anim_fadein);
Animation CorrAniHide = AnimationUtils.loadAnimation(this, R.anim.anim_fadeout);
final RelativeLayout corrA_layout = (RelativeLayout) findViewById(R.id.aniC_Layout);
final ImageView corrView = (ImageView) findViewById(R.id.corAn1);
corrView.setBackgroundResource(R.anim.corr_anim);
corrView.post(new Runnable() {
#Override
public void run() {
AnimationDrawable corAn = (AnimationDrawable)corrView.getBackground();
corAn.start();
}
});
CorrAniHide.setAnimationListener(new AnimationListener() {
public void onAnimationStart(final Animation arg0) {}
public void onAnimationRepeat(final Animation arg0) {}
public void onAnimationEnd(final Animation rg0){
corrA_layout.setVisibility(View.GONE);
}
});
Can anyone see why the view isn't gone? Is there a better way to make the animation go away? Thanks!

Related

How to animate an ImageButton into invisibility

This is a follow up to Animate ImageView from alpha 0 to 1.
I have an ImageButton that I want to animate into view when user does X and then animate out of view when user does Y. Animating into view is working fine. But animating out of view is kind of not working. In fact it is working, in that the animation happens. But after the button fades out of view, it pops back as VISIBLE. So I use this code
AlphaAnimation animation1 = new AlphaAnimation(0.9f, 0.0f);
animation1.setDuration(5000);
animation1.setStartOffset(1000);
animation1.setFillAfter(true);
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationStart: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationRepeat(Animation animation) {
GVLog.d(TAG,"MAKE INVISIBLE onAnimationRepeat: %s",tokenBtn.getVisibility());
}
#Override
public void onAnimationEnd(Animation animation) {
tokenBtn.setVisibility(View.INVISIBLE);
GVLog.d(TAG,"MAKE INVISIBLE onAnimationEnd: %s",tokenBtn.getVisibility());
}
});
tokenBtn.startAnimation(animation1);
but the call to make the ImageButton invisible is not taking effect. So how do I make it take effect so that the ImageButton remains invisible?
Try like this. These codes always work on my apps.
AlphaAnimation animation1 = new AlphaAnimation(1, 0);
animation1.setDuration(1000);
tokenBtn.startAnimation(animation1);
tokenBtn.setVisibility(View.INVISIBLE);

wait for animation to complete before hiding View

I perform some things triggered by a Button click:
private void onSearchPressed() {
title.setVisibility(View.GONE);
etActionSearch.setVisibility(View.VISIBLE);
etActionSearch.requestFocus();
btnActionFavs.setVisibility(View.GONE);
etActionSearch.setAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.et_anim_open));
isSearch = true;
}
So basically I just hide some Views and show some others, my EditText is "sliding out" using a simple set Animation.
When the action is cancelled, I reverse the process:
private void onSearchCancelled() {
etActionSearch.setAnimation(AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.et_anim_close));
etActionSearch.setVisibility(View.GONE);
btnActionFavs.setVisibility(View.VISIBLE);
title.setVisibility(View.VISIBLE);
isSearch = false;
}
What I'd like to do is to apply an Animation (opposite direction) to my EditText, so it disappears also with a slide animation. The problem is that all the code is executed immediately, so the EditText is gone BEFORE its animation is complete. I tried some weird things like using an AsyncTask and putting the animation inside the doInBackground() method, setting the Visibility of the Views in onPostExecute() but that didnt change anything.. SystemClock.sleep() also doesn't do anything but a lag impression. Any solutions?
Animations run asynchronously, so you need to use AnimationListener on that animation and put your code that should be executed when animation is over to onAnimationEnd().
See docs: http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
Like Marcin Orlowski said, you have to use AnimationListener with your Animation. After the animation is ended it will launch the onAnimationEnd() event where you can do your stuff.
Here's a little example what i could look like:
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.your_specific_animation);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// Do your hiding stuff here
}
});
etActionSearch.startAnimation(animation);

Android How can i add some delay on multiple animations?

What i want is that each animation, from the code below, to start with a delay, like a sequence. So i have this code:
public void setAnimation(){
View view;
String animation = prefs.getString("animations", "Scale");
String interpolator = prefs.getString("interpolators", "Bounce");
Animation animate = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_in);
for(int i=0; i<gridView.getChildCount(); i++){
view = gridView.getChildAt(i);
view.startAnimation(animate);
}
}
because there is a for loop, all child animations will start instantly. I already tried with:
Thread.sleep....
Handler...
animate.setStartTime...
animate.setStartOffset...
but all child animations starts instantly.
I tried this method inside of loop, and the animations won't start:
animate.setAnimationListener(new AnimationListener(){
public void onAnimationEnd(Animation arg0) {
view.startAnimation(animate);
}
public void onAnimationRepeat(Animation arg0) {
}
public void onAnimationStart(Animation arg0) {
}
});
Thanx in advance.
The solution is to create a GridLayoutAnimationController or LayoutAnimationController.
I used LayoutAnimationController to show elements in LinearLayout with animation effect one by one using following code.
Animation fadeIn = AnimationUtils.loadAnimation(context, R.anim.anim_fade_in);
//lnrContactContainer is LinearLayout.
AnimationSet set = new AnimationSet(true);
set.addAnimation(fadeIn);
set.setDuration(500);
controller = new LayoutAnimationController(set, 1f);
lnrContactContainer.setLayoutAnimation(controller);
lnrContactContainer.setVisibility(View.VISIBLE);
But same approach does not work when I use it to show fadeout animation while hiding LinearLayout lnrContactContainer.setVisibility(View.GONE);

Android animation one after another

Here is my scenario.
At first I do an animation set to my view (View.startAnimation(animationSet)) where animationSet consists of Translate + Rotate + Scale all at the same time. It works fine.
on that animationSet I have fillAfter(true).
After some time user click on a button and onClick I must start new ScaleAnimation on that same View. So if I do something like:
#Override
public void onClick(View v) {
ScaleAnimation scaleAnimation = new ScaleAnimation(mOldScaleFactor, mScaleFactor, mOldScaleFactor, mScaleFactor, mPivotX, mPivotY);
scaleAnimation.setDuration(ANIMATION_DURATION_MILIS);
scaleAnimation.setStartOffset(ANIMATION_OFFSET_MILIS);
scaleAnimation.setFillAfter(true);
v.startAnimation(scaleAnimation);
}
All the previous animation (Translate + Rotete + Scale) is forgotten.
How to start new animation from where old animation ends?
You could try to implement a listener and possible capture the information from when the previous animation left off, however I'm not sure you'll be able to query the animation for its state. Another option is to make sure the animation stops into a known state. That way you can know how you need to start the next animation.
public float param1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
//load text view to add animation
ImageView image1 = (ImageView) findViewById(R.id.splash_imageView1);
Animation fade1 = AnimationUtils.loadAnimation(this, R.anim.fade_anim);
image1.startAnimation(fade1);
fade1.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
//interrogate animation here
}

Android: PopupWindow Animation - Need to know when the animation has ended

I've got a PopupWindow that's using an alpha animation to produce a fade-in display of the window.
Using PopupWindow.setAnimationStyle() works as expected: the popup fades-in when shown.
However, once the popup is displayed (meaning the fade-in animation has completed) I'd like to kick off another animation.
I've tried using the following to obtain the underlying animation that's referred to via setAnimationStyle() and attach an AnimationListener to it:
Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.popup_fade_in);
fadeInAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Log.d(TAG, "fade-in animation START");
}
#Override
public void onAnimationEnd(Animation animation) {
Log.d(TAG, "fade-in animation END");
// Kick off the next animation
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
This does't work: None of the AnimationListener methods ever get called.
Anyone know of a way to determine when a popup window's animation has ended?
Alternatively, if there's a way to determine when the popup window has initially become visible I could kick off the secondary animation at that time. Unfortunately, I'm not finding anything in the API docs indicating how to do this.
Appreciate the help!

Categories

Resources