9

我有一个从活动子类开始的视图子类,如下所示:

this.setContentView(instanceOfMyView);

在我的视图子类中,我想对屏幕大小进行一些处理,但是这里的所有人都说它应该像这样开始:

DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm); 
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels; 

但是getWindowManager()只能从活动子类调用的方法(我对吗?)

那么,这是个坏主意,我需要在活动中获取屏幕大小并将其用作视图构造函数中的参数,还是有一种方法可以在视图子类中获取屏幕大小?也许,只需要以某种方式获取视图类中活动实例的链接?

提前致谢。

4

5 回答 5

33

是的,有一种方法,如果您可以将上下文对象传递给您的非活动类,

   int width= context.getResources().getDisplayMetrics().widthPixels;
   int height= context.getResources().getDisplayMetrics().heightPixels;

您不需要活动对象本身。

于 2012-07-26T12:52:20.317 回答
4
DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();         
    final int w = metrics.widthPixels;
    final int h = metrics.heightPixels;
于 2012-07-26T12:56:14.057 回答
1

这里的其他答案不会给你屏幕分辨率。

这是你应该如何做的:

@SuppressLint("ObsoleteSdkInt")
@JvmStatic
fun getScreenResolution(context: Context, naturalResolution: Boolean = false): Point {
    val screenResolution = Point()
    val display = getDisplay(context)!!
    @Suppress("DEPRECATION")
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        display.getRealSize(screenResolution)
    } else display.getSize(screenResolution)
    if (naturalResolution) {
        val screenNaturalOrientation = getScreenNaturalOrientation(context)
        if ((screenNaturalOrientation == Configuration.ORIENTATION_PORTRAIT && screenResolution.x > screenResolution.y)
                || (screenNaturalOrientation == Configuration.ORIENTATION_LANDSCAPE && screenResolution.y > screenResolution.x))
            screenResolution.set(screenResolution.y, screenResolution.x)
    }
    return screenResolution
}

/**
 * returns the natural orientation of the device: Configuration.ORIENTATION_LANDSCAPE or Configuration.ORIENTATION_PORTRAIT .<br></br>
 * The result should be consistent no matter the orientation of the device
 */
fun getScreenNaturalOrientation(context: Context): Int {
    //based on : http://stackoverflow.com/a/9888357/878126
    val config = context.resources.configuration
    val rotation = getDisplay(context)!!.rotation
    return if ((rotation == Surface.ROTATION_0 || rotation == Surface.ROTATION_180) &&
            config.orientation == Configuration.ORIENTATION_LANDSCAPE
            || (rotation == Surface.ROTATION_90 || rotation == Surface.ROTATION_270) &&
            config.orientation == Configuration.ORIENTATION_PORTRAIT) Configuration.ORIENTATION_LANDSCAPE else Configuration.ORIENTATION_PORTRAIT
}

要获得显示,请使用:

fun getDisplay(context: Context): Display? {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R)
        try {
            val result = context.display
            if (result != null)
                return result
        } catch (e: Throwable) {
        }
    if (context is Activity) {
        return try {
            @Suppress("DEPRECATION")
            context.windowManager.defaultDisplay
        } catch (e: Throwable) {
            null
        }
    }
    return null
}

文档:

https://developer.android.com/reference/android/view/Display.html#getRealSize(android.graphics.Point)

于 2018-07-12T14:27:28.127 回答
0

这可以在任何类中使用:

DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
Integer with = metrics.widthPixels;
Integer height = metrics.heightPixels;

希望这有帮助

于 2018-07-28T16:21:04.020 回答
0

甚至比公认的答案更简单。你不需要上下文。

object ScreenUtils {

    fun getScreenWidth() = Resources.getSystem().displayMetrics.widthPixels

    fun getScreenHeight() = Resources.getSystem().displayMetrics.heightPixels

}
于 2020-02-27T11:47:53.300 回答