Scale a view to a different view size with ValueAnimator

I am trying to resize a view to fit a different view size using ValueAnimator. I use it instead of animation because I need the view to be available after clicking.

private Animator getHeightScaleAnimator(View target) {
    ConstraintLayout.LayoutParams thisParams = (ConstraintLayout.LayoutParams) getLayoutParams();
    ConstraintLayout.LayoutParams targetParams = (ConstraintLayout.LayoutParams) target.getLayoutParams();

    int currentHeight = thisParams.height;
    int desiredHeight = targetParams.height;

    ValueAnimator animator = ValueAnimator.ofInt(currentHeight, desiredHeight);
    animator.addUpdateListener(animation -> {
        int newInt = (int) animation.getAnimatedValue();
        thisParams.width = newInt;
        invalidate();
        requestLayout();
    });
    return animator;
}

The code above shows a method in which I am trying to increase the height of the view to the required height of the view, and I have the same method for the width, but with the width instead of the height.

The desired behavior was the view, in order to increase its size until it reaches the target view, but for some odd reason, the view moves instead of resizing.

The implementation class is an extension of ImageView, but the method from ImageView has not been changed.


, ?

+4
2

.

private Animator getViewScaleAnimator(View from, final View target) {
        // height resize animation
        AnimatorSet animatorSet = new AnimatorSet();
        int desiredHeight = from.getHeight();
        int currentHeight = target.getHeight();
        ValueAnimator heightAnimator = ValueAnimator.ofInt(currentHeight, desiredHeight);
        heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) target.getLayoutParams();
                params.height = (int) animation.getAnimatedValue();
                target.setLayoutParams(params);
            }
        });
        animatorSet.play(heightAnimator);

        // width resize animation
        int desiredWidth = from.getWidth();
        int currentWidth = target.getWidth();
        ValueAnimator widthAnimator = ValueAnimator.ofInt(currentWidth, desiredWidth);
        widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) target.getLayoutParams();
                params.width = (int) animation.getAnimatedValue();
                target.setLayoutParams(params);
            }
        });
        animatorSet.play(widthAnimator);
        return animatorSet;
    }

(, )

getViewScaleAnimator(fromView, targetView).setDuration(1000).start();

.

enter image description here

+3

, ValueAnimator ScaleAnimation:

float scaleY = (float) currentHeight / (float) desiredHeight;
curView.animate().scaleY(scaleY);

, , , scaleY .

, .setDuration(1000), .

0

Source: https://habr.com/ru/post/1676882/


All Articles