Mar*_*Gin 6 android android-animation android-view
这是我的代码.这里动画后图像尺寸减小了.当我ImageView再次点击时,我只想要ImageView原来的尺寸.我是初学者,所以我需要一些帮助.我尝试过类似的东西:
football.animate().scaleX(1f).scaleY(1f).setDuration(1000).start();
Run Code Online (Sandbox Code Playgroud)
在开始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();
}
});
Run Code Online (Sandbox Code Playgroud)
您可以检查 的当前缩放值View( 或scaleX,scaleY在这种情况下并不重要,因为您对它们进行同等缩放)并根据该值增加/减少大小。
例如:
// 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();
Run Code Online (Sandbox Code Playgroud)
编辑(在下面解决您的评论):如果您希望View每次单击它时都缩小其原始大小,那么您只需在每个动画之前“重置”它:
// 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();
Run Code Online (Sandbox Code Playgroud)