I have a translate animation of an image from location1 to location2. However at any time during the animation, if a user click a button then I want to apply another translate animation from where the image at to location3 (even if the animation has not finished). Any way I can do that?
Thanks
TranslateAnimation translate = new TranslateAnimation(0, mDestLoc[0]-mSrcLoc[0], 0, mDestLoc[1]-mSrcLoc[1]);
translate.setFillAfter(true);
translate.setDuration(getDuration());
ball.startAnimation(translate);
void onbuttonClicked(){
// stop previous animation and translate image to location 3 "From where it stopped"
}
why don't you use ObjectAnimators?
You can create an animator for a particular target(or obtain the view one through animate() if you're running on a version greater-equal API level 12) and then translate, resize, fade,... your object.
If for some particular reasons the first animation is cancelled, you can call cancel and then the view will persist the current status. Then, if you create another animator based on the current values you should be able to easily perform another animation.
Related
I want to translate animation perform on button. Here Buttons are drag able in layout. I drag the button on any place of screen and when I remove my touch,I want to perform transform animation from current drop able points of button to original place which is initially button located on the screen. if any one have idea then well come.
You can animate any View very simply with a View Property Animator like this:
button.animate().translationX(deltaX)
.translationY(deltaY)
.setDuration(duration);
This works on API level 11 and above. If it is supposed to work before API level 11 then you need to use View Animations:
TranslateAnimation animation = new TranslateAnimation(fromX, toX, fromY, toY);
animation.setDuration(duration);
button.startAnimation(animation);
If you have any further question feel free to ask.
I am trying make an animation of an imageView from left to right and get it back from right to left but I also want to get the position of the imageview while its animation from the following code. I want to use the coordinates from origin to display in my app. Please tell me how to do this. Following is the code I am using to animate and move position of imageview.
Animation anx = new TranslateAnimation(0,100,0, 0);
anx.setDuration(2000);
anx.setFillAfter(true);
myImage.startAnimation(anx);
Is there a function like getX() and getY() to get position.
Have u tried to set animation listener and starting a runnable when the animation starts to check the image getx , gety every milliseconds u want and stop it after the animation ends
I have a mixture of animations running on various scenarios.
As an example;
I have a fade-in and fade-out animator on a small circle bitmap. On ACTION_DOWN the fade-in should start
and on ACTION_UP, when the fade-in ends, fade-out should run. BUT, if the user picks up the finger before the fade-in ends, it should stop right there (fadein.cancel()), fade-out should start from THAT alpha value.
I dont want a new object animator each time, I defined an animator like this in the constructor
fadein = Objectanimator.ofFloat(bitmap, "alpha", 1.0f);
fadeOut = Objectanimator.ofFloat(bitmap, "alpha", 0.0f);
What happens is, if I produce the issue as mentioned (picking up the finger fast), the fadeout animator will pick up the alpha value where fade-in left off and it'll RETAIN that. That is, 2nd time onwards it'll NOT pick up a new alpha! Why is that? Whats the workaround? Create new objects each time?
ObjectAnimator class modifies the attributes of the view, so it updates the property of the view using the setter method of the Object property (as per official Android Guide).
So, when fadein animation runs, the alpha value is constantly modified, and when fadein.cancel() executes, the alpha value of the View stops updating. Let's say this value is X.
So, when you start fadeOut, it animates from X to 0.0f.
Whats the workaround?
For fadeOut, use two values imply a starting and ending values
fadeOut = Objectanimator.ofFloat(bitmap, "alpha", 1.0f, 0.0f);
So if I have a translation animation like
Animation anim = new TranslateAnimation(0, 0, 0, OFFSET);
anim.setDuration(1000L);
anim.setAnimationListener(listener);
anim.setFillAfter(true);
and I apply this to a textswitcher so that given a offset, the textswitcher will move from a location, A, up or down.
My textswitcher will stay at a given location, B, after the animation due to the setfillafter. How can I then from that location, B, reverse the animation and go back to location, A?
The Overall Question
So I'm translating a textswitcher down and then up, but of course, setfillafter only translates the bitmap of the view and not the view itself, so this may not be the best approach. I've also tried to do an animation and then offset of the view, but that looks glitchy. So basically, I want a view(textswitcher) which a swipe/fling down will move it down and touching(onClick) it when it's down will move it back up (same animation reversed). How can I do this?
I know how to do the swipe/fling and onClick stuff, just not how to implement the correct animation.
Perhaps you may want to consider adding an on touch listener to see where you touched it last, If above move up with a constant animation. This is if im understanding the question correctly.
Is there a way to find out the final location of my animated "object" after the animation? Let's say I am animating an ImageView with location in parent as (0,0 - 20,20) using TranslateAnimation and ScaleAnimation over 1 second with setFillAfter(true).
How to I find the final location of this "object" (since the View location itself does not move)? I need to continue the animation from this point based on an user input.
hook up the AnimationStopped event and then request a location from the animated view.
I figured it out - use getTransformation on the Animation and apply the Matrix to the initial Rectangle, to obtain the final destination.