Java – use valueanimator to scale a view to the size of another view
•
Android
I'm trying to use valueanimator to resize the view to fit another view. I'm using it instead of animation because I need a view to click later
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 above code shows a method. I try to increase the height of the view to the desired height, and I have the same method for width, but width is not height
The desired behavior is for the view to increase its size until it is as large as the target view, but for some strange reasons, the view moves rather than resizes
The implementation class is an extension of ImageView, but the method of ImageView has not changed
What can I do to animate the size of a view to the size of another view?
resolvent:
Just add the following methods to the code
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;
}
And call it on any event. (for example, click)
getViewScaleAnimator(fromView, targetView).setDuration(1000).start();
This is an output
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码