如何从android中的动画位置做反向动画?
How to do reverse animation from the animated position in android?
我正在尝试使用缩放和旋转属性为图像制作动画。我能够开始动画。但是如何从动画位置而不是图像的实际位置进行反转。
AnimatorSet animation = new AnimatorSet();
private void rotateImage(boolean isReverse) {
if(isReverse){
animation.playTogether(
ObjectAnimator.ofFloat(fingerPrintImage, "rotation", 0, 60),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 0.5f),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleY", 1, 0.5f)
);
}
else {
animation.playTogether(
ObjectAnimator.ofFloat(fingerPrintImage, "rotation", 0, 60),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 1.3f),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleY", 1, 1.3f)
);
}
animation.setDuration(5000);
animation.start();
}
rotateImage(true);
通常反转你的 'from' 和 'to' 浮动参数 f.e。 :
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 1.3f) // forward
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1.3f, 1) //backward
给你一个倒退的效果。
像这样的动画,强烈推荐ViewPropertyAnimator。它超级简洁,代码更少,不需要 AnimationSets(通常有问题),你可以在一行中链接不同的动画:
private void rotateImage(boolean isReverse) {
if(isReverse){
fingerPrintImage.animate().rotationBy(-60).scaleXBy(0.3f).scaleYBy(0.3f).setDuration(5000);
} else {
fingerPrintImage.animate().rotationBy(60).scaleXBy(-0.3f).scaleYBy(-0.3f).setDuration(5000);
}
也许你需要稍微调整一下你的价值观,但这应该是你所需要的。
通过函数参数传递所有浮点值会更短、更动态:
private void rotateImage(View view, float rotate, float scaleY, float scaleX, long duration) {
view.animate().rotationBy(rotate).scaleXBy(scaleX).scaleYBy(scaleY).setDuration(duration);
}
然后可以这样调用:
rotateImage(fingerPrintImage, 60, 0.3f, 0,3f, 5000); // forward
rotateImage(fingerPrintImage, 60, -0.3f, -0.3f, 5000) //backwards
这最大限度地减少了代码,摆脱了 isReverse 布尔值,并且您还可以为进一步的动画重用具有不同参数的方法。
我正在尝试使用缩放和旋转属性为图像制作动画。我能够开始动画。但是如何从动画位置而不是图像的实际位置进行反转。
AnimatorSet animation = new AnimatorSet();
private void rotateImage(boolean isReverse) {
if(isReverse){
animation.playTogether(
ObjectAnimator.ofFloat(fingerPrintImage, "rotation", 0, 60),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 0.5f),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleY", 1, 0.5f)
);
}
else {
animation.playTogether(
ObjectAnimator.ofFloat(fingerPrintImage, "rotation", 0, 60),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 1.3f),
ObjectAnimator.ofFloat(fingerPrintImage, "scaleY", 1, 1.3f)
);
}
animation.setDuration(5000);
animation.start();
}
rotateImage(true);
通常反转你的 'from' 和 'to' 浮动参数 f.e。 :
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1, 1.3f) // forward
ObjectAnimator.ofFloat(fingerPrintImage, "scaleX", 1.3f, 1) //backward
给你一个倒退的效果。
像这样的动画,强烈推荐ViewPropertyAnimator。它超级简洁,代码更少,不需要 AnimationSets(通常有问题),你可以在一行中链接不同的动画:
private void rotateImage(boolean isReverse) {
if(isReverse){
fingerPrintImage.animate().rotationBy(-60).scaleXBy(0.3f).scaleYBy(0.3f).setDuration(5000);
} else {
fingerPrintImage.animate().rotationBy(60).scaleXBy(-0.3f).scaleYBy(-0.3f).setDuration(5000);
}
也许你需要稍微调整一下你的价值观,但这应该是你所需要的。
通过函数参数传递所有浮点值会更短、更动态:
private void rotateImage(View view, float rotate, float scaleY, float scaleX, long duration) {
view.animate().rotationBy(rotate).scaleXBy(scaleX).scaleYBy(scaleY).setDuration(duration);
}
然后可以这样调用:
rotateImage(fingerPrintImage, 60, 0.3f, 0,3f, 5000); // forward
rotateImage(fingerPrintImage, 60, -0.3f, -0.3f, 5000) //backwards
这最大限度地减少了代码,摆脱了 isReverse 布尔值,并且您还可以为进一步的动画重用具有不同参数的方法。