I am trying to create a mirror reflection of a view, for that I tired the Y axis ObjectAnimator animation using the following code:
ObjectAnimator animation = ObjectAnimator.ofFloat(view, "rotationY", 0.0f, 360f);
animation.setDuration(3600);
animation.setRepeatCount(ObjectAnimator.INFINITE);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
animation.start();
This works perfectly but I want the animation to stop after reaching the desired end position rather than returning back to the original place.
After some research I found that setting the setFillAfter(boolean) method of Animation object to true does that thing.
Now my problem is that i am unable to convert the ObjectAnimator animation to a standard Animation code. to get access to that method.
I realized i could use RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY) to rotate my view and also keep it at the ending place.
But I couldnt figure out what should be the pivotx and pivoty values so that the view stays at the same location and flips around.
Usually an ObjectAnimator stays at the desired location when the animation ends. Im guessing the line
animation.setRepeatCount(ObjectAnimator.INFINITE);
is what is making your animation start over and over in an infinite loop, remove this line and you should be fine...
Related
My application is used in landscape orientation. What I want to do is move an element from off the screen at all until it enters the screen and then when pressing a button, it leaves the screen again by scrolling to the left.
The animation starts from the default position of the view (i.e the position you have specified in your xml file). if you do not what the animation to start from that position, you can either:
change the initial position of the view in the xml file.
or set the x and y coordinates of the view at run time and then start your animation like so:
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
All you need is a TranslationX animation, see below, or check out this link. https://developer.android.com/training/animation/reposition-view
ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", deltaX);
ObjectAnimator translateY = ObjectAnimator.ofFloat(view, "translationY", deltaY);
If you have any question, please feel free.:)
I want to display a buttons with animations like fade-in along with the translate animation programmatically. I have to start translate animation from a specific position on the screen. So i need to pass the x and y values for the translate animation to start from.
Im using the following code for translate animation but how to add fade-in animation to the existing translate animation?
TranslateAnimation tanim = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", maxX),
TranslateAnimation.ABSOLUTE, 0,
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, 100);
tanim.setDuration(3000);
I've used Animations several times before and they always left me scratching my head. Some things I would recommend you should look into are Object Animators and Animator Sets. They're really useful, as they allow you to animate any property of the given view. As long as the view has a setter method for that property, you can add it to the animator set by passing in the string name of the property to the Animator constructor. The AnimatorSet class also allows you to easily choreograph multiple animations.
In your case, you could do something roughly like the following:
AnimatorSet animations = new AnimatorSet();
Animator xAnim = ObjectAnimator.ofFloat(button, "translationX", finalXValue);
xAnim.setDuration(3000);
Animator yAnim = ObjectAnimator.ofFloat(button, "translationY", finalYValue);
yAnim.setDuration(3000);
Animator alphaAnim = ObjectAnimator.ofFloat(button, "alpha", finalAlphaValue);
alphaAnim.setDuration(alphaDuration);
//Play all the animations together
animations.play(xAnim).with(yAnim).with(alphaAnim);
You can string together as many animators as you want this way, and you can also set animators to be played in a sequence using the .before() and .after() methods rather than the .with() method.
i wonder how to get current angle / degrees in rotate animation when animation ends or gets interrupted?
my idea is when the animation gets interrupted when some button is clicked, the rotation will go counter-clockwise (current flow is clockwise), start from last angle /degree right when it gets interrupted.
i've tried using applyTransformation, but it seems not called :(
thanks!
For this you must have to rotate your object not with simple rotate animation.
But Need to rotate with ObjectAnimator.
Here is simple example to find rotation angle of ball(imageview) .
ImageView ball;
int i = ball.getMeasuredHeight();
ObjectAnimator animation = ObjectAnimator.ofFloat(ball, "rotation", 0, 360);
animation.setDuration(10000);
ball.setPivotX(0);
ball.setPivotY(i / 2);
animation.setRepeatCount(ObjectAnimator.INFINITE);
animation.setInterpolator(new LinearInterpolator());
animation.start();
To get current rotation angle use:
ball.getRotation();
I'm using this code to moving view from x Position to another x Position :
int xStart=100,xEnd=500;
ObjectAnimator objectAnimator= ObjectAnimator.ofFloat(view, "translationX", xStart, xEnd);
objectAnimator.setDuration(duration);
objectAnimator.start();
but i need moving and rotating .
how can Rotate and move the view together ?
It is possible ?
You can use ObjectAnimator to animate any property of a View- essentially any property with set___() and get___() methods.
For rotation, you can use "rotation", "rotationX", and "rotationY" as appropriate.
It sounds like you already have translation working correctly, so I'm not sure what else you are looking for in "moving" the View.
To play multiple animations together, you can use an AnimatorSet. To move and rotate at the same time, you might do this:
AnimatorSet animations = new AnimatorSet();
ObjectAnimator translationAnim= ObjectAnimator.ofFloat(view, "translationX", 100, 500);
ObjectAnimator rotationAnim = ObjectAnimator.ofFloat(view, "rotation", 0, 90);
animations.play(rotationAnim).with(translationAnim);
animations.start();
For more information see the Property Animation documentation.
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);