第二次调用setVisibility无效

时间:2015-07-31 10:25:14

标签: java android visibility

我正在写一款安卓游戏。最初会在游戏结束后显示LinearLayout。用户可以点按LinearLayout开始游戏,LinearLayout消失(setVisibility (View.GONE))动画。

游戏结束后,同一个LinearLayout显示另一个动画。这次我在setVisibility (View.Visible)方法中调用onAnimationStart。然而,在游戏结束后,我既看不到动画,也看不到LinearLayout

这是我的代码:

public void removeCover (View view) {
    Animation anim = AnimationUtils.loadAnimation (this, R.anim.cover_move_out);
    anim.setAnimationListener (new Animation.AnimationListener () {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            GameActivity.this.cover.setVisibility (View.GONE);
            Game.getInstance ().startGame ();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
    cover.startAnimation (anim);
}

public void gameOver () {
    tvScore.setText (Integer.toString (Game.getInstance ().getScore ()));
    tvScoreText.setVisibility (View.VISIBLE);
    tvScore.setVisibility (View.VISIBLE);

    Animation anim = AnimationUtils.loadAnimation (this, R.anim.cover_fade_in);
    anim.setAnimationListener (new Animation.AnimationListener () {
        @Override
        public void onAnimationStart(Animation animation) {
            GameActivity.this.cover.setVisibility (View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            GameActivity.this.cover.setVisibility (View.VISIBLE);
            Game.InitializeGame (GameActivity.this);
            cover.setVisibility (View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}

cover_move_out动画:

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:fromXDelta="0%"
        android:toXDelta="0%"
        android:duration="1000"/>
    <scale
        android:fromXScale="100%"
        android:fromYScale="100%"
        android:toXScale="0%"
        android:toYScale="0%"
        android:duration="1000"/>
</set>

cover_fade_in动画:

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="2000"/>
</set>

说明: 当用户点击removeCover时,系统会调用LinearLayout。游戏结束时将调用gameOver。我尝试将setVisibility(View.Visible)放在不同的地方:gameOver方法,onAnimationEndsonAnimationStarts方法。我还尝试在动画开始之前将可见性设置为INVISIBLE。但这些都不起作用。我想这可能是因为setVisibility(View.GONE)只能被调用一次。之后,观点真的是“GONE”。

0 个答案:

没有答案