我使用这个代码,我已经在另一个线程发现这是工作的罚款于mdpi屏幕到新闻部MDPI和HDPI屏幕:转换像素
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;
}
当使用它HDPI屏幕上的文字大小跳跃时试图增加文字大小。我假设它是因为它在使用硬编码160f时除以densityDpi?有没有什么动态的方法来确定它是160dpi还是240dpi?