如何恢复和暂停Android中的ObjectAnimator API级别低于19?

eli*_*tht 9 animation android objectanimator

我知道API级别19支持ObjectAnimators上的pause()和resume().但是在API级别14的项目中,我有一个ObjectAnimator应用于Image视图来旋转它.我想在触摸时暂停ObjectAnimator提供的动画,并从图像视图所在的位置(触摸前)恢复它.

所以我试图保存当前的播放时间并取消我的stopAnimation()函数上的对象动画.

private void stopAnimation(){
        currentTime = mGlobeAnimator.getCurrentPlayTime();
        mGlobeAnimator.cancel();
    }
Run Code Online (Sandbox Code Playgroud)

在startAnimation()函数中,我重新创建动画师,将其目标设置为图像视图,设置保存的播放时间并启动它.

private void startAnimation(Context context, View view, float startAngle) {
        ObjectAnimator globeAnimatorClone = (ObjectAnimator)AnimatorInflater.loadAnimator(context, R.animator.rotate_globe);
        globeAnimatorClone.setTarget(mImageView);
        globeAnimatorClone.setCurrentPlayTime(currentTime);
        globeAnimatorClone.start();
}
Run Code Online (Sandbox Code Playgroud)

这不起作用.在19岁之前,有任何人会帮忙请求暂停和恢复动画师为API级别提供的动画吗?

eli*_*tht 16

我想通过启动动画师然后设置currentPlayTime()来实现它.文档清楚地告诉我(我只是偶然发现)如果动画还没有开始,使用这种方法设置的currentPlayTime将不会前进!

将动画的位置设置为指定的时间点.此时间应介于0和动画的总持续时间之间,包括任何重复.如果尚未启动动画,则在设置为此时它不会前进; 它只会将时间设置为此值,并根据该时间执行任何适当的操作.如果动画已在运行,则setCurrentPlayTime()会将当前播放时间设置为此值并从该点继续播放.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);
}
Run Code Online (Sandbox Code Playgroud)


Rod*_*uin 1

您所做的是,它只会重新启动动画,而您可以使用暂停和恢复方法为动画创建自定义类。

您需要首先检查设备是否为 19 api 及以上版本,如果是,则使用对象动画器的本机暂停和恢复,否则使用 api 1 设计的常规动画,您可以按照此线程进行 api 19 以下的暂停和恢复。