2016-02-11 57 views
0

我正在學習有關的動畫,無法在網上找到一個很好的文件反正我想做到以下幾點:的Android動畫顯示和隱藏查看

  1. 點擊按鈕,隱藏視圖 - 工程確定

    再次
  2. 點擊相同的按鈕來顯示視圖 - 不正常

這樣做的問題是,該視圖只顯示了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); 


           } 


          } 

         }); 
+0

您正在爲不存在的東西製作動畫。您應該將視圖設置爲VISIBLE,等待它計算其佈局和尺寸,並在onDraw()之前爲其設置動畫。 –

回答

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) { 

     } 
     }); 

    } 

    } 
}); 

查詢..?讓我知道。

+0

謝謝,代碼適用於以下2個更改: 1.布爾標誌;更改爲boolean fl; ().tv.animate()。translationY(tv.getHeight() - 100)改爲tv.animate()。translationY(0)。 將這些修改添加到您的答案,我會接受。再次感謝。 (在我的情況下,我用LinearLayout與按鈕,而不是textView) – user3560827

+0

感謝您的評論。您提到的更改已完成。再次感謝您通知它。 –