gpt4 book ai didi

android - 如何为 wrap_content 设置动画?

转载 作者:搜寻专家 更新时间:2023-11-01 09:21:13 27 4
gpt4 key购买 nike

是否可以使用 ValueAnimatorwrap_content 制作动画?这似乎只适用于常量值。

public static void valueAnimate(final View obj, int from, int to, Interpolator interpolator, long duration, long delay){

ValueAnimator anim = ValueAnimator.ofInt(from, to);
anim.setInterpolator(interpolator == null ? DEFAULT_INTERPOLATOR : interpolator);
anim.setDuration(duration);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
obj.getLayoutParams().height = value.intValue();
obj.requestLayout();
}
});
anim.setStartDelay(delay);
anim.start();
}

如何将 to 参数作为 wrap_content 传递?

Animator.valueAnimate(mapUtilsContainer, CURR_MAPUTILC_H, 800, OVERSHOOT, 300, 0);

最佳答案

你可以这样做。传递最初设置为 gone 的 View 。

public static void expand(final View view) {
view.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
final int targetHeight = view.getMeasuredHeight();

// Set initial height to 0 and show the view
view.getLayoutParams().height = 0;
view.setVisibility(View.VISIBLE);

ValueAnimator anim = ValueAnimator.ofInt(view.getMeasuredHeight(), targetHeight);
anim.setInterpolator(new AccelerateInterpolator());
anim.setDuration(1000);
anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = (int) (targetHeight * animation.getAnimatedFraction());
view.setLayoutParams(layoutParams);
}
});
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
// At the end of animation, set the height to wrap content
// This fix is for long views that are not shown on screen
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
}
});
anim.start();
}

关于android - 如何为 wrap_content 设置动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55251650/

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