[英]How to animate to wrap_content?
是否可以使用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);
你可以做這樣的事情。 傳遞最初設置為消失的視圖。
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: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 }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.