【问题标题】:View.GONE doesn't work after Translate Animation翻译动画后 View.GONE 不起作用
【发布时间】:2014-05-02 04:09:14
【问题描述】:

所以我有一个应用程序,当用户点击button 时,将使用按钮(如sliding menu)为layout 执行animation,然后如果他点击另一个按钮,它必须使其不可见或走了第一个布局,然后是新的布局。

但是当我尝试在AnimationStart 上设置我的layout 时按钮不可见时,它不会那样做。

我已经从这里尝试了一些解决方案:

Why doesn't setvisibility work after a view is animated

Setvisibilityview Gone doesn't disappear a view

但没有任何效果!

有什么帮助吗??

Java code(两个按钮都一样)

 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>

【问题讨论】:

  • 动画后你在哪里将视图设置为不可见?
  • 更新!我在 AnimationStart 上执行此操作,因此布局和按钮都可以不可见或消失。

标签: android animation


【解决方案1】:

调用 View 的 clearAnimation 解决了这个问题。就我而言,我想在将 fillAfter 设置为 true 进行翻译后将 View 设置回其原始位置。

【讨论】:

    【解决方案2】:

    我又研究了这个链接:Why doesn't setVisibility work after a view is animated?

    并找到了@Chris Knight 的答案:

    Another way to work around this is to wrap your animated view in another view and set the visibility of that wrapper view.

    所以我像他一样使用了两个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>
    

    【讨论】:

      猜你喜欢
      • 2018-09-06
      • 1970-01-01
      • 2018-09-28
      • 2019-10-16
      • 2015-08-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      相关资源
      最近更新 更多