Перемещение представления на Android с помощью objectAnimator, получение смещения 24dp (72p)

Я пытаюсь переместить кнопку в 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???
}

person Ivars Briedis    schedule 06.04.2017    source источник


Ответы (1)


Я исправил проблему, используя view.getY() вместо view.getLocationOnScreen(). Функция getLocationOnScreen() вернула значение на 72 пикселя больше, чем view.getY(), и я предполагаю, что это значение — моя строка состояния. Я должен был использовать функцию getY() с самого начала, так как это значение, которое я использую в функции ObjectAnimator.ofFloat(view, "y", newLocation).

person Ivars Briedis    schedule 07.04.2017