Android动画隐藏和显示视图

时间:2016-02-11 15:28:51

标签: android

我正在学习动画,无论如何我都不能在网上找到一份好的文件:

  1. 点击按钮隐藏视图 - 工作正常

  2. 再次单击相同按钮以显示视图 - 不正常

  3. 这个问题是视图只在屏幕底部显示大约10%,视图的其余部分是空白/空白区域的90%。不确定我做错了什么。代码如下:

    runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
    
    
                                    if(bottomButtonLayout.getVisibility()==View.VISIBLE)
                                    {
                                        //Shown - Hide It
                                        bottomButtonLayout.animate().translationY(bottomButtonLayout.getHeight()).alpha(0.0f).setDuration(900).setListener(new AnimatorListenerAdapter()
    
    
                                        {
                                               @Override
                                               public void onAnimationEnd(Animator animation) {
                                               super.onAnimationEnd(animation);
                                               bottomButtonLayout.setVisibility(View.GONE);
                                          }
                                               });
    
                                        iconHideView = (ImageButton) findViewById(R.id.icnHideView);
                                        iconHideView.setImageResource(R.drawable.arrow_up);
    
                                    }
                                    else if(bottomButtonLayout.getVisibility()==View.GONE)
                                    {
                                        //Hidden - Show it
                                        bottomButtonLayout.setAlpha(1);
                                        bottomButtonLayout.animate().alpha(1f).setDuration(500).setListener(new AnimatorListenerAdapter()
    
    
                                        {
                                               @Override
                                               public void onAnimationEnd(Animator animation) {
                                               super.onAnimationEnd(animation);
                                               bottomButtonLayout.setVisibility(View.VISIBLE);
                                                       }
                                              });
    
                                        iconHideView = (ImageButton) findViewById(R.id.icnHideView);
                                        iconHideView.setImageResource(R.drawable.arrow_down);
    
    
                                    }  
    
    
                              }
    
                          });
    

1 个答案:

答案 0 :(得分:1)

查看我用你的想法实现的逻辑。它可能会帮助你。

XML代码:

<LinearLayout
   xmlns:android = "http://schemas.android.com/apk/res/android"
   android:layout_width = "match_parent"
   android:layout_height = "match_parent"
   android:background = "#ffffff"
   android:gravity = "center"
   android:orientation = "vertical">

  <TextView
     android:id = "@+id/tv"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:layout_gravity = "center"
     android:gravity = "center"
     android:text = "MOVING TEXT"
     android:textSize = "24sp"/>

  <Button
     android:id = "@+id/but"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:text = "click me"/>

</LinearLayout>

然后,在您的java代码中,编写这些代码段。

Java:

boolean flag;
final TextView tv = (TextView) findViewById(R.id.tv);
Button but = (Button) findViewById(R.id.but);

然后在按钮上单击侦听器实现动画逻辑。

but.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {

    if (!flag) {
      tv.animate().translationY(tv.getHeight()).alpha(0.0f).setDuration(1200).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
          tv.setVisibility(View.GONE);
          flag = true;
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
      });
    } else {
// Part you require
      tv.animate().translationY(0).alpha(1.0f).setDuration(1200).setListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {
          tv.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationEnd(Animator animation) {

          flag = false;
        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
      });

    }

  }
});

查询..?让我知道。