gpt4 book ai didi

android - 如何从停止的值开始 Android 动画?

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:44:54 25 4
gpt4 key购买 nike

我有一个应用旋转动画的 ImageView 。动画效果很好。着陆时,我尝试使用 cancel() 停止旋转动画,使用 reset() 重置动画并使用 clearAnimation() 清除 View 上的动画。但是动画又回到了原来的位置。如何使用触地事件发生时的值停止动画并从停止的地方重新开始?

我的动画在xml中定义如下

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="360"
android:toDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:fillAfter="true"
android:fillEnabled="true"
android:duration="200000"
android:repeatMode="restart"
android:repeatCount="infinite"
android:interpolator="@android:anim/linear_interpolator"/>

我正在尝试使用以下代码停止动画

private void stopAnimation(){
mRotateAntiClockwiseAnimation.cancel();
mRotateAntiClockwiseAnimation.reset();
imageView.clearAnimation();
mRotateAntiClockwiseAnimator.end();
mRotateAntiClockwiseAnimator.cancel();
stopAnimationForImageButton();
}

我正在使用以下代码在我的 View 上设置动画

mRotateAntiClockwiseAnimation = AnimationUtils.loadAnimation(context, R.anim.rotate_anticlockwise);
mRotateAntiClockwiseAnimation.setFillEnabled(true);
mRotateAntiClockwiseAnimation.setFillAfter(true);
imageView.setAnimation(mRotateAntiClockwiseAnimation);
mRotateAntiClockwiseAnimation.startNow();
imageView.invalidate();

如您所见,即使使用 cancel() 或 reset() 也无助于在动画被触及的地方停止动画。任何指针都会有所帮助

最佳答案

我想我是通过启动动画器然后设置 currentPlayTime() 来让它工作的。文档清楚地告诉(这是我偶然发现的)如果动画尚未开始,使用此方法设置的 currentPlayTime 将不会向前推进!

Sets the position of the animation to the specified point in time. This time should be between 0 and the total duration of the animation, including any repetition. If the animation has not yet been started, then it will not advance forward after it is set to this time; it will simply set the time to this value and perform any appropriate actions based on that time. If the animation is already running, then setCurrentPlayTime() will set the current playing time to this value and continue playing from that point. http://developer.android.com/reference/android/animation/ValueAnimator.html#setCurrentPlayTime(long)

    private void stopAnimation(){
mCurrentPlayTime = mRotateAntiClockwiseAnimator.getCurrentPlayTime();
mRotateAntiClockwiseAnimator.cancel();
}

private void startAnimation() {
mRotateAntiClockwiseAnimator.start();
mRotateAntiClockwiseAnimator.setCurrentPlayTime(mCurrentPlayTime);
}

关于android - 如何从停止的值开始 Android 动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25167153/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com