首页 > 解决方案 > 将 ImageView 的大小调整为另一个 ImageView 的大小的动画

问题描述

我有一张想要制作动画并移动到另一个 ImageView 位置的图片。

目前这就是我正在做的事情:

                v.animate()
                    .scaleX(??)
                    .scaleY(??)
                    .x(target.x)
                    .y(target.y)
                    .setDuration(1000)
                    .start()

我的问题是如何计算 x 和 y 的正确比例因子?如果我将布局参数设置为等于目标的布局参数,那么它可以正常工作,但它不是动画的。我尝试将源图像的宽度和高度与目标图像相除,但它并没有给我正确的比例。

谢谢你的帮助

标签: javaandroidkotlin

解决方案


为两个图像之间的比率创建一个浮点值:

float w_ratio = (targetImage.getMeasuredWidth() - sourceImage.getMeasuredWidth()) / sourceImage.getMeasuredWidth();
float h_ratio = (targetImage.getMeasuredHeight() - sourceImage.getMeasuredHeight()) / sourceImage.getMeasuredHeight();

// then animate:

v.animate().scaleXBy(w_ratio).scaleYBy(h_ratio).setDuration(1000);

// Don't forget to reset the size later if needed, using:

v.setScaleX(1.0f);
v.setScaleY(1.0f);

推荐阅读