How to get Animation from View - android

I've set an Animation on ProgressBar in click event in Adapter
ObjectAnimator animation = ObjectAnimator.ofInt(holder.progressbar, "progress", 0, 100);
animation.setDuration(PROGRESS_TIME);
animation.setInterpolator(new DecelerateInterpolator());
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
Toast.makeText(context,"HELL_Start",Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationEnd(Animator animator) {
//do something when the countdown is complete
Toast.makeText(context,"HELL_OFF_END",Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationCancel(Animator animator) {
Toast.makeText(context,"HELL_OFF_Cancel",Toast.LENGTH_SHORT).show();
}
#Override
public void onAnimationRepeat(Animator animator) { }
});
animation.start();
I'm trying to get Animation (when list item values get changed) from the ProgressBar by using
AlphaAnimation animation = (AlphaAnimation)mProgressBar.getAnimation();
but it is returning null

You can set the animator object as the associated view's tag.
holder.progressbar.setTag(animation);
Than retrieve it later:
ObjectAnimator animator = (ObjectAnimator) holder.progressBar.getTag();
// Do something with animator

Related

How to restart object animator after some seconds in Android?

My code looks like that:
objectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, (float) truePosition);
animator.setDuration(totalTime);
animator.setInterpolator(new DecelerateInterpolator());
animator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
einsatz = 0;
disableChips();
imageRoulette.setEnabled(false);
}
#Override
public void onAnimationEnd(Animator animator) {
imageRoulette.setEnabled(true);
imageRoulette.setImageDrawable(null);
imageRoulette.setBackgroundResource(R.drawable.roulette);
enableChips();
printNumberAfterRotation(randPosition);
myBets.clear();
einsatz = -1;
gesamtEinsatz = 0;
}
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
});
animator.start();
By now I always have to tap on the view to rotate my image. Is there a way to restart it after 10 seconds?
Do you have any suggestions how to fix it?
#Override
public void onAnimationEnd(Animator animator) {
animation.setStartDelay(10000); // 10 SEC
animation.start();
}

How to create Accelerate - Linear - Decelerate animation effect in android using ValueAnimator?

Now I have to create an animation of a spinning fan. When user click POWER ON button, this fan begin to spin, then keep it's spinning speed at some level.When user click POWER OFF button, it slowing down then it stop.
I make some code as follows:
ImageView mFanImageview;
private ValueAnimator mFanValueAnimator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_centripetal_particle);
ButterKnife.bind(this);
}
private void startFan() {
mFanValueAnimator = new ValueAnimator();
mFanValueAnimator.setDuration(1000);
mFanValueAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
mFanValueAnimator.setInterpolator(new AccelerateInterpolator());
mFanValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
mFanValueAnimator = new ValueAnimator();
mFanValueAnimator.setDuration(500);
mFanValueAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
mFanValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
mFanValueAnimator.setRepeatMode(ValueAnimator.RESTART);
mFanValueAnimator.setInterpolator(new LinearInterpolator());
mFanValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationCancel(Animator animation) {
stopFanValueAnimator().start();
}
});
mFanValueAnimator.start();
}
#Override
public void onAnimationCancel(Animator animation) {
stopFanValueAnimator().start();
}
});
mFanValueAnimator.start();
}
private ValueAnimator stopFanValueAnimator() {
ValueAnimator stopAnimator = new ValueAnimator();
stopAnimator.setDuration(1000);
stopAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
stopAnimator.setInterpolator(new DecelerateInterpolator());
stopAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
return stopAnimator;
}
#OnClick(R.id.stop_button)
public void onStopButtonClicked() {
mFanValueAnimator.cancel();
}
#OnClick(R.id.start_button)
public void onStartButtonClicked() {
startFan();
}
The animation above seems ok, but I found that the fan spinning speed at the end of AccelerateInterpolator animator is hard to match the beginning of LinearInterpolator.I have to adjust duration of LinearInterpolator animator carefully.
How can I get the update rate at the end of AccelerateInterpolator animator then set the right duration of LinearInterpolator animator?
I think the best approach is set accelerate interpolator initially, and on repeat again set LinearInterpolator. Now in case of repeat put a flag for handling switch off. If the switch off demanded then change value of switch Off flag that will apply Decelerate interpolator and make sure that the animation will not run again.
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationRepeat(Animator animation) {
if (switchOff) {
animation.setInterpolator(new DecelerateInterpolator());
} else {
animation.setInterpolator(new LinearInterpolator());
}
}
});

Animator onAnimationEnd cant change text

I'm translating a TextView 500dp to the right of the screen and then I use a listener to listen for the animation end where I want to change the text and then translate the TextView back to its initial space.
My code is :
ObjectAnimator animator = ObjectAnimator.ofFloat(rvDownCircle, "x", 500f);
animator.setStartDelay(500);
animator.setDuration(1500);
animator.start();
animator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
rvDownCircle.setText("New Label");
ObjectAnimator animator = ObjectAnimator.ofFloat(rvDownCircle, "x", 0);
animator.setDuration(1000);
animator.start();
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
The TextView is named rvDownCircle and as you can see when I'm on AnimationEnd I change the text to a new text - but what it happens is that text stays the same.
My question is : what am i doing wrong here and what should i change so when i reach the end of the animation setText will take effect.

How to reset ObjectAnimator to it's initial status?

I want to vibrate a view with scaleX and scaleY, and I am doing it with this code, but the problem is that sometimes the view is not correctly reset, and it shows with the scale applied...
I want that when the animation ends, the view must be seen with its original status always
this is the code:
ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 0.9f);
scaleX.setDuration(50);
scaleX.setRepeatCount(5);
scaleX.setRepeatMode(Animation.REVERSE);
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 0.9f);
scaleY.setDuration(50);
scaleY.setRepeatCount(5);
scaleY.setRepeatMode(Animation.REVERSE);
set.play(scaleX).with(scaleY);
set.start();
Thanks
For ValueAnimator and ObjectAnimator can be like this a try:
animator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
animation.removeListener(this);
animation.setDuration(0);
((ValueAnimator) animation).reverse();
}
});
UPDATE
On Android 7 it doesn't work.
Best way use the interpolator.
public class ReverseInterpolator implements Interpolator {
private final Interpolator delegate;
public ReverseInterpolator(Interpolator delegate){
this.delegate = delegate;
}
public ReverseInterpolator(){
this(new LinearInterpolator());
}
#Override
public float getInterpolation(float input) {
return 1 - delegate.getInterpolation(input);
}
}
In your code
animator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
animation.removeListener(this);
animation.setDuration(0);
animation.setInterpolator(new ReverseInterpolator());
animation.start();
}
});
According to the docs(Property Animation):
The property animation system can animate Views on the screen by changing the actual properties in the View objects. In addition, Views also automatically call the invalidate() method to refresh the screen whenever its properties are changed.
so you can use AnimatorListener to listen the animation event, then just reset the view property you animate. let's say cancel event and scaleX property:
scaleAnimator.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationCancel(Animator animation) {
scaleView.setScaleX(0)
}
});
Hope this can help a bit.
You can add an AnimatorListener, to be notified when the animation ends:
scaleY.addListener(new AnimatorListener() {
#Override
public void onAnimationEnd(Animator animation) {
// TODO Restore view
}
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
#Override
public void onAnimationCancel(Animator animation) {
}
});

Check if AnimatorSet has finished animation?

I'm trying to animate buttons with fade in animation using AnimatorSet
Button fades in > Click button > Remaining buttons fade out
So in order to do this, I want to set the onClickListner after the animation is completed, but that doesn't seem to work. Clicking a button in the middle of the animation triggers the onClick action:
setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.start();
This is the method that checks if the animation has finished.
private void checkAnimation() {
while (true) {
// Check if animation has ended
if (setQuestion.isRunning() == false) {
assignListners();
break;
}
}
}
You can set an AnimatorListener on fadeinAnimation5.
This will give you an onAnimationEnd callback.
fadeinAnimation5.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// ...
}
#Override
public void onAnimationRepeat(Animator animation) {
// ...
}
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
#Override
public void onAnimationCancel(Animator animation) {
// ...
}
});
Or, as suggested by slott use an AnimatorListenerAdapter
fadeinAnimation5.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
}
I was having a similar problem and here is how I solved it:
private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
AnimatorSet mAnimationSet = new AnimatorSet();
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA, 1f, 0f);
fadeOut.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
fadeOutTarget.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
fadeOut.setInterpolator(new LinearInterpolator());
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
fadeIn.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
fadeInTarget.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
fadeIn.setInterpolator(new LinearInterpolator());
mAnimationSet.setDuration(duration);
mAnimationSet.playTogether(fadeOut, fadeIn);
mAnimationSet.start();
}
You can actually set a listener to the AnimatorSet directly since AnimatorSet inherits from Animator. Here's some code:
import android.animation.Animator;
AnimatorSet setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
// !! turn on your onClickListener here !!
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
setQuestion.start();

Categories

Resources