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

Android按钮带动画切换图片问题:图片可隐藏但无法再次显示

Fix: ImageView Won't Reappear After Hide Animation

Alright, let's break down why your ImageView hides correctly but won't come back when you tap the button again, and fix it step by step.

The Root Problem

Your current code has a timing issue with how you handle the View's visibility and animations:

  • When hiding the ImageView, you first set sjt.setVisibility(View.GONE) which immediately removes it from the layout. The animation you run after that doesn't actually act on a visible View (you might see a hidden effect by accident, but it's not working as intended).
  • When trying to show it again, even though you set View.VISIBLE, the ImageView's alpha is still stuck at 0 from the hide animation, and there's no proper reset to kick off the show animation correctly.

Fixed Code

public void jerry(View view) {
    Button button = (Button) findViewById(R.id.button);
    ImageView sjt = (ImageView) findViewById(R.id.imageView2);

    if (sjt.getVisibility() == View.VISIBLE) {
        // Run hide animation first, then set GONE once it's done
        sjt.animate()
                .alpha(0)
                .rotationBy(1800)
                .setDuration(3000)
                .setListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {}

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        sjt.setVisibility(View.GONE);
                        // Clean up the listener so it doesn't interfere later
                        sjt.animate().setListener(null);
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {}

                    @Override
                    public void onAnimationRepeat(Animator animator) {}
                });
    } else {
        // Make the View visible first, then reset alpha to start the show animation properly
        sjt.setVisibility(View.VISIBLE);
        sjt.setAlpha(0);
        sjt.animate()
                .alpha(1)
                .rotationBy(1800)
                .setDuration(3000)
                .setListener(null); // Clear any leftover listeners
    }
}

Key Changes Explained

  • Hide Logic: We run the fade-out and rotation animation first, and only set View.GONE once the animation finishes. This ensures the animation plays fully on a visible View, and the View disappears cleanly after.
  • Show Logic: Before starting the show animation, we explicitly set the ImageView's alpha to 0. This guarantees the fade-in animation starts from a fully transparent state, so you can actually see it appear. We also clear any old animation listeners to prevent conflicts.
  • Listener Cleanup: Removing the animation listener after it's done prevents multiple listeners from piling up if you tap the button quickly, which could cause unexpected behavior.

With these changes, your ImageView will hide and show smoothly with the animations every time you tap the button.

内容的提问来源于stack exchange,提问作者Harshit Mk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 08:48:05