在DialogFragment上使用动画进行缩放的布局

时间:2018-09-03 00:11:32

标签: android android-layout android-fragments android-animation

我有一个DialogFragment想要设置动画,以便在onClick()之后创建一个确认。 我曾尝试将setVisibility()与Animator配合使用,但这不是我想要的。我希望版式可以滑入,动画不会在之后出现,或者相反在之前消失。

我在这里https://github.com/ThePreviousOne/Example正在使用Github的一些代码  `

    handle.setOnClickListener( new View.OnClickListener() {
            float startHeight;

            @Override
            public void onClick(View v) {

            startHeight = slideDownView.getHeight();

            // Adjust the slide down height immediately with touch movements.
                if (down) {
                LayoutParams params = slideDownView.getLayoutParams();
                params.height = (int) (startHeight - 300);
                slideDownView.setLayoutParams(params);
                down = false;
            } else {
                LayoutParams params = slideDownView.getLayoutParams();
                params.height = (int) (startHeight + 300);
                slideDownView.setLayoutParams(params);
                down = true;
            }
        }
    });

这行得通,但是我不知道如何将新代码连接到动画师,所以我可以控制片段调整大小的速度

1 个答案:

答案 0 :(得分:0)

您可以定义一个自定义动画器,以更新动画每一帧的height属性。例如:

int startHeight = slideDownView.getHeight();
// Note that you should not hardcode "300" as that will be different pixel values on
// different devices - get the value from a dimen resource or scale by the
// device density
int endHeight = startHeight - getDistanceToAnimate();

// Create a simple int animator that animates between the starting and ending height
ValueAnimator animator = ValueAnimator.ofInt(startHeight, endHeight);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator valueAnimator) {
        // On each frame, update the view height
        int value = (Integer) valueAnimator.getAnimatedValue();
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        layoutParams.height = value;
        view.setLayoutParams(layoutParams);
    }

    @Override
    public void onAnimationEnd(Animator animation) {
       // Once the animation finishes, you might have to update the view's final
       // height and / or its `layout_height` attribute.
    }
});

animator.setDuration(getAnimationTime());
animator.start();

希望有帮助!