You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在onCreate中实现硬币翻转动画持续运行?线程方案无效

解决硬币翻转动画持续循环的问题

看起来你遇到的核心问题是子线程直接操作UI以及不合理的线程创建方式导致动画无法持续运行。咱们一步步来修复:

问题分析

你的rotate()方法里每次都会新建一个Thread,睡眠3秒后再次调用rotate()——但这个递归调用是在子线程里执行的,而applyRotation()里的image.startAnimation()是UI操作,Android不允许在非主线程操作UI,这会触发CalledFromWrongThreadException,只是可能你没注意到日志里的报错。另外,频繁创建Thread也会带来不必要的性能开销。

修复方案:利用主线程的延迟回调 + 动画监听

我们可以用Handler在主线程延迟触发下一次动画,或者结合动画的AnimationListener,等当前动画完全结束后再等待一段时间启动下一次翻转,这样更贴合动画的生命周期。

修改后的代码示例:

private ImageView image1;
private ImageView image2;
private boolean isFirstImage = true;
private Handler mHandler = new Handler(Looper.getMainLooper()); // 绑定主线程的Handler

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.review_users);
    image1 = findViewById(R.id.ImageView01);
    image2 = findViewById(R.id.ImageView02);
    image2.setVisibility(View.GONE);
    
    // 启动第一次翻转动画
    startNextRotation();
}

private void startNextRotation() {
    // 执行当前方向的翻转
    if (isFirstImage) {
        applyRotation(0, 90);
    } else {
        applyRotation(0, -90);
    }
    isFirstImage = !isFirstImage;
    
    // 延迟3秒后启动下一次翻转
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            startNextRotation();
        }
    }, 3000);
}

private void applyRotation(float start, float end) {
    final float centerX = image1.getWidth() / 2.0f;
    final float centerY = image1.getHeight() / 2.0f;
    final Flip3dAnimation rotation = new Flip3dAnimation(start, end, centerX, centerY);
    rotation.setDuration(500);
    rotation.setFillAfter(true);
    rotation.setInterpolator(new AccelerateInterpolator());
    rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1, image2));
    
    if (isFirstImage) {
        image1.startAnimation(rotation);
    } else {
        image2.startAnimation(rotation);
    }
}

// 页面销毁时移除Handler回调,避免内存泄漏
@Override
protected void onDestroy() {
    super.onDestroy();
    mHandler.removeCallbacksAndMessages(null);
}

进阶优化:动画结束后再延迟触发

如果你想让动画严格在当前动画完全结束后再等待3秒,可以修改为在AnimationListener的onAnimationEnd方法里触发延迟回调,这样动画衔接会更自然:

private void startNextRotation() {
    if (isFirstImage) {
        applyRotation(0, 90);
    } else {
        applyRotation(0, -90);
    }
    isFirstImage = !isFirstImage;
}

// 修改applyRotation中的动画监听逻辑
rotation.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {}

    @Override
    public void onAnimationEnd(Animation animation) {
        // 动画结束后延迟3秒启动下一次翻转
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                startNextRotation();
            }
        }, 3000);
    }

    @Override
    public void onAnimationRepeat(Animation animation) {}
});

这样就能实现硬币持续翻转的效果了,试试这个方案吧!

内容的提问来源于stack exchange,提问作者Deep Patel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:13:17