帮酷LOGO
0 0 评论
文章标签:animations  Sliding  动画  高度  sli  Layout  


概述

我看到很多以幻燈片方式打開的動畫,通常,這是要更改的視圖的實際布局,然而,如果您只是改變高度,它將只是在閃爍。我給你們展示兩種方法來實現這個動畫。

ValueAnimation

第一種方法是使用ValueAnimation (可以在AnimatorSet中使用),這個動畫會改變設置屬性的值,並沿著拋物線(或者任何你專門設置進插值器的線方程),然後轉到它的每個點,然後讓你創建一個方程,基於該行上的動畫在哪個位置。

//view we want to animate
final View messageView = view.findViewById(R.id.message_section);

//set the values we want to animate between and how long it takes
//to run
ValueAnimator slideAnimator = ValueAnimator
 . ofInt(currentHeight, newHeight)
 . setDuration(300);


//we want to manually handle how each tick is handled so add a
//listener
slideAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator animation) {
 //get the value the interpolator is at
 Integer value = (Integer) animation.getAnimatedValue();
 //I'm going to set the layout's height 1:1 to the tick
 messageView.getLayoutParams().height = value.intValue();
 //force all layouts to see which ones are affected by
 //this layouts height change
 messageView.requestLayout();
 }
});

//create a new animationset
AnimatorSet set = new AnimatorSet();
//since this is the only animation we are going to run we just use
//play
set.play(slideAnimator);
//this is how you set the parabola which controls acceleration
set.setInterpolator(new AccelerateDecelerateInterpolator());
//start the animation
set.start();

自定義動畫效果

另外一個更為極端的解決方案,就是創建一個擴展Animation,它似乎不總是按你想要的方式工作,這比上面的ValueAnimator要靈活得多,而且工作幾乎完全一樣,你可以使構造函數採用新的高度和舊的高度。這也使用了插值器,所以,你仍然可以設置一個自定義方程,下面是一個基本示例:

public class SlideAnimation extends Animation {

 int mFromHeight;
 int mToHeight;
 View mView;

 public SlideAnimation(View view, int fromHeight, int toHeight) {
 this.mView = view;
 this.mFromHeight = fromHeight;
 this.mToHeight = toHeight;
 }

 @Override
 protected void applyTransformation(float interpolatedTime, Transformation transformation) {
 int newHeight;

 if (mView.getHeight()!= mToHeight) {
 newHeight = (int) (mFromHeight + ((mToHeight - mFromHeight) * interpolatedTime));
 mView.getLayoutParams().height = newHeight;
 mView.requestLayout();
 }
 }

 @Override
 public void initialize(int width, int height, int parentWidth, int parentHeight) {
 super.initialize(width, height, parentWidth, parentHeight);
 }

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

以上幾乎完全相同,但是,你必須在某處使用它,所以,看起來像這樣,

View messageView = view.findViewById(R.id.message_section);

Animation animation = new SlideAnimation(messageView, currentHeight, newHeight);

//this interpolator only speeds up as it keeps going
animation.setInterpolator(new AccelerateInterpolator());
animation.setDuration(300);
messageView.setAnimation(animation);
messageView.startAnimation(animation);

結束語

這兩種方法都可以獲得相同的結果,並且可以構建。注意,它並非總是在所有地方工作,當我製作這個自定義動畫類時,當我在一個抽屜中使用它時它不能工作,但是,ValueAnimator 可以.


sliding_animation
</p>



文章标签:动画  sli  Layout  animations  Sliding  高度  

Copyright © 2011 HelpLib All rights reserved.    知识分享协议 京ICP备05059198号-3  |  如果智培  |  酷兔英语