How to make animation using ObjectAnimator? - android

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.:)

Related

Animate Views form one point to another point Android programatically

Im trying to animate two Buttons from a point (1st button). On Click of the first button, that button is set to INVISIBLE/GONE and two other button are shown. I am trying to pass the X and Y values of the View(1st button) as follows:
button.getX();
button.getY();
but im not getting the exact center (of 1st button).
Im trying to send those X and Y values to the animation but both the buttons (button2, button3) are coming side by side.. I want them to overlap as the point where they start animation is same. Im using the following code for the animation:
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);
I want the animation to be like this
#Deev Thanks thats really helped but the code im using has no start position
TranslateAnimation tanim_stay = new TranslateAnimation(
TranslateAnimation.ABSOLUTE, getIntent().getFloatExtra("ButtonX", 0) - btn_armed_stay.getWidth(),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getX()+ (btn_armed_stay.getWidth()/2),
TranslateAnimation.ABSOLUTE, -getIntent().getFloatExtra("ButtonY", 0),
TranslateAnimation.ABSOLUTE, btn_armed_stay.getY()+ (btn_armed_stay.getHeight()/2));
Im using this code for the start and end position of the animation but how to add another animation to the current Translate animation.. I want to add Alpha animation to this Translate animation..

moving and rotating view in android

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.

animate background image in horizontal scrollview item click

I need to animate a background view either left to right or right to left on in a horizontal scroll view item click. So it may indicate the selected item to user.
I am currently taking the absolute screen positions of each view on screen and translating them to there x and y positions on item click.
TranslateAnimation animation = new TranslateAnimation(xFrom, xTo, yFrom, yTo);
animation.setDuration(1000);
animation.setRepeatCount(0);
animation.setRepeatMode(2);
animation.setFillAfter(true);
mScroller.startAnimation(animation);
But i am failed to achieve the scenario?
So, I have achieve the scenario with ObjectAnimator.
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mSliderMember,"translationX", xFrom, xTo);
objectAnimator.setDuration(1000); // animation duration
objectAnimator.start();

Android View Y Axis Animation

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...

Android property animation relative to view's height

I initialize ObjectAnimator like this;
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationY", 0f, view.getHeight());
But when the first time animation runs, view.getHeight() method returns 0. So it does not animate. Second time i run the animation, it animates right. Is there a way to translate views relative to their width or height properly?

Categories

Resources