[英]Android - Send imageView to X Y position
i have an imageView A that starts at right screen. 我有一个从右侧屏幕开始的imageViewA。 I want to set X and Y position programatically. 我想以编程方式设置X和Y位置。 To move the imageView near the top left corner, i have to set a X = -1497 Y = 20. What i dont understand is why X = -1497. 为了将imageView移动到左上角附近,我必须设置X = -1497 Y =20。我不明白的是为什么X = -1497。 I think because it tooks the (0,0) position where the imageView starts, but how to catch the (0,0) at top left screen? 我认为是因为它占据了imageView开始的(0,0)位置,但是如何捕捉左上方屏幕的(0,0)?
This is because, for all screen i have to calculate a % to move the imageView to same place always, but how to do it with negative values. 这是因为,对于所有屏幕,我都必须计算%才能始终将imageView移到同一位置,但是如何使用负值来实现。
Point origImagePos = new Point(-1460, 20);
public void moveImageView(View view){
ObjectAnimator objectX;
ObjectAnimator objectY;
AnimatorSet animatorXY;
objectX = ObjectAnimator.offFloat(view, "translationX", origImagePos.x);
objectY = ObjectAnimator.offFloat(view, "translationY", origImagePos.y);
animatorXY.playTogether(objectX, objectY);
animatorXY.setDuration(500);
animatorXY.start();
}
Greets 招呼
translationX works relative to view. translationX相对于视图有效。 Try this, 尝试这个,
objectX = ObjectAnimator.offFloat(view, "X", 20);
objectY = ObjectAnimator.offFloat(view, "Y", 20);
Actually you could cut most your code using ViewPropertyAnimator instead of ObjectAnimator. 实际上,您可以使用ViewPropertyAnimator而不是ObjectAnimator剪切大多数代码。
public void moveImageView(View view){
view.animate().translationX(0).translationY(0).setDuration(500);
}
thats all the code you need and should move your View to the top left corner. 多数民众赞成在您需要的所有代码,应将您的视图移动到左上角。
You could always enhance your method for later animations like this for example : 您可以随时为以后的动画增强方法,例如:
// You should also always use Interpolators for more realistic motion.
public void moveImageView(View view, float toX, float toY, int duration){
view.animate()
.setInterpolator(new AccelerateDecelerateInterpolator())
.translationX(toX)
.translationY(toY)
.setDuration(duration);
}
and then call it like : 然后像这样调用它:
moveImageView(yourImageView, 0, 0, 500);
To get the coordinates of the device dynamically so you know where to move : 要动态获取设备的坐标,以便您知道要移动的位置:
float screenWidth = getResources().getDisplayMetrics().widthPixels;
float screenHeight = getResources().getDisplayMetrics().heightPixels;
screenWidth + screenHeight would be the coordinates of the right bottom corner. screenWidth + screenHeight将是右下角的坐标。
Top Left corner coordinates are simply 0, 0. 左上角的坐标只是0、0。
Center of screen coordiantes are logically (screenWidth / 2) + (screenHeight / 2). 屏幕坐标的中心在逻辑上为(screenWidth / 2)+(screenHeight / 2)。
Hope that makes your life easier in the future. 希望以后可以让您的生活更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.