我想使用扩展动画向视图组添加视图,因此添加的视图开始非常小并占用越来越多的空间,直到达到其完整大小(可能在此过程中移动其他视图)。
尝试不同的方法后,我想出了下面的解决方案。投票,如果它可以帮助您或请发布一个更好的选择。我相信必须有一种更简单的方法来做到这一点。
有关详细信息,请参阅this one和this one等帖子。
以下是我添加视图并启动动画的方法:
// 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);
}
最后是SizedHeightScaleAnimation
动画类:
/**
* 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();
}
}
顺便说一下,你可以用这种方式创建折叠效果:
public static Animation createCollapse(View view) {
return new SizedHeightScaleAnimation(view, 1, 0,
Animation.RELATIVE_TO_SELF, 0f,
Animation.RELATIVE_TO_SELF, 0f);
}
当您使用它时,您可能希望在动画完成时从其父视图中删除视图:
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);
答案 0 :(得分:6)
这可能有点简单:
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);
}