gpt4 book ai didi

Android:添加带有展开动画的 View (不闪烁)

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:14 24 4
gpt4 key购买 nike

我想使用展开动画向 View 组添加一个 View ,因此添加的 View 开始时非常小,并且占用越来越多的空间,直到达到其最大尺寸(可能在此过程中移动其他 View )。

在尝试了不同的方法之后,我想出了下面的解决方案。投票是否对您有帮助,或者请发布更好的选择。我相信一定有更简单的方法来做到这一点。

有关更多信息,请参阅 this one 等帖子和 this one .

下面是我如何添加 View 并启动动画:

// 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);
}

当您使用它时,您可能希望在动画完成后从其父 View 中删除 View :

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);

最佳答案

这可能会更简单一些:

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);
}

关于Android:添加带有展开动画的 View (不闪烁),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com