Wait for animation end - android

I've an activity that loads the resources and shows the message "Please wait, loading", and when all resources are loader, the textview changes to "Ready" and change the views of the activity. The problem is that I want to wait for the animation end, but the views are removed even before they start. How can I wait for that?
tview1.startAnimation(desvanecer); tview2.startAnimation(desvanecer);
tview1.setText(R.string.ready);
tview2.setText(R.string.launcher_continue);
tview1.startAnimation(aparecer);
tview2.startAnimation(aparecer);
layout = (RelativeLayout) findViewById(R.id.launcher_layout);
screen = new Screen(context);
thread = new Thread(screen);
layout.removeAllViews();
layout.addView(screen);
thread.start();

You can perform your removals at the end of the animation by using an animation listener and doing them onAnimationEnd
desvanecer.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//REMOVAL OR OTHER CODE HERE
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});

What you need is an AnimationListener and implement the onAnimationEnd method. But have a look at my comment and the doubts I have about your code.

Related

How to clear Animation Listeners set by NineOldAndroids?

I am trying to have a view animated in my app and am using NineOldAndroid for animations.
The desired effect is to fade the view out and then set it's visibility to gone so that it doesn't get clicked while invisible. Here is How I do it.
ViewPropertyAnimator.animate(view).alpha(0).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
The problem here is that the listener above sticks with the view and when I try to fade it in again the listener gets called again resulting the view to be GONE upon appearing.
ViewPropertyAnimator.animate(enterGallery).alpha(1);
How can I clear the listener after the view visibility is set to GONE in the first piece of code?
I found the solution and it would be to pass null as listener when making the view VISIBLE.
ViewPropertyAnimator.animate(view).alpha(1).setListener(null);

How to make a View blink after translation using Animation objects?

I want to make 2 animations on one View, with the second one starting after the first one. The first one is translating and I use TranslateAnimation for that purpose and the second one is AlphaAnimation as I want the View to start blinking after translation. The problem is no matter what I'm doing these animations start simultaneously. As far as I'm concerned I should use AnimationSet object for this purpose but how to do this exactly? I even tried to use setStartTime() method to use some crazy value for the AlphaAnimation before putting it into AnimationSet but still the animations start immediately and simultaneously. So what should I do to prevent it from happening?
PS Here is what I tried to do and what didn't work:
TextView cursor = (TextView) findViewById(R.id.cursor);
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
AnimationSet s = new AnimationSet(false);
s.addAnimation(tAnim);
s.addAnimation(bAnim);
cursor.startAnimation(s);
you can use this
TextView cursor = (TextView) findViewById(R.id.cursor);
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
tAnim.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation arg0) { }
#Override
public void onAnimationRepeat(Animation arg0) { }
#Override
public void onAnimationEnd(Animation arg0) {
cursor.startAnimation(bAnim);
}
});
cursor.post(new Runnable() {
#Override
public void run() {
cursor.startAnimation(tAnim);
}
});
Make sure that cursor and tAnim are instance/global variable in your class, and also start the animation in the post method of the TextView to run it in synchronize with the textView
Use animation listener. hope this works
Animation tAnim = new TranslateAnimation(-1000.f,200.0f,0.0f,0.0f);
tAnim.setStartTime(0);
tAnim.setDuration(3000);
tAnim.setStartOffset(0);
Animation bAnim = new AlphaAnimation(1.0f, 0.0f);
bAnim.setStartTime(tAnim.getDuration());
bAnim.setDuration(300);
bAnim.setStartOffset(30);
bAnim.setRepeatCount(Animation.INFINITE);
bAnim.setRepeatMode(Animation.REVERSE);
AnimationListener AnimationListener = new AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
// TODO Animation start and end
}
#Override
public void onAnimationEnd(Animation animation) {
//start the second animation here
yourview.startAnimation(bAnim);
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}};
tAnim.setAnimationListener(AnimationListener);
yourview.startAnimation(tAnim);
tAnim.hasEnded();

how to clear aftereffects of animation

I have 2 views that are being animated by sliding in and out of the screen simultaniously
this is the code for it: How to animate a slide in notification view that pushes the content view down
However, when the animation slides in, i must clear the animation, in order for the click events to work properly, otherwise they are also translated (which is not what i want)
What do i need to put in onAnimationEnd in the listener in order to clear the animation and have the view remain the way it looks after it has been animated
this is my current code:
greenViewSlideIn(1500, 0, new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
greenView.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationRepeat(Animator animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator animation) {
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(blueView.getWidth(), blueView.getHeight());
layoutParams.setMargins(blueView.getLeft(), blueView.getTop() - greenViewHeight, blueView.getRight(), blueView.getBottom());
blueView.setLayoutParams(layoutParams);
greenView.clearAnimation();
// blueView.clearAnimation();
}
#Override
public void onAnimationCancel(Animator animation) {
// TODO Auto-generated method stub
}
});
Two things you can do:
Make sure the setFillAfter flag on the animation is set to false. Then there's no need to an AnimationListener
Once the animation ends, call view.setAnimation(null);
Hope this helps :)

Doing animation on "hidden" Activity

I have been working on trying to get an Activity to not show until data has been selected and is ready for display: Prevent screen display until after fetching data
Finally got it to work with the aid of themes.
Now I have another problem.
I need to animate the transition from one Activity to the next.
I know how to use overridePendingTransition but this won't work here since I am already in the target Activity when I want to do the animation.
The only reason I can see the other one is because the current one is transparent.
I have no problem sliding in the new one:
View view = getLayoutInflater().inflate(R.layout.content_screen, null);
view.startAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_in_right));
setContentView(view);
However, I can't think of any way to get the old one to slide out.
Any ideas anyone?
Create a "slide out left" animation. Then adjust your animation for the new view, so when the animation starts, you apply and start the other animation to the other view. E.g.,
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO - Set and start other animation here
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
view.startAnimation(animation);
[Edit]
// First, define and infalte the view that will be incoming
View upcomingView = inflater.inflate(...);
// Next, we'll set the animation up
Animation animation = AnimationUtils.loadAnimation(context,
R.anim.slide_in_right);
animation.setDuration(TIME);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
Animation outAnimation = AnimationUtils.loadAnimation(context,
R.anim.slide_out_left);
upcomingView.startAnimation(outAnimation);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
currentView.startAnimation(animation); // This will start the animation defined above, which will also set and start the animation for the incoming object

android temporarily use setEnabled(false)

I am animating an ImageView so that when you click the image the animation occurs (then later it resets) but my problem is that if you click again where the image sat originally - the animation starts from the beginning right away with out finishing (it just resets and starts again.
so I tried using
setEnabled(false)
which works great, the animation continues on its path up perturbed by any random clicking, now the only problem is getting the ImageView enabled again - at about the same time as the animation stops
here's what i have
stopImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
mpButtonClick.start();
stopImage.setEnabled(false);
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
stopImage.startAnimation(anim);
now is there an easy way i can call setEnabled(true) after a some time has passed?
You could try using an AnimationListener and then calling setEnabled(true) from onAnimationEnd().
Something like this:
stopImage.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
mpButtonClick.start();
TranslateAnimation anim = new TranslateAnimation(0f,250 + Math.round(Math.random() * (-700)),0f,-300f);
anim.setDuration(4200);
anim.setRepeatCount(0);
anim.setAnimationListener(new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
stopImage.setEnabled(false);
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
stopImage.setEnabled(true);
}
});
stopImage.startAnimation(anim);
}
}
Here's the documentation for AnimationListeners.

Categories

Resources