![](/img/trans.png)
[英]android - how to get the image edge x/y position inside imageview
[英]Android - Send imageView to X Y position
我有一个从右侧屏幕开始的imageViewA。 我想以编程方式设置X和Y位置。 为了将imageView移动到左上角附近,我必须设置X = -1497 Y =20。我不明白的是为什么X = -1497。 我认为是因为它占据了imageView开始的(0,0)位置,但是如何捕捉左上方屏幕的(0,0)?
这是因为,对于所有屏幕,我都必须计算%才能始终将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();
}
招呼
translationX相对于视图有效。 尝试这个,
objectX = ObjectAnimator.offFloat(view, "X", 20);
objectY = ObjectAnimator.offFloat(view, "Y", 20);
实际上,您可以使用ViewPropertyAnimator而不是ObjectAnimator剪切大多数代码。
public void moveImageView(View view){
view.animate().translationX(0).translationY(0).setDuration(500);
}
多数民众赞成在您需要的所有代码,应将您的视图移动到左上角。
您可以随时为以后的动画增强方法,例如:
// 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);
}
然后像这样调用它:
moveImageView(yourImageView, 0, 0, 500);
要动态获取设备的坐标,以便您知道要移动的位置:
float screenWidth = getResources().getDisplayMetrics().widthPixels;
float screenHeight = getResources().getDisplayMetrics().heightPixels;
screenWidth + screenHeight将是右下角的坐标。
左上角的坐标只是0、0。
屏幕坐标的中心在逻辑上为(screenWidth / 2)+(screenHeight / 2)。
希望以后可以让您的生活更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.