转换像素的分辨率进行MDPI和HDPI屏幕 [英] Converting pixels to dpi for mdpi and hdpi screens

查看:529
本文介绍了转换像素的分辨率进行MDPI和HDPI屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这code,我已经在另一个线程这是工作的罚款于mdpi屏幕发现:

I am using this code that I have found on another thread which is working fine on mdpi screens:

public static float convertDpToPixel(float dp,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float px = dp * (metrics.densityDpi/160f);
        return px;
    }

public static float convertPixelsToDp(float px,Context context){
        Resources resources = context.getResources();
        DisplayMetrics metrics = resources.getDisplayMetrics();
        float dp = px / (metrics.densityDpi /160f);
        return dp;

    }

在对华电国际的屏幕努力增加文字大小时文字大小跳转使用。我,因为它是划分densityDpi在使用硬codeD 160F假设它?是否有任何动态的方式,以确定其是否为160dpi或240dpi?

When using it on hdpi screens the text size jumps when trying to increase the text size. I am assuming its because it is using the hardcoded 160f when dividing the densityDpi? Is there any dynamic way to determine whether it is 160dpi or 240dpi?

推荐答案

试试这个方法:

public float dpToPx(float dp, Context context) {
    float density = context.getResources().getDisplayMetrics().density;
    return dp * density;
}

这篇关于转换像素的分辨率进行MDPI和HDPI屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆