【问题标题】:Android animation not working on isShown methodAndroid动画不适用于isShown方法
【发布时间】:2017-02-07 12:24:49
【问题描述】:

我有一个动画不工作,应该在单击按钮时触发,单击检查图像是否显示并运行动画,如果是,则在 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 中作为默认可行的左侧。我知道错误不在我的动画文件中,因为即使我在 else 中使用 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" />

【问题讨论】:

    标签: java android android-layout android-animation


    【解决方案1】:

    添加动画监听器:

    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 中隐藏你的视图。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多