[英]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) Activity
是Context
,这就是为什么您可以直接访问resources
的原因(技术上它是this.resources
, this
是 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 byActivity
orContext
created withContext.createWindowContext(int, Bundle)
.在
Build.VERSION_CODES#R
之后,必须通过使用Context.createWindowContext(int, Bundle)
创建的Activity
或Context
来获取资源。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 会:
resources
, displayMetrics
etc) and store that if you need to access it laterresources
、 displayMetrics
等)并在以后需要访问时将其存储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.