1
有转换DP没有上下文到PX的非常简洁的方式,它是这样的:转换DP没有上下文,以PX
public static int dpToPx(int dp) {
float density = Resources.getSystem().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
在每一个谷歌的例子在Google GitHub page他们使用以下方法:
public static int convertDpToPixel(Context ctx, int dp) {
float density = ctx.getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
那么,有什么问题的第一种方法?对我来说,它在我的所有应用程序中都能正常工作,但我想知道是否有某些情况可能会失败?
感谢。在我的情况下,应用程序没有“投射”选项,但它可能会在奇怪的设备上失败[如这个](https://www.asus.com/us/Phone/ASUS_PadFone_X_US/)。 –