2017-10-19 52 views
0

所以我研究瞭如何使用this thread動畫淡化和下拉/滑動視圖的動畫,但它沒有如預期的那樣工作。首先,這裏是我使用的動畫代碼:如何將視圖動畫化並將其下推下方的所有內容?

public void toggleAdvancedVisibility(View text) { //text is a clickable textview thats acts as a toggle 
    int dur = 1000; 
    final View advView = findViewById(R.id.enc_advanced); 
    if(advView.getVisibility() == View.GONE && animationDone) { 
     advView.setVisibility(View.VISIBLE); 
     advView.setAlpha(0.0f); 

     //animate fade + drop down 
     advView.animate() 
       .setDuration(dur) 
       .translationY(advView.getHeight()) 
       .alpha(1.0f) 
       .setListener(new AnimatorListenerAdapter() { 
        @Override 
        public void onAnimationEnd(Animator animation) { 
         super.onAnimationEnd(animation); 
         animationDone = true; 
        } 
       }); 
     animationDone=false; 
    } 
    else if(advView.getVisibility() == View.VISIBLE && animationDone) { 
     //animate fade + slide up 
     advView.animate() 
       .setDuration(dur) 
       .translationY(0) 
       .alpha(0.0f) 
       .setListener(new AnimatorListenerAdapter() { 
        @Override 
        public void onAnimationEnd(Animator animation) { 
         super.onAnimationEnd(animation); 
         advView.setVisibility(View.GONE); 
         animationDone = true; 
        } 
       }); 
     animationDone = false; 
    } 
} 

正如我所說的,雖然有動畫,這並不像預期的附近的任何地方採取行動。

問題#1 視圖幾乎被推出了可見性。我相信這是由於.translationY(advView.getHeight())這條線的原因,就好像我將動畫前的視圖位置設置爲advView.setTranslationY(-advView.getHeight()),然後動畫效果爲.translationY(0)它會轉到它應該設置的位置。

這個問題的一個明顯問題是,當視圖是動畫時,視圖在它完成之前與視圖「碰撞」。那麼,如何正確地將它滑入/滑出而不會碰到上面的視圖?

問題#2 動畫並不完全「推」下來,這是我的預期。我的意思是,動畫視圖也有一個低於它的視圖。我預計下面的視圖會被動畫視圖壓低。雖然我還沒有嘗試過,但我認爲這可以通過將相同的動畫設置到其下面的視圖來模擬,但還有其他方法嗎?

我對這個動畫的東西很陌生,並且操作這樣的視圖,所以任何幫助表示讚賞。

+0

使用'android.support.design.widget.BaseTransientBottomBar'作爲基類或(如果您需要不同的行爲)查看其源代碼以瞭解它是如何完成的 – pskink

+0

@pskink您是否有特定的鏈接或源我可以使用?我正在閱讀文檔,我相信我在某種程度上理解我應該做什麼,但不確定。我也找不到使用它的任何好例子。 –

+0

問谷歌叔叔'BaseTransientBottomBar.java' – pskink

回答

0

我給你做了一個簡短的例子,我看到它會推下其餘的視圖。 XML:

<LinearLayout 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" 
tools:context="com.example.teststuff.MainActivity" 
android:orientation="vertical"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/b1" 
    android:text="show"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/tv1" 
    android:text="Hello World!" 
    android:visibility="gone"/> 
<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hello World!!!!"/> 

而這裏的.java:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    tv1 = (TextView) findViewById(R.id.tv1); 
    tv2 = (TextView) findViewById(R.id.tv2); 
    b1 = (Button) findViewById(R.id.b1); 

    b1.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      if (tv1.isShown()){ 
       tv1.startAnimation(slideInAnimation(view)); 
       tv1.setVisibility(View.GONE); 
      } 
      else { 
       tv1.startAnimation(slideOutAnimation(view)); 
       tv1.setVisibility(View.VISIBLE); 
      } 
     } 
    }); 


} 

private TranslateAnimation slideOutAnimation(View view){ 
    TranslateAnimation animate = new TranslateAnimation(0,0,-view.getHeight(),0); 
    animate.setDuration(500); 
    animate.setFillAfter(false); 
    return animate; 
} 
private TranslateAnimation slideInAnimation(View view){ 
    TranslateAnimation animate = new TranslateAnimation(0,0,0,-view.getHeight()); 
    animate.setDuration(500); 
    animate.setFillAfter(true); 
    return animate; 
} 

它工作正常的我。

+0

問題依然存在。也許是因爲我試圖移動整個'LinearView'? –

+0

你能解釋一下你現在確切的問題嗎?它不會推低你的觀點?它會過來嗎? – Laur89

+0

仍是同樣的問題,動畫視圖會覆蓋上方的視圖頂部。也不會移動它下面的視圖。 –

相關問題