[英]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.