如何将视图动画化并将其下推下方的所有内容?

问题描述:

所以我研究了如何使用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

我给你做了一个简短的例子,我看到它会推下其余的视图。 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

仍是同样的问题,动画视图会覆盖上方的视图顶部。也不会移动它下面的视图。 –