SurfaceView 中的 'resources' 在哪里定义,我如何从不同的 class 访问它?
Where is 'resources' in SurfaceView defined and how can I access it from different class?
我正在尝试使用 sprite 编写应用程序。现在我正在将图像传递给每个精灵对象。但是由于每个精灵的图像都是相同的,所以我宁愿将图像存储为 class 属性。
不幸的是,变量 'resources' 只能在 SurfaceView class 中访问,而不能在 sprite class 中访问。
这是我的代码的相关部分:
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log.d
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList
class GameView(context: Context, attributes: AttributeSet): SurfaceView(context, attributes), SurfaceHolder.Callback {
override fun surfaceCreated(p0: SurfaceHolder?) {
Note(BitmapFactory.decodeResource(resources, R.drawable.note), 200)
}
}
备注代码:
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.Log.d
class Note (var image: Bitmap, var x: Int) {
var y: Int = 0
var width: Int = 0
var height: Int = 0
private var vx = -10
private val screenWidth = Resources.getSystem().displayMetrics.widthPixels
private val screenHeight = Resources.getSystem().displayMetrics.heightPixels - 100
// I would like to load the image like this:
private val image2: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.note)
init {
width = image.width
height = image.height
//x = screenWidth/2
y = ((screenHeight-height)/2).toInt()
}
fun draw(canvas: Canvas) {
canvas.drawBitmap(image, x.toFloat(), y.toFloat(), null)
}
fun update(){
x += vx
if (x < 0) {
x = screenWidth
}
}
}
我尝试使用 GameView.resources 和 SurfaceView.resources,但都不起作用。
resources 变量从哪里来,如何访问它?
它是任何 Context
子类的 Android Resources object. You can access it via Context.resources,例如您的 Activity
。请注意,您需要 Context
或 Activity
的特定实例 - 只写 Activity.resources
是行不通的。
在Android中,Resources是一个class,用于访问应用程序的资源。您可以从
获取此 class 的实例
- 上下文 class 或其子 class 实例,如应用程序、Activity、服务等
- View class 或其子 class 实例,如 TextView、Button、SurfaceView 等
在您的情况下,您可以在创建 Node 实例时传递 GameView class 的上下文实例(View class 的子 class)。
class GameView(context: Context, attributes: AttributeSet): SurfaceView(context, attributes), SurfaceHolder.Callback {
override fun surfaceCreated(p0: SurfaceHolder?) {
// Pass context that associated with this view to Node constructor.
Note(context, BitmapFactory.decodeResource(resources, R.drawable.note), 200)
}
}
然后在节点中使用它class
// Modify Node constructor to add a Context parameter.
class Note (val context: Context, var image: Bitmap, var x: Int) {
...
// Get resources instance from a Context instance.
private val image2: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.note)
...
}
我正在尝试使用 sprite 编写应用程序。现在我正在将图像传递给每个精灵对象。但是由于每个精灵的图像都是相同的,所以我宁愿将图像存储为 class 属性。 不幸的是,变量 'resources' 只能在 SurfaceView class 中访问,而不能在 sprite class 中访问。 这是我的代码的相关部分:
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.graphics.Paint
import android.util.AttributeSet
import android.util.Log.d
import android.view.SurfaceHolder
import android.view.SurfaceView
import android.view.View
import java.lang.Exception
import java.util.*
import kotlin.collections.ArrayList
class GameView(context: Context, attributes: AttributeSet): SurfaceView(context, attributes), SurfaceHolder.Callback {
override fun surfaceCreated(p0: SurfaceHolder?) {
Note(BitmapFactory.decodeResource(resources, R.drawable.note), 200)
}
}
备注代码:
import android.content.res.Resources
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.graphics.Canvas
import android.util.Log.d
class Note (var image: Bitmap, var x: Int) {
var y: Int = 0
var width: Int = 0
var height: Int = 0
private var vx = -10
private val screenWidth = Resources.getSystem().displayMetrics.widthPixels
private val screenHeight = Resources.getSystem().displayMetrics.heightPixels - 100
// I would like to load the image like this:
private val image2: Bitmap = BitmapFactory.decodeResource(resources, R.drawable.note)
init {
width = image.width
height = image.height
//x = screenWidth/2
y = ((screenHeight-height)/2).toInt()
}
fun draw(canvas: Canvas) {
canvas.drawBitmap(image, x.toFloat(), y.toFloat(), null)
}
fun update(){
x += vx
if (x < 0) {
x = screenWidth
}
}
}
我尝试使用 GameView.resources 和 SurfaceView.resources,但都不起作用。 resources 变量从哪里来,如何访问它?
它是任何 Context
子类的 Android Resources object. You can access it via Context.resources,例如您的 Activity
。请注意,您需要 Context
或 Activity
的特定实例 - 只写 Activity.resources
是行不通的。
在Android中,Resources是一个class,用于访问应用程序的资源。您可以从
获取此 class 的实例- 上下文 class 或其子 class 实例,如应用程序、Activity、服务等
- View class 或其子 class 实例,如 TextView、Button、SurfaceView 等
在您的情况下,您可以在创建 Node 实例时传递 GameView class 的上下文实例(View class 的子 class)。
class GameView(context: Context, attributes: AttributeSet): SurfaceView(context, attributes), SurfaceHolder.Callback {
override fun surfaceCreated(p0: SurfaceHolder?) {
// Pass context that associated with this view to Node constructor.
Note(context, BitmapFactory.decodeResource(resources, R.drawable.note), 200)
}
}
然后在节点中使用它class
// Modify Node constructor to add a Context parameter.
class Note (val context: Context, var image: Bitmap, var x: Int) {
...
// Get resources instance from a Context instance.
private val image2: Bitmap = BitmapFactory.decodeResource(context.resources, R.drawable.note)
...
}