Android object animation not working smoothly - android

I am using ObjectAnimator to move the position of the RelativeLayout from center of the screen to above.The animation works on the RelativeLayout,but it does not move smoothly.It jumps between the start and end position.
Code:
ObjectAnimator animY = ObjectAnimator.ofFloat(logolayout, "y", 350f);
animY.setInterpolator(new LinearInterpolator());
animY.setDuration(1500);
animY.start();

Use ViewPropertyAnimator instead.
animate(view).y(350f).setInterpolator(new LinearInterpolator()).setDuration(1500).start();

Just remove the following:
animY.setInterpolator(new LinearInterpolator());

Related

Bounce animation with depth

how i can add bounce animation to view which will go down and come up but it should do this only for one time .
if i set bounce interpolator with y its bouncing for given duration
but i want it for one time only but it should go say 5dp down and up to current view
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 50, 0);
animator.setInterpolator(new BounceInterpolator());
animator.setDuration(200);
animator.start();
Use OvershootInterpolator.
Doc:
An interpolator where the change flings forward and overshoots the
last value then comes back.
You can adjust the tension of the overshoot using the constructor with tension parameter:
OvershootInterpolator(float tension)
tension: Amount of overshoot. When tension equals 0.0f, there is no
overshoot and the interpolator becomes a simple deceleration
interpolator.
Default value of tension is 2.0f.
Example using ViewPropertyAnimator:
targetView.animate()
.translationY(50)
.setInterpolator(new OvershootInterpolator())
.setDuration(200);
using ObjectAnimator:
ObjectAnimator animator = ObjectAnimator.ofFloat(targetView, "translationY", 0, 50); // pass only start and end values.
animator.setInterpolator(new OvershootInterpolator());
animator.setDuration(200);
animator.start();

How to use setDelay for animation within an AnimatorSet?

I'm trying to implement a set of animations, but the start delay value does not seem to be respected. Example:
ObjectAnimator a1 = ObjectAnimator.ofFloat(view1, View.ALPHA, 1f, 0f);
a1.setDuration(500);
a1.setStartDelay(500);
ObjectAnimator a2 = ObjectAnimator.ofFloat(view2, "translationX", ...);
a2.setDuration(500);
AnimatorSet set = new AnimatorSet();
set.playTogether(a1, a2);
set.start();
I can see the alpha animation starts immediately. If I don't try to play them together, the delay is respected and works fine. Can the delay not be used if part of an AnimatorSet?
If you want to play the animations in sequence, don't use ObjectAnimator.setStartDelay(). It's not designed for that use case.
Use AnimatorSet.playSequentially() instead:
AnimatorSet set = new AnimatorSet();
set.playSequentially(a2, a1);
set.start();

How to add a delay between animations in AnimatorSet playSequentially()?

I'm using AnimatorSet playSequentially method like this:
AnimatorSet set = new AnimatorSet();
ObjectAnimator in = ObjectAnimator.ofFloat(splash, "alpha", 0f, 1f);
in.setDuration(2500);
in.setInterpolator(new AccelerateInterpolator());
ObjectAnimator out = ObjectAnimator.ofFloat(splash, "alpha", 1f, 0f);
out.setDuration(2500);
out.setInterpolator(new AccelerateInterpolator());
set.playSequentially(in,out);
I whould like to add a delay between animation 1 and 2, like this:
set.playSequentially(in,1000,out);
It is possible to add delays between animations using playSequentially method?
Thank you.
Add this line of code:
out.setStartDelay(1000);
You could set a start delay on the 2nd Animator (i.e. out.setStartDelay(1000)) before adding it to the set.
Rather than setting a start delay on the individual animations,
use the AnimatorSet.after(long delay) function.
AnimatorSet s = new AnimatorSet();
s.play(anim1).after(1000).after(anim2);
AnimatorSet.Builder

how can i use property Animation to rotate view around the point?

use Tween Animation code:
#Override
public void onClick(View v) {
RotateAnimation rotateAnimation = new RotateAnimation (0,90,Animation.RELATIVE_TO_SELF,0,Animation.RELATIVE_TO_SELF,0);
rotateAnimation.setDuration (2000);
rotateAnimation.setFillAfter (true);
image2.startAnimation (rotateAnimation);
}
of cause use Tween Animation can do that,but Tween Animation hava a defect.
For example the following gif click event does not move.
So I want use property Animation to do that,but I found property Animation can only rotate around the Center.
how can i use property Animation to rotate view around the point?
I think from your question that you want rotation around the mouse pointer.
Try this, I think it should work but I haven't had a chance to try it yet.
...
Animation rotateAnimation = new RotateAnimation(0.0f, 360.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.0f);
...

Reverting animation done with AnimatorSet

I am creating an animation with AnimatorSet and when it ends I would like to leave the View where it was.
The code is something like this:
mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(
ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f),
ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
mLastAnimation.setDuration(3000);
mLastAnimation.addListener(this);
mLastAnimation.start();
// The Activity implements the AnimatorListener interface
#Override
public void onAnimationEnd(Animator animator) {
// Undo the animation changes to the view.
}
EDIT:
I am using the new animation API so setFillAfter() will not work here.
If you are using nineoldandroids there is a function called reverse. You can set the duration to be 0 and call reverse to reverse the animation.
You have to set the properties of the View back to its original values.
For example if you translateY 40 pixels forward, you need to translateY 40 pixels backwards. This is because the actual properties of the View have changed during property animation, not just the way it is rendered.
http://developer.android.com/guide/topics/graphics/prop-animation.html#property-vs-view
While using ObjectAnimator you can simply set
android:repeatMode="reverse"
You can use this:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImageView, "scaleX", 1.5f, 1f);
// here is the important part!
scaleX.setRepeatMode(ValueAnimator.REVERSE);
scaleX.setRepeatCount(1);
ObjectAnimator transl = ObjectAnimator.ofFloat(mImageView, "translationY", 40f, 0f));
// here is the important part!
transl.setRepeatMode(ValueAnimator.REVERSE);
transl.setRepeatCount(1);
mLastAnimation = new AnimatorSet();
mLastAnimation.playTogether(scaleX, transl);
// the rest is the same...

Categories

Resources