ViewPropertyAnimator缩放后如何return一个View恢复到原来的大小?
How to return a View to its original size after it is scaled by ViewPropertyAnimator?
这是我的代码。此处图像尺寸在动画后减小。当我再次单击 ImageView
时,我只想要原来的 ImageView
size.I 是初学者,所以我需要一些帮助。我试过类似的东西:
football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();
在 setonclicklistener
的开头,但这不起作用。
提前致谢
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();
}
});
您可以检查 View
的当前缩放值(scaleX
或 scaleY
,在这种情况下无关紧要,因为您对它们进行了同等缩放)和increase/decrease 基于该值的大小。
例如:
// 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(在下面解决您的评论):如果您希望每次单击 View
时都从其原始大小缩小,那么您只需在每个动画之前 "reset" 它:
// 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();
这是我的代码。此处图像尺寸在动画后减小。当我再次单击 ImageView
时,我只想要原来的 ImageView
size.I 是初学者,所以我需要一些帮助。我试过类似的东西:
football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();
在 setonclicklistener
的开头,但这不起作用。
提前致谢
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();
}
});
您可以检查 View
的当前缩放值(scaleX
或 scaleY
,在这种情况下无关紧要,因为您对它们进行了同等缩放)和increase/decrease 基于该值的大小。
例如:
// 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(在下面解决您的评论):如果您希望每次单击 View
时都从其原始大小缩小,那么您只需在每个动画之前 "reset" 它:
// 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();