Android 翻译动画进度回调
Android Translate Animation Progress Callback
我正在使用 Translate
动画(借用 here)如下:
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE,200, Animation.ABSOLUTE,200,
Animation.ABSOLUTE,200, Animation.ABSOLUTE,200);
a.setDuration(1000);
a.setFillAfter(true);
animationSet.addAnimation(a);
myView.startAnimation(a);
有什么回调可以给我myView
的当前位置吗?我想在动画进行时根据 myView
的位置执行一个动作。
很遗憾,您可以这样做。使用
a.setRepeatCount(200);
并设置动画每次移动 1 个像素
Animation.ABSOLUTE,1
然后
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// this will fire after each repetition
}
});
新解决方案现已作为 android 中 Spring Physics 的一部分提供。
您可以使用DynamicAnimation.OnAnimationUpdateListener
界面。
此接口的实现者可以将自己添加为 DynamicAnimation
实例的更新侦听器,以接收每个动画帧的回调。
这是一个快速入门的 kotlin 代码
// Setting up a spring animation to animate the view1 and view2 translationX and translationY properties
val (anim1X, anim1Y) = findViewById<View>(R.id.view1).let { view1 ->
SpringAnimation(view1, DynamicAnimation.TRANSLATION_X) to
SpringAnimation(view1, DynamicAnimation.TRANSLATION_Y)
}
val (anim2X, anim2Y) = findViewById<View>(R.id.view2).let { view2 ->
SpringAnimation(view2, DynamicAnimation.TRANSLATION_X) to
SpringAnimation(view2, DynamicAnimation.TRANSLATION_Y)
}
// Registering the update listener
anim1X.addUpdateListener { _, value, _ ->
// Overriding the method to notify view2 about the change in the view1’s property.
anim2X.animateToFinalPosition(value)
}
// same for Y position
anim1Y.addUpdateListener { _, value, _ -> anim2Y.animateToFinalPosition(value)
}
您可以收听动画视图的每帧更新以及相应的 animate/update 其他视图。
我正在使用 Translate
动画(借用 here)如下:
TranslateAnimation a = new TranslateAnimation(
Animation.ABSOLUTE,200, Animation.ABSOLUTE,200,
Animation.ABSOLUTE,200, Animation.ABSOLUTE,200);
a.setDuration(1000);
a.setFillAfter(true);
animationSet.addAnimation(a);
myView.startAnimation(a);
有什么回调可以给我myView
的当前位置吗?我想在动画进行时根据 myView
的位置执行一个动作。
很遗憾,您可以这样做。使用
a.setRepeatCount(200);
并设置动画每次移动 1 个像素
Animation.ABSOLUTE,1
然后
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
// this will fire after each repetition
}
});
新解决方案现已作为 android 中 Spring Physics 的一部分提供。
您可以使用DynamicAnimation.OnAnimationUpdateListener
界面。
此接口的实现者可以将自己添加为 DynamicAnimation
实例的更新侦听器,以接收每个动画帧的回调。
这是一个快速入门的 kotlin 代码
// Setting up a spring animation to animate the view1 and view2 translationX and translationY properties
val (anim1X, anim1Y) = findViewById<View>(R.id.view1).let { view1 ->
SpringAnimation(view1, DynamicAnimation.TRANSLATION_X) to
SpringAnimation(view1, DynamicAnimation.TRANSLATION_Y)
}
val (anim2X, anim2Y) = findViewById<View>(R.id.view2).let { view2 ->
SpringAnimation(view2, DynamicAnimation.TRANSLATION_X) to
SpringAnimation(view2, DynamicAnimation.TRANSLATION_Y)
}
// Registering the update listener
anim1X.addUpdateListener { _, value, _ ->
// Overriding the method to notify view2 about the change in the view1’s property.
anim2X.animateToFinalPosition(value)
}
// same for Y position
anim1Y.addUpdateListener { _, value, _ -> anim2Y.animateToFinalPosition(value)
}
您可以收听动画视图的每帧更新以及相应的 animate/update 其他视图。