2013-06-27 21 views
1

在我的活动屏幕中,一半的屏幕包含一个布局。当活动加载它可见时,10秒钟后它会慢慢下降,最终它不会被用户看到,但它慢慢下降。我该怎么做请问任何人都可以帮助我。如何在android中设置动画布局?

感谢提前。

+0

[Rü显示和隐藏视图知名度?和你想显示垂直刻度动画?这里是你的代码? – TheFlash

回答

11

在你res\anim文件夹(创建文件夹,如果它不存在)创建slide_out_down.xml并粘贴以下

<?xml version="1.0" encoding="utf-8"?> 
<translate 
xmlns:android="http://schemas.android.com/apk/res/android" 

android:fromYDelta="0%p" 
android:toYDelta="100%p" 
android:duration="@android:integer/config_longAnimTime" /> 

开始动画并隐藏视图使用此

private void hideView(final View view){ 
    Animation animation = AnimationUtils.loadAnimation(this, R.anim.slide_out_down); 
    //use this to make it longer: animation.setDuration(1000); 
    animation.setAnimationListener(new AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) {} 

     @Override 
     public void onAnimationRepeat(Animation animation) {} 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      view.setVisibility(View.GONE); 
     } 
    }); 

    view.startAnimation(animation); 
} 
+0

你的代码是帮助完整,但我有布局,即时应用此翻译是在其他布局的中间。翻译是在底部布局。我想从底部布局后面发生翻译。 – user2466123

+0

你能解释一下你需要多一点吗? – Plato

+0

在我的父级布局中,分为3部分.1和3部分固定为页眉和页脚,2部分为花版布局。当我的活动加载时,2部分将在1和3之间缓慢放下布局。但是,您的代码,布局在第三个布局上下来。 – user2466123

1

您可以使用FragmentActivity和片段,这和添加动画片段

<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:interpolator="@android:anim/accelerate_interpolator"> 
<scale 
    android:fromXScale="1.0" android:toXScale="0.0" 
    android:fromYScale="1.0" android:toYScale="0.0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:duration="1000" 
/> 

0
try this: 

// gone layout 
    collapse(recipientLayout); 
//show layout 
     expand(recipientLayout); 

    public void expand(final LinearLayout v) { 
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT); 
    final int targtetHeight = v.getMeasuredHeight(); 
    /*if (v.isShown()) { 
     collapse(v); 
    } else */{ 
     v.getLayoutParams().height = 0; 
     v.setVisibility(View.VISIBLE); 
     Animation a = new Animation() { 
      @Override 
      protected void applyTransformation(float interpolatedTime, 
               Transformation t) { 
       v.getLayoutParams().height = interpolatedTime == 1 ? LinearLayout.LayoutParams.WRAP_CONTENT 
         : (int) (targtetHeight * interpolatedTime); 
       v.requestLayout(); 
      } 

      @Override 
      public boolean willChangeBounds() { 
       return true; 
      } 
     }; 
     a.setDuration((int) (targtetHeight + 600)); 
     v.startAnimation(a); 
    } 

} 

public void collapse(final LinearLayout v) { 
    final int initialHeight = v.getMeasuredHeight(); 
    Animation a = new Animation() { 
     @Override 
     protected void applyTransformation(float interpolatedTime, 
              Transformation t) { 
      /*if (v.isShown()) { 
       collapse(v); 
      }*/ 
      if (interpolatedTime == 1) { 
       v.setVisibility(View.GONE); 
      } else { 
       v.getLayoutParams().height = initialHeight 
         - (int) (initialHeight * interpolatedTime); 
       v.requestLayout(); 
      } 
     } 

     @Override 
     public boolean willChangeBounds() { 
      return true; 
     } 
    }; 

    a.setDuration((int) (v.getLayoutParams().height + 600)); 
    v.startAnimation(a); 
} 
0

public void animateLayout(){ LinearLayout layout = findViewById(R.id.layoutId); layout.animate().translationYBy(1000f).setDuration(50000); }

上面的代码将使视图去无形非常缓慢。

setDuration(50000) //change the number according to your need. It varies the speed of the layout.