简体   繁体   English

我不知道如何获得用于 class 的屏幕密度

[英]I don't know how to get the screen density for use in a class

I'm teaching myself how to program in Kotlin and I've run into a problem when I've tried to make a function in a class to handle some math for me.我正在自学如何在 Kotlin 中编程,当我尝试在 class 中为我处理一些数学问题时,我遇到了一个问题。

The problem I've run into is that I don't know how to get the screen density as a var/val in the class that is out side of the MainActivity.kt我遇到的问题是我不知道如何在 MainActivity.kt 之外的 class 中将屏幕密度作为 var/val

I've searched a lot for the answer and I can't seem to come up with the right term to get the resources that I need and it's driving me crazy.我已经搜索了很多答案,但我似乎无法想出正确的术语来获得我需要的资源,这让我发疯了。 Every answer I've found either doesn't work or Android Studio says it's going to cause a memory leak.我发现的每个答案要么不起作用,要么 Android Studio 说它会导致 memory 泄漏。

I know how to get the density in the MainActivity.kt by using this code.我知道如何使用此代码获取 MainActivity.kt 中的密度。

val scale: Float = resources.displayMetrics.density

Thanks for any help.谢谢你的帮助。 I'm really enjoying learning Kotlin and how to make apps.我真的很喜欢学习 Kotlin 以及如何制作应用程序。

You need to access resources through a Context , as in context.resources.displayMetrics.density .您需要通过Context访问resources ,如context.resources.displayMetrics.density An Activity is a Context , which is why you can just access resources directly (technically it's this.resources , where this is the Activity) ActivityContext ,这就是为什么您可以直接访问resources的原因(技术上它是this.resourcesthis是 Activity)

If you're calling that directly within an Activity, it shouldn't be a problem, If it's in another class, you'll have to get a Context from somewhere - ideally passing one in (eg if an Activity is calling a function, it can pass itself as a Context parameter).如果您直接在 Activity 中调用它,那应该不是问题,如果它在另一个类中,则必须从某个地方获取Context - 理想情况下传递一个(例如,如果 Activity 正在调用 function,它可以将自身作为Context参数传递)。

There are a few things that are Context s, but in this case specifically, you need to be careful :有一些东西是Context ,但在这种情况下, 你需要特别小心

After Build.VERSION_CODES#R , Resources must be obtained by Activity or Context created with Context.createWindowContext(int, Bundle) .Build.VERSION_CODES#R之后,必须通过使用Context.createWindowContext(int, Bundle)创建的ActivityContext来获取资源。 Application#getResources() may report wrong values in multi-window or on secondary displays. Application#getResources()可能会在多窗口或辅助显示器上报告错误的值。

Basically the Context needs to have an idea of how big the app window is, which an Activity will, but something like applicationContext might not.基本上 Context 需要知道应用程序 window 有多大, Activity会,但applicationContext之类的可能不会。


The main reason to be careful is that Activities are big, so you don't want to hold onto them when they're being passed as a Context, because you might keep them in memory after the Activity has been discarded - that would be a memory leak, and it might be what you're getting warned about!要小心的主要原因是活动很大,所以当它们作为上下文传递时你不想保留它们,因为你可能会在活动被丢弃后将它们保留在 memory - 那将是memory 泄漏,这可能是您收到的警告!

Typically if you did need to hold onto the Context for future use, you'd do something like myContext = passedContext.applicationContext - so you're not holding on to whatever object was passed in, you're plucking out the shared application context which is always in memory anyway, so it doesn't matter if you keep it.通常,如果您确实需要保留 Context 以供将来使用,您会执行类似myContext = passedContext.applicationContext类的操作 - 因此您不会保留传入的任何 object ,而是提取共享的应用程序上下文反正一直在memory,所以留着也没关系。 That's a safe way to store one.这是一种安全的存储方式。

But like the docs mentioned above, that's no good here, so ideally your function would:但就像上面提到的文档一样,这在这里不好,所以理想情况下你的 function 会:

  • use the Activity context to calculate whatever within the function, and use/store that value, discarding the context, or使用活动上下文计算 function 中的任何内容,并使用/存储该值,丢弃上下文,
  • access something within the context ( resources , displayMetrics etc) and store that if you need to access it later访问上下文中的某些内容( resourcesdisplayMetrics等)并在以后需要访问时将其存储

that way you're storing what you need to, and not holding onto the Activity when you don't need it这样你就可以存储你需要的东西,而不是在你不需要的时候坚持住 Activity


Contexts are weird when you're starting out, but they're basically something that provides access to information about the environment the code is running in, and system functions.刚开始时,上下文很奇怪,但它们基本上是提供对有关代码运行环境和系统功能的信息的访问。 And some are more specific - like an Activity is a visible component in your app, so it can provide info like the dimensions of its window, things like that.还有一些更具体——比如Activity是应用程序中的可见组件,因此它可以提供 window 的尺寸等信息。

So they're really important for Android apps, and you end up passing them around a lot (you'll see them as parameters in a lot of functions), but you also have to be careful of keeping Activity s in memory by storing references to them in components that outlive the Activity itself.因此,它们对于 Android 应用程序非常重要,并且您最终会大量传递它们(您会在很多函数中将它们视为参数),但您还必须小心通过存储引用将Activity保留在 memory 中在比 Activity 本身寿命更长的组件中给他们。 Hope that makes some sense!希望这有点道理!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 没有活动对象来获取windowManager时如何获得屏幕密度? - How I can get screen density when I don't have activity-object to get windowManager? 如何获得屏幕的密度? - How to get the density of a screen? 我需要知道屏幕尺寸和屏幕密度之间有什么区别 - i need to know what different between screen size and screen density 我没有在Android中拨号屏幕 - I don't get dialing screen in android 我不知道如何解决此NullPointerException - I don't know how to fix this NullPointerException 我不知道为什么我不能使用bitmapTransform - I don't know why I can't use bitmapTransform 我如何知道以下设备的屏幕密度(如 ldpi、mdpi 等) - How can I know screen density (like ldpi, mdpi, etc.) of following devices 如何知道 Android 中所有屏幕密度的像素范围? (图像的基本浓度) - How can I know the pixels range for all screen densities in Android ? (base density of an image) 我有一个重复的方法,我不知道如何将抽象与ormlite一起使用 - I have a repeated method and I don't know how can I use abstract with ormlite 列表<studentmodel>学生详细信息 = []; 这里的 StudentModel 是 flutter 中的 class 我不知道这行代码是如何工作的</studentmodel> - List<StudentModel> studentDetails = []; here StudentModel is class in flutter and i don't know how this line of code work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM