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();
Related
I am using ObjectAnimator to animate marker in google map but i want to reuse ObjectAnimator or continue the animation when new location is received i am using it like
public void onLocationUpdate(LatLong latLng)
{
ObjectAnimator animator =
ObjectAnimator.ofObject(marker, property, typeEvaluator,latLng);
animator.setDuration(1000);
animator.setInterpolator(new LinearInterpolator());
animator.setStartDelay(0);
animator.start();
animator.addListener(animatorEndListener);
}
i am getting this method called at the interval of 1 second i want to animate to have it appear continuous or at least reuse ObjectAnimator in case animation is finished when new value come so that it don,t create always new object
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());
I have a progress bar view like this:
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:max="100"/>
It lasts 3 second, so how to use interpolator to make update smoothly?
ObjectAnimator animation = ObjectAnimator.ofInt(what_is_in_here?);
animation.setDuration(3000); // second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
I really appreciate your help. Thank you very much in advance.
I found out the solution:
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
animation.setDuration(3500); // 3.5 second
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
Here is a detailed explanation:
create an animation object:
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", 100, 0);
progressBar: reference to the ProgressBar in the layout;
"progress": the name of the property to be animated;
100: starting point of the animation;
0: ending point of the animation.
and set an interpolation:
animation.setInterpolator(new DecelerateInterpolator());
It is possible to use different interpolators for our animation, like for example:
LinearInterpolator: where the rate of change is constant.
DecelerateInterpolator: where the rate of change starts out quickly and and then decelerates.
AccelerateInterpolator: where the rate of change starts out slowly and and then accelerates.
OvershootInterpolator: where the change flings forward and overshoots the last value then comes back.
For other interpolators check the interface android.view.animation.Interpolator.
I have three animations like scale, translate and scale. I play those animations in order. First two is working fine but last scale animation reset the position of view to original. If I remove last scale animation, it's working fine the view stay the new position after translate animation. Do you have any idea about this behavior?
AnimationSet animationSet = new AnimationSet(false);
animationSet.setFillAfter(true);
ScaleAnimation scaleAnimation1 = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f);
scaleAnimation1.setDuration(500);
scaleAnimation1.setFillAfter(true);
TranslateAnimation moveAnim = new TranslateAnimation(0, -x, 0, -y);
moveAnim.setDuration(1000);
moveAnim.setStartOffset(500);
moveAnim.setFillAfter(true);
ScaleAnimation scaleAnimation2 = new ScaleAnimation(1.0f, 0.0f, 1.0f, 0.0f);
scaleAnimation2.setDuration(500);
scaleAnimation2.setStartOffset(1000);
scaleAnimation2.setFillAfter(true);
scaleAnimation2.setFillBefore(true);
animationSet.addAnimation(scaleAnimation1);
animationSet.addAnimation(moveAnim);
animationSet.addAnimation(scaleAnimation2);
scaleAnimation2.setFillAfter(true); - If fillAfter is true, the transformation that this animation performed will persist when it is finished
scaleAnimation2.setFillBefore(true);- If fillBefore is true, this animation will apply its transformation before the start time of the animation.
These two properties cannot work simultaneously, and the one which set later is effective.
So, remove scaleAnimation2.setFillBefore(true); may solve your problem.
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...