是否可以使用ValueAnimator
到wrap_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);
答案 0 :(得分:1)
您可以执行以下操作。通过最初设置为不可用的视图。
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();
}
答案 1 :(得分:1)
一个简单的解决方案是在包含组件的布局中使用android:animateLayoutChanges属性。这适用于Build.VERSION.SDK_INT> = Build.VERSION_CODES.JELLY_BEAN(Android 4.3)。 例如,我有一个EditText,它需要从某个高度更改为wrap_content。
将android:animateLayoutChanges属性添加到我的ScrollView。
< ScrollView
...
android:animateLayoutChanges="true">
将此添加到您的onCreateView()
scrollView.layoutTransition.enableTransitionType(LayoutTransition.CHANGING)
然后,您将看到EditText高度的相当平滑的变化。
将EditText的高度更改为wrap_content
的代码 fun wrapText() {
val layoutParams = this.layoutParams
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT
this.layoutParams = layoutParams
isExpanded = false
}