Android view's getTop(), getLeft(), getX(), getY(), getWidth(), getHeight() methods

后端 未结 3 1763
我寻月下人不归
我寻月下人不归 2021-02-19 08:02

I am writing a drag and drop application and got really confused because of some parameters.

Please help to figure out.

First of all, I read the documentation fo

3条回答
  •  甜味超标
    2021-02-19 08:33

    All these measurement methods return sizes in pixels( px ), not density-pixels ( dp ). If you want to convert it you can get the density by calling:

    float density = getResources().getDisplayMetrics().density;
    

    And then divide the values you get with the provided density, for example:

    int widthDp = (int)(img.getWidth() / density);
    

提交回复
热议问题