Android:使用展开animation添加视图(不闪烁)

我想使用展开的animation为视图组添加视图,所以添加的视图开始非常小,并占用越来越多的空间,直到达到其全部大小(可能移动过程中的其他视图)。

尝试不同的方法后,我想出了下面的解决scheme。 投票,如果它可以帮助你或请张贴一个更好的select。 我相信肯定有一个更简单的方法来做到这一点。

欲了解更多信息,请参阅这个和这一个职位。

以下是我如何添加视图并开始animation:

// Trick: set height to 1 so the view doesn't take its full size at the beginning. // (0 doesn't work, I don't know why) container.addView(view, LayoutParams.MATCH_PARENT, 1); Animation expansion = createExpansion(view); expansion.setDuration(200); view.startAnimation(expansion); 

createExpansion()方法:

 public static Animation createExpansion(View view) { return new SizedHeightScaleAnimation(view, 0, 1, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); } 

最后是SizedHeightScaleAnimationanimation类:

 /** * Like {@link ScaleAnimation} for height scaling that also resizes the view accordingly, * so it takes the right amount of space while scaling. */ public class SizedHeightScaleAnimation extends ScaleAnimation { private float fromHeight; private float toHeight; private View viewToScale; public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY) { super(1, 1, fromY, toY); init(viewToScale, fromY, toY); } public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, float pivotX, float pivotY) { super(1, 1, fromY, toY, pivotX, pivotY); init(viewToScale, fromY, toY); } public SizedHeightScaleAnimation(View viewToScale, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) { super(1, 1, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue); init(viewToScale, fromY, toY); } private void init(View viewToScale, float fromY, float toY) { this.viewToScale = viewToScale; viewToScale.measure(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT); fromHeight = viewToScale.getMeasuredHeight() * fromY; toHeight = viewToScale.getMeasuredHeight() * toY; } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); final int newHeight = (int) (fromHeight * (1 - interpolatedTime) + toHeight * interpolatedTime); viewToScale.getLayoutParams().height = newHeight; viewToScale.requestLayout(); } } 

顺便说一句,你可以这样创build崩溃效果:

 public static Animation createCollapse(View view) { return new SizedHeightScaleAnimation(view, 1, 0, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f); } 

当你使用它的时候,你可能想要在animation完成的时候从其父视图中移除视图:

 Animation collapse = createCollapse(view); collapse.setDuration(200); collapse.setAnimationListener(new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { final ViewGroup parent = (ViewGroup) view.getParent(); parent.removeView(view); } ... other overrides ... }); view.startAnimation(collapse); 

Solutions Collecting From Web of "Android:使用展开animation添加视图(不闪烁)"

这可能会更简单一些:

 public void expand(final View view) { view.getLayoutParams().height = 0; Animation a = new Animation() { @Override protected void applyTransformation(float interpolatedTime, Transformation t) { view.getLayoutParams().height = (int)(mTargetHeight * interpolatedTime); view.requestLayout(); } @Override public boolean willChangeBounds() { return true; } }; a.setDuration(1000); view.startAnimation(a); }