Android 不是从活动子类获取屏幕大小
我有一个视图子类,它从活动子类开始,如下所示:Android 不是从活动子类获取屏幕大小,android,android-activity,android-view,screen-size,Android,Android Activity,Android View,Screen Size,我有一个视图子类,它从活动子类开始,如下所示: this.setContentView(instanceOfMyView); 在我的视图子类中,我想对屏幕大小进行一些处理,但这里的所有人都说应该这样开始: DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); screenWidth = dm.widthPixels; screenHeight = dm.
this.setContentView(instanceOfMyView);
在我的视图子类中,我想对屏幕大小进行一些处理,但这里的所有人都说应该这样开始:
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
screenWidth = dm.widthPixels;
screenHeight = dm.heightPixels;
但是getWindowManager()
是只能从activity子类调用的方法(对吗?)
所以,这是个坏主意吗?我需要在活动中获取屏幕大小,并将其用作视图构造函数中的参数,还是有办法在视图子类中获取屏幕大小?也许,只需要在视图类中获得一个指向活动实例的链接
提前谢谢 是的,有一种方法,如果可以将上下文对象传递给非活动类
int width= context.getResources().getDisplayMetrics().widthPixels;
int height= context.getResources().getDisplayMetrics().heightPixels;
您不需要活动对象本身。此处的其他答案不会给出屏幕分辨率
DisplayMetrics metrics = getBaseContext().getResources().getDisplayMetrics();
final int w = metrics.widthPixels;
final int h = metrics.heightPixels;
以下是您应该如何做到这一点:
@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
}
文件:
这可用于任何类别:
DisplayMetrics metrics = Resources.getSystem().getDisplayMetrics();
Integer with = metrics.widthPixels;
Integer height = metrics.heightPixels;
希望这个帮助比公认的答案更简单。你不需要背景
object ScreenUtils {
fun getScreenWidth() = Resources.getSystem().displayMetrics.widthPixels
fun getScreenHeight() = Resources.getSystem().displayMetrics.heightPixels
}
很好,我在视图子类的构造函数中已经有了
context
对象。谢谢。这不会返回屏幕分辨率。只有减去各种系统条形图后可用的内容才是getBaseContext()
在视图子类中不起作用,而只是getContext()
起作用。这不会返回屏幕分辨率。只有减去API 30的各种系统条后可用的内容,WindowManager\getDefaultDisplay()
才不推荐使用。让显示器与WindowManager关联的新方法是什么?上下文必须是活动上下文,还是可以是应用程序上下文?此外,getRealSize()
对设备方向敏感。我们如何才能在纵向模式下或在设备的自然方向上获得真实的宽度/高度,比如说?@Pallguer是一个相当古老的解决方案。我现在已经更新了。请让我知道它现在是否工作正常。我在我的项目中使用java,但我详细检查了更新的kotlin代码,它似乎是正确的,谢谢。为了回答我自己上面的问题,使用应用程序上下文可以工作,但它不是故意的,如果您这样做,StrictMode会给您一个红色的警告(java中的非可视上下文中的getDisplay())。自然分辨率位不能是(?:if(naturalResolution){val rotation=getDisplay(context)!!。rotation if(rotation==Surface.rotation_90 | | rotation==Surface.rotation_270)screenResolution.set(screenResolution.y,screenResolution.x)}
但是在实用工具带中也有getScreenNaturalOrientation功能,这太棒了。
object ScreenUtils {
fun getScreenWidth() = Resources.getSystem().displayMetrics.widthPixels
fun getScreenHeight() = Resources.getSystem().displayMetrics.heightPixels
}