Android动画无法使用isShown方法

时间:2017-02-07 12:24:49

标签: java android android-layout android-animation

我有一个不起作用的动画应该在按钮点击时触发,点击检查图像是否显示并运行动画如果是,然后在else中运行不同的动画。问题是只有else语句中的动画才有效。如果使用logcat测试条件,if语句仍然执行,则动画不会发生。任何帮助表示感谢。

在点击事件被调用后

  if (smsArea.isShown()) {
                Animation backDoww = AnimationUtils.loadAnimation(getContext(),
                        R.anim.slide_out_right);
                smsArea.startAnimation(slide_out_right);
                smsArea.setVisibility(View.GONE);
            }else{
                Animation slide_in_right= AnimationUtils.loadAnimation(getContext(),
                        R.anim.slide_in_right);
                smsArea.startAnimation(slide_in_right);
                smsArea.setVisibility(View.VISIBLE);
            }

我的else语句中的动画是唯一有效的动画,第一个应该用if(smsArea.isShown())触发的动画永远不会发生。

我最初将smsArea设置为Gone,我在onCreate中执行此操作而不是在xml中,它的左边是xml中的默认值。我知道错误不在我的动画文件中,因为即使我在其他地方使用xml文件,我知道动画也不会发生。

XML

         <LinearLayout
            android:id="@+id/smsArea"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingTop="12dp"> .....   </LinearLayout>

动画

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate android:duration="300" android:fromXDelta="0%" android:toXDelta="100%"/>
<alpha android:duration="300" android:fromAlpha="1.0" android:toAlpha="0.0" />

1 个答案:

答案 0 :(得分:0)

添加动画监听器:

backDoww.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
           smsArea.setVisibility(View.GONE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
onAnimationEnd中的

隐藏你的视图。