This is my code. Here the image size is decreased after the animation. When I click the ImageView
again I just want the ImageView
in its original size.I am a beginner so I need some help. I have tried something like :
football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();
at the beginning of setonclicklistener
but that doesn't work.
Thanks in advance
football.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ValueAnimator animator = ValueAnimator.ofFloat(0, 1);
// values from 0 to 1
animator.setDuration(1000); // 5 seconds duration from 0 to 1
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener()
{
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float value = ((Float) (animation.getAnimatedValue()))
.floatValue();
// Set translation of view here. Position can be calculated
// out of value. This code should move the view in a half circle.
football.setTranslationX((float)(100.0 * Math.sin(value*Math.PI)));
football.setTranslationY((float)(400.0 * Math.cos(value*Math.PI)));
}
});
animator.start();
football.setScaleType(ImageView.ScaleType.CENTER);
//here the scaling is performed
football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();
}
});
For ValueAnimator and ObjectAnimator can be like this a try: animator. addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animation. removeListener(this); animation.
You create the Animator , set any optional properties such as the duration or repetition attributes, and start it. For example, to fade an object called myView out, you would animate the alpha property like this: ObjectAnimator. ofFloat(myView, "alpha", 0f).
A property animation changes a property's (a field in an object) value over a specified length of time. To animate something, you specify the object property that you want to animate, such as an object's position on the screen, how long you want to animate it for, and what values you want to animate between.
You could check for the current scale value of the View
(either scaleX
or scaleY
, doesn't matter in this case since you scale both of them equally) and increase/decrease the size based on that value.
For example:
// if the current scale is lesser than 1.0f, increase it to 1.0f
// otherwise decrease it to 0.4f
float scaleValue = football.getScaleX() < 1.0f ? 1.0f : 0.4f;
football.animate().scaleX(scaleValue).scaleY(scaleValue).setDuration(1000).start();
EDIT (addressing your comment below): If you would like your View
to shrink from its original size every time you click on it, then you just have to "reset" it before each animation:
// resetting the scale to its original value
football.setScaleX(1.0f);
football.setScaleY(1.0f);
// shrinking
football.animate().scaleX(0.4f).scaleY(0.4f).setDuration(1000).start();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With