upon onClickListener, I am starting an animation on textView (icon)
Animation anim = AnimationUtils
.loadAnimation(activity.getBaseContext(), R.anim.rotate_refresh);
icon.startAnimation(anim);
on clicking again, I want to check, if the icon is animating (rotating) , keep it that way or else start the animation again.
I want to know how can I check textview is in animation?
Maybe try something like this
Animation animation = icon.getAnimation();
if(animation != null && animation.hasStarted() && !animation.hasEnded()){
//do what you need to do if animating
}
else{
//start animation again
}
This line will let you know if the TextView is animated
Animation animation=textView.getAnimation();
It will most probably work for you as you want to do.
boolean isInBetweenAnim = false;
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
isInBetweenAnim = true;
}
#Override
public void onAnimationEnd(Animation animation) {
isInBetweenAnim = false;
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
So whenever a animation starts you will have isInBetweenAnim true and for rest you will have it false. Easily check the value of isInBetweenAnim in onClick of your button and you can code for it.
Hope it Works ! Happy Coding..
If you have multiple views that can start an animation on one view , you must focus on animated view, not animation.
I had multiple info imageViews that shows help text in a textView. To avoid multiple animations on textView, I used Arun's approach:
if (helpTextView.getAnimation() == null) {
helpTextView.startAnimation(helpAnimation);
}
Related
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);
I have a activity where I have multiple ImageViews and when you click on it the ImageView will fade out and fade back in. What I'm trying to figure out is how I can click one ImageView and start the animation and when I click a 2nd one and the animation is still running it will ignore the second one. I think I need to do something with the animationListener but I can't figure out how to use that to check if the animation is running or not before I initiate a new animation. I could have sworn I saw an example that did this but I've been looking for days and can't find it anymore, I'm hoping someone would be able to help out here..... below is the code for my animation:
// Create Animation
protected void fadeAnimation() {
tempImg.startAnimation(fadeout);
//Allow animation to finish
mHandler.postDelayed(new Runnable() {
public void run() {
tempImg.startAnimation(fadein);
}
}, 1000);
}
I'm assuming both fadeout and fadein are Animation objects.
Use fadeout.hasEnded() to check if the first has finished before starting your second one.
For more details about the Animation class, see here:
http://developer.android.com/reference/android/view/animation/Animation.html
Instead of having to loop possibly in another thread checking if an animation has ended, you could use an animation listener, doing something like this:
// Create Animation
protected void fadeAnimation() {
fadeout.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
tempImg.startAnimation(fadein);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
};
tempImg.startAnimation(fadeout);
}
With this kind of solution you wouldn't need to be actively checking if an animation has finished and time it with the duration of the previous animation.
The onAnimationEnd(Animation) is fired right after the animation has ended. This also solves the issue of users with developer options "on" and animation speed set to "off".
I applied an infinite animation on an ImageView to indicate a running background thread in my app. When the thread finishes, I'm able to stop the animation by using clearAnimation(), but it snaps the ImageView back to its starting position, and I'd like the current animation cycle to complete (which is designed to gracefully end in its starting position). Is there a way to do this?
Register an AnimationListener, and wait until onAnimationRepeat() to clear it. I haven't tried this, but I think it will work.
Just call setRepeatCount(0) and then listen for onAnimationEnd. See here for more details.
You can set the desirable position of your animation in the onAnimationEnd() method of your animation listener. Then, instead of going to the initial position, the View will be displayed at the coordinates you set up there.
animation = new TranslateAnimation(0, -length, 0, 0); //setup here your translation parameters
animation.setInterpolator(new LinearInterpolator());
animation.setDuration(10);
animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
animationRunning = true;
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
animationRunning = false;
// setAnimationPositionAfterPositionChange();
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) runningText.getLayoutParams();
params.leftMargin = currentPosition; //Calculate you current position here
runningText.setLayoutParams(params);
}
});
runningText.setAnimation(animation);
If you are using animate()
you use setListener(null) to stop it the animation,
works for me.
image.animate()
.translationXBy(-width* 0.5f)
.setDuration(300)
.setStartDelay(6000)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
image.animate().rotationBy(90).setDuration(1000).setStartDelay(1).setListener(null);
}
});
It seems that an android animation is not truly finished when the onAnimationEnd event is fired although animation.hasEnded is set to true.
I want my view to change it's background drawable on the end of it's ScaleAnimation which it does, but you can clearly see that it is changed some miliseconds before it finishes. The problem is, that it flickers because the new background appears (=is) scaled for a short time until the animation really finishes.
Is there a way to get either the real end of the animation or just prevent the new background from beeing scaled this short period of time?
Thank you!
//EDIT: I'm using an AnimationListener to get the following call:
#Override
public void onAnimationEnd(Animation animation)
{
View view = (MyView) ((ExtendedScaleAnimation) animation).getView();
view.clearAnimation();
view.requestLayout();
view.refreshBackground(); // <-- this is where the background gets changed
}
Here is the actual bug related to this issue http://code.google.com/p/android-misc-widgets/issues/detail?id=8
This basically states that the onAnimationEnd method doesn't really work well when an AnimationListener is attached to an Animation
The workaround is to listen for the animation events in the view to which you were applying the animation to
For example if initially you were attaching the animation listener to the animation like this
mAnimation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
//Functionality here
}
});
and then applying to the animation to a ImageView like this
mImageView.startAnimation(mAnimation);
To work around this issue, you must now create a custom ImageView
public class MyImageView extends ImageView {
and then override the onAnimationEnd method of the View class and provide all the functionality there
#Override
protected void onAnimationEnd() {
super.onAnimationEnd();
//Functionality here
}
This is the proper workaround for this issue, provide the functionality in the over-riden View -> onAnimationEnd method as opposed to the onAnimationEnd method of the AnimationListener attached to the Animation.
This works properly and there is no longer any flicker towards the end of the animation.
I was abe to resolve this by calling clearAnimation() on the view being animated inside onAnimationEnd, that took away the flicker
Its weird why would anyone have to do that, as onAnimationEnd callback should have been called only if the animation has already ended. But I guess the answer lies in the depth of Framework on how view/layout handles animation callback.
For now take it as a hack-free solution, that just works.
animation.setAnimationListener(new AnimationListener() {
public void onAnimationEnd(Animation anim) {
innerView.clearAnimation(); // to get rid of flicker at end of animation
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(innerBlockContainer.getWidth(), innerBlockContainer.getHeight());
/* Update lp margin, left/top to update layout after end of Translation */
ViewGroup parent_ofInnerView = (ViewGroup)innerView.getParent();
vp.updateViewLayout(innerBlockContainer, lp);
}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationStart(Animation arg0) {
}
});
innerView.startAnimation(animation);
I had same issue and solved it using
view.clearAnimation();
before
view.startAnimation(anim);
I had a similar problem and I used Soham's solution with custom view class.
It worked fine, but at the end, I've found a simpler solution that worked for me.
After calling the view.StartAnimation(animation), and before the next step in my program, I've added a short delay that will be long enough to let the animation finish, but short enough to be unnoticeable by the user:
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
nextStepInMyProgram();
}
}, 200);// delay in milliseconds (200)
For some reason the onAnimationStart works properly, and the onAnimationEnd doesnt. So heres how I originally did it and what I changed:
Attempt 1 (flicker):
a) Move image from 0px to 80px
b) In onAnimationEnd, set the image's location to 80px
Attempt 2 (no flicker):
a) In onAnimationStart, set the image's location to 80px
b) Move the image from -80px to 0px
Hope that made sense. Basically I flipped the way I did it
Try to use getAnimation() from your object:
public void onShowListBtnClick(View view)
{
rightPanel.startAnimation(AnimationUtils.loadAnimation(MainActivity.this, R.anim.slide_left));
rightPanel.getAnimation().setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animation animation) {
// write your code here
}
});
}
An easy fix is to add one line to AnimationListener.onAnimationEnd():
#Override
public void onAnimationEnd(Animation a) {
a.setAnimationListener(null);
…
}
annimation can be also stopped on screen rotation. in this case onAnimationEnd() is not being called. my workaround:
animation.setDuration(animationDuration);
animation.setAnimationListener(new AnimationListenerAdapter() {...});
view.startAnimation(animation);
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!animation.hasEnded()) {
// here you can handle this case
}
}
}, animationDuration + 100);
I had this issue because my Animation was not started in the main thread.
This resulted in a duration of 0.
However , the Animation did play correctly - it just called onAnimationEnd() immediately after execution.
If you are using repeat count as infinite, then onAnimationEnd would not get called. Check the documentation link
You can also use setUpdateListener, then check the current fraction of the animation progress and act accordingly.
Here's a Kotlin example for a fade-out animation which eventually makes the view gone from the layout:
view.animate()
.alpha(0f)
.setDuration(duration)
.setUpdateListener { animation ->
if (animation.animatedFraction > 0.99f) {
view.visibility = View.GONE
}
}
.start()
This worked for me:
#Override
public void onAnimationUpdate(ValueAnimator animation) {
if (Float.compare(animation.getAnimatedFraction(),1.0f)) {
// animation ended;
//do stuff post animation
}
}
I have a simple LinearLayout with two Buttons side by side. They are supposed to slide into and out of view from the right side of the screen when needed. I have the animation working and the rest of the work is done, but I have one last problem to solve.
How can I set the LinearLayout's visibility to View.GONE after the slide out animation is complete? I need it to disappear once it's of screen.
Grab a reference of your Animation object doing the animation. Call Animation#setAnimationListener and in the listener's onAnimationEnd method set the visibility to View.GONE.
Duplicate : https://stackoverflow.com/a/7606533/3717188
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
}
});
LinearLayout al = (LinearLayout) findViewById(R.id.layoutid);
al.setVisibility(view.INVISIBLE);
Add the above code in your
onAnimationEnd(){
}