我已经为分辨率为480x800的Pantech设备创建了以像素为单位的高度和宽度的应用程序。
我需要转换G1设备的高度和宽度。 我认为将其转换为dp将解决问题,并为两个设备提供相同的解决方案。
有没有什么简单的方法将像素转换为dp? 有什么建议吗?
我已经为分辨率为480x800的Pantech设备创建了以像素为单位的高度和宽度的应用程序。
我需要转换G1设备的高度和宽度。 我认为将其转换为dp将解决问题,并为两个设备提供相同的解决方案。
有没有什么简单的方法将像素转换为dp? 有什么建议吗?
当前回答
开发者文档提供了如何将dp单位转换为像素单位的答案:
在某些情况下,您需要用dp表示尺寸,然后 把它们转换成像素。dp单位到屏幕像素的转换是 简单: Px = dp * (dpi / 160)
提供了两个代码示例,一个Java示例和一个Kotlin示例。下面是Java示例:
// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels...
Kotlin的例子:
// The gesture threshold expressed in dp
private const val GESTURE_THRESHOLD_DP = 16.0f
...
private var mGestureThreshold: Int = 0
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Get the screen's density scale
val scale: Float = resources.displayMetrics.density
// Convert the dps to pixels, based on density scale
mGestureThreshold = (GESTURE_THRESHOLD_DP * scale + 0.5f).toInt()
// Use mGestureThreshold as a distance in pixels...
}
我从Java例子中创建的Java方法工作得很好:
/**
* Convert dp units to pixel units
* https://developer.android.com/training/multiscreen/screendensities#dips-pels
*
* @param dip input size in density independent pixels
* https://developer.android.com/training/multiscreen/screendensities#TaskUseDP
*
* @return pixels
*/
private int dpToPixels(final float dip) {
// Get the screen's density scale
final float scale = this.getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
final int pix = Math.round(dip * scale + 0.5f);
if (BuildConfig.DEBUG) {
Log.i(DrawingView.TAG, MessageFormat.format(
"Converted: {0} dip to {1} pixels",
dip, pix));
}
return pix;
}
这个问题的其他几个答案提供了与这个答案类似的代码,但其他答案中没有足够的支持文档,让我知道其他答案中的一个是否正确。公认的答案也不同于这个答案。我认为开发人员文档已经清楚地说明了Java解决方案。
在Compose中也有一个解决方案。Compose中的密度为设备无关像素(dp)和像素之间的转换提供了一行解决方案。
val sizeInPx = with(LocalDensity.current) { 16.dp.toPx() }
其他回答
对我来说是这样的:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int h = displaymetrics.heightPixels;
float d = displaymetrics.density;
int heightInPixels=(int) (h/d);
您可以对宽度执行相同的操作。
你应该像使用像素一样使用dp。他们就是这样;显示独立像素。在中等密度的屏幕上使用相同的数字,在高密度的屏幕上大小将神奇地正确。
然而,听起来你需要的是布局设计中的fill_parent选项。当您希望视图或控件扩展到父容器中的所有剩余大小时,请使用fill_parent。
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @param context Context to get resources and device specific display metrics
* @return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(float dp, Context context){
return dp * ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
/**
* This method converts device specific pixels to density independent pixels.
*
* @param px A value in px (pixels) unit. Which we need to convert into db
* @param context Context to get resources and device specific display metrics
* @return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(float px, Context context){
return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
开发者文档提供了如何将dp单位转换为像素单位的答案:
在某些情况下,您需要用dp表示尺寸,然后 把它们转换成像素。dp单位到屏幕像素的转换是 简单: Px = dp * (dpi / 160)
提供了两个代码示例,一个Java示例和一个Kotlin示例。下面是Java示例:
// The gesture threshold expressed in dp
private static final float GESTURE_THRESHOLD_DP = 16.0f;
// Get the screen's density scale
final float scale = getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
mGestureThreshold = (int) (GESTURE_THRESHOLD_DP * scale + 0.5f);
// Use mGestureThreshold as a distance in pixels...
Kotlin的例子:
// The gesture threshold expressed in dp
private const val GESTURE_THRESHOLD_DP = 16.0f
...
private var mGestureThreshold: Int = 0
...
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Get the screen's density scale
val scale: Float = resources.displayMetrics.density
// Convert the dps to pixels, based on density scale
mGestureThreshold = (GESTURE_THRESHOLD_DP * scale + 0.5f).toInt()
// Use mGestureThreshold as a distance in pixels...
}
我从Java例子中创建的Java方法工作得很好:
/**
* Convert dp units to pixel units
* https://developer.android.com/training/multiscreen/screendensities#dips-pels
*
* @param dip input size in density independent pixels
* https://developer.android.com/training/multiscreen/screendensities#TaskUseDP
*
* @return pixels
*/
private int dpToPixels(final float dip) {
// Get the screen's density scale
final float scale = this.getResources().getDisplayMetrics().density;
// Convert the dps to pixels, based on density scale
final int pix = Math.round(dip * scale + 0.5f);
if (BuildConfig.DEBUG) {
Log.i(DrawingView.TAG, MessageFormat.format(
"Converted: {0} dip to {1} pixels",
dip, pix));
}
return pix;
}
这个问题的其他几个答案提供了与这个答案类似的代码,但其他答案中没有足够的支持文档,让我知道其他答案中的一个是否正确。公认的答案也不同于这个答案。我认为开发人员文档已经清楚地说明了Java解决方案。
在Compose中也有一个解决方案。Compose中的密度为设备无关像素(dp)和像素之间的转换提供了一行解决方案。
val sizeInPx = with(LocalDensity.current) { 16.dp.toPx() }
使用kotlin-extension会更好
fun Int.toPx(context: Context): Int = (this * context.resources.displayMetrics.density).toInt()
fun Int.toDp(context: Context): Int = (this / context.resources.displayMetrics.density).toInt()
更新:
因为displayMetrics是全局共享资源的一部分,所以我们可以使用Resources. getsystem ()
val Float.toPx get() = this * Resources.getSystem().displayMetrics.density
val Float.toDp get() = this / Resources.getSystem().displayMetrics.density
val Int.toPx get() = (this * Resources.getSystem().displayMetrics.density).toInt()
val Int.toDp get() = (this / Resources.getSystem().displayMetrics.density).toInt()
PS:根据@EpicPandaForce的评论:
你不应该为此使用Resources.getSystem(),因为它不处理可折叠文件和Chrome OS设备。