【问题标题】:moving a View from Random x and y positions to specific X and Y positions with animations使用动画将视图从随机 x 和 y 位置移动到特定的 X 和 Y 位置
【发布时间】:2021-02-23 15:05:45
【问题描述】:
我有一个图像视图。我在约束布局上放置了特定位置(特定 x 和 y)。我想将此图像视图从随机 x y 位置(例如屏幕外)移动(与动画一起飞行)到我之前在约束布局上放置的特定位置。
我试过翻译动画。没用。
{imageView4.animate().yBy(-200).y(imageView4.getY()).xBy(-200).x(imageView4.getX()).setDuration(4000);}
【问题讨论】:
标签:
android
animation
imageview
move
translate-animation
【解决方案1】:
方法yBy(value) 允许您将元素从当前坐标相对移动到value。方法y(value) 允许您将元素移动到绝对value 坐标。所以你需要写这段代码:
imageView4.setX(startValueX);
imageView4.setY(startValueY);
imageView4.animate().x(endValueX).y(endValueY).setDuration(4000);
请注意,以像素为单位给出的值。如果你需要 dp,你必须将 px 转换为 dp。