如何为wrap_content设置动画? [英] How to animate to wrap_content?

查看:179
本文介绍了如何为wrap_content设置动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用ValueAnimatorwrap_content进行动画处理?这似乎只适用于常量值.

Is it possible to animate using ValueAnimator to wrap_content? this only seems to work with constant values.

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

推荐答案

您可以执行以下操作.通过最初设置为不可用的视图.

You can do something like this. Pass a view that is set initially to gone.

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

这篇关于如何为wrap_content设置动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆