Я пытаюсь переместить кнопку в Android с помощью ObjectAnimator. Я хочу, чтобы вид двигался именно так, чтобы он не перекрывался с предыдущим местоположением и не более того. Поэтому при перемещении кнопки вниз я вычисляю новое местоположение следующим образом: new_location = Current_location_of_view + view_height. А при движении вверх: new_location = Current_location_of_view + view_height. Но чтобы это работало правильно, я должен добавить смещение вверх на 24 dp (72p), я делаю это, вычитая 72 при получении текущего местоположения представления. Откуда такое смещение? Я предполагаю, что функция view.getLocationOnScreen() имеет другую начальную точку оси Y, чем функция ObjectAnimator.ofFloat(view, "y", new_location), но я понятия не имею, почему. Как это правильно сделать?
Я использую этот код:
private void moveViewUpMinimum(View view, int viewHeight){
int currentViewLocation = getViewLocation(view);
Log.e("Actual location", String.valueOf(currentViewLocation));
if(currentViewLocation >viewHeight){
//if there is enough space on top of the screen
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "y", currentViewLocation-viewHeight);
Log.e("Set location:", String.valueOf(currentViewLocation-viewHeight));
objectAnimator.setDuration(100);
objectAnimator.start();
}
else{
//if no more room move the view down
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "y", screenHeight-viewHeight);
objectAnimator.setDuration(100);
objectAnimator.start();
}
}
private void moveViewDownMinimum(View view, int viewHeight){
int currentViewLocation = getViewLocation(view);
Log.e("Actual location", String.valueOf(currentViewLocation));
if(screenHeight - currentViewLocation >= 2*viewHeight){
//if there is enough space on the bottom of the screen
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "y", currentViewLocation+viewHeight);
Log.e("Set location:", String.valueOf(currentViewLocation+viewHeight));
objectAnimator.setDuration(100);
objectAnimator.start();
}
else{
//if no more room move the view up
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(view, "y", 0);
objectAnimator.setDuration(100);
objectAnimator.start();
}
}
private int getViewLocation(View view){
int buttonLoc[] = {0, 0};
view.getLocationOnScreen(buttonLoc);
return buttonLoc[1]-72;//where does this offset come from???
}