Translate Animation 后 View.GONE 不起作用。

3
所以我有一个应用程序,当用户点击按钮时,会对带有按钮的布局(如滑动菜单)进行动画处理,然后如果他再次点击另一个按钮,则必须使第一个布局不可见或消失,然后是新布局。
但是,当我尝试在AnimationStart中使我的带有按钮的布局不可见时,它并没有做到这一点。
我已经尝试了一些来自以下链接的解决方案: 为什么setvisibility在视图动画之后不起作用 Setvisibilityview Gone不会使视图消失 但是都没有起作用!
有任何帮助吗?
Java代码(两个按钮相同)。
 btn_home1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                   layout1.setVisibility(View.VISIBLE);
                   btn_home.setVisibility(View.VISIBLE);
                   btn_book.setVisibility(View.VISIBLE);
                   btn_find_us.setVisibility(View.VISIBLE);
                   btn_menu.setVisibility(View.VISIBLE);

                TranslateAnimation slide = new TranslateAnimation(-100, 0, 0,0 );   
                slide.setDuration(1000);   
                slide.setFillAfter(true);   
                slide.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
     new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
                   btn_home2.setVisibility(View.GONE);
                   btn_book2.setVisibility(View.GONE);
                   btn_find_us2.setVisibility(View.GONE);
                   btn_menu2.setVisibility(View.GONE);
                       layout2.setVisibility(View.GONE);
        }
    }, 0);
                          btn_home.setClickable(false);  
                          btn_book.setClickable(false);  
                          btn_find_us.setClickable(false);  
                          btn_menu.setClickable(false);                           
                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                          btn_home.setClickable(true);  
                          btn_book.setClickable(true);  
                          btn_find_us.setClickable(true);  
                          btn_menu.setClickable(true);      
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });
                btn_menu.startAnimation(slide);
                btn_book.startAnimation(slide);
                btn_find_us.startAnimation(slide);  
                btn_home.startAnimation(slide); 
                layout1.startAnimation(slide);
                }
        });

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#BE2625" >
             <Button
                android:id="@+id/btn_home1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:text="342"
                 />

                <Button
                android:id="@+id/btn_home11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="250dp"
                android:text="34243"
                 />

    <LinearLayout 
        android:id="@+id/lala"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#80000000"
        android:visibility="gone"
        android:orientation="vertical">

         <Button
                android:id="@+id/btn_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                 />
            <Button
                android:id="@+id/btn_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />
            <Button
                android:id="@+id/btn_find_us"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />
            <Button
                android:id="@+id/btn_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

    </LinearLayout>

    <LinearLayout 
        android:id="@+id/lala1"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#80000000"
        android:visibility="gone"
        android:orientation="vertical">

         <Button
                android:id="@+id/btn_home2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                 />
            <Button
                android:id="@+id/btn_book2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />
            <Button
                android:id="@+id/btn_find_us2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />
            <Button
                android:id="@+id/btn_menu2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

    </LinearLayout>
</RelativeLayout>

你在动画后把视图设置为不可见的地方在哪里? - Whitney
更新!我在AnimationStart上执行它,因此布局和按钮都可以不可见或消失。 - pap
2个回答

2

调用View的clearAnimation方法解决了问题。在我的情况下,我想将View设置回原来的位置,这是在使用fillAfter设置为true后进行平移的。


0

我又对这个链接进行了研究:为什么在视图动画后setVisibility不起作用?

并找到了@Chris Knight的答案:

另一种解决方法是将您的动画视图包装在另一个视图中,并设置该包装器视图的可见性。

所以我像他一样使用了两个FrameLayout,然后逐个设置其中一个的setVisibility(View.GONE),因为用户每次只会点击一个按钮,所以每次只会打开一个Slide Menu

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:background="#BE2625" >
             <Button
                android:id="@+id/btn_home1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="150dp"
                android:text="342"
                 />

                <Button
                android:id="@+id/btn_home11"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="250dp"
                android:text="34243"
                 />
    <FrameLayout
        android:id="@+id/lsd1"
        android:layout_height="match_parent"
        android:layout_width="240dp">  

    <LinearLayout 
        android:id="@+id/lala"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#80000000"
        android:visibility="gone"
        android:orientation="vertical">

         <Button
                android:id="@+id/btn_home"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                 />

            <Button
                android:id="@+id/btn_book"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

            <Button
                android:id="@+id/btn_find_us"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

            <Button
                android:id="@+id/btn_menu"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />


    </LinearLayout>
</FrameLayout>     

       <FrameLayout
        android:id="@+id/lsd2"
        android:layout_height="match_parent"
        android:layout_width="240dp">
    <LinearLayout 
        android:id="@+id/lala1"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#80000000"
        android:visibility="gone"
        android:orientation="vertical">

         <Button
                android:id="@+id/btn_home2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                 />

            <Button
                android:id="@+id/btn_book2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

            <Button
                android:id="@+id/btn_find_us2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />

            <Button
                android:id="@+id/btn_menu2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="0.25"
                  />


    </LinearLayout>
</FrameLayout>     

</RelativeLayout>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接