Android - pixels to dips

后端 未结 4 1922
我寻月下人不归
我寻月下人不归 2020-12-10 08:07

I have an image of a face (250px X 250px) that is in an absolute layout element. I currently get the user\'s touch coordinates and using some maths calculate what has been t

4条回答
  •  执笔经年
    2020-12-10 09:12

    I have three useful functions in my library...

    get Screen Density

    public static float getDensity(Context context){
        float scale = context.getResources().getDisplayMetrics().density;       
        return scale;
    }
    

    convert Dip to Pixels.

    public static int convertDiptoPix(int dip){
        float scale = getDensity();
        return (int) (dip * scale + 0.5f);
    }
    

    convert Pixels to Dips.

    public static int convertPixtoDip(int pixel){
        float scale = getDensity();
        return (int)((pixel - 0.5f)/scale);
    }
    

提交回复
热议问题