非活动类中的 getResources
我一直在尝试在非活动类中加载位图,但到目前为止我所做的一切都失败了。我什至尝试发送上下文作为参考,但这也会导致错误。
我目前的情况是这样的:
// Surface class
public class GameScreen extends SurfaceView implements Callback {
TileSet ts;
public GameScreen(Context context, AttributeSet attr) {
// Here I send context as a reference
ts = new TileSet(context, R.drawable.tiles);
}
}
// This is the class I need to get resources
public class TileSet {
public TileSet(Context context, int id) {
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
}
}
有什么想法吗?
I've been trying to load a bitmap in a non-activity class but everything I've done so far has failed. I have even tried to send the Context as reference, but this also results in error.
My current situation looks like this:
// Surface class
public class GameScreen extends SurfaceView implements Callback {
TileSet ts;
public GameScreen(Context context, AttributeSet attr) {
// Here I send context as a reference
ts = new TileSet(context, R.drawable.tiles);
}
}
// This is the class I need to get resources
public class TileSet {
public TileSet(Context context, int id) {
Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), id);
}
}
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过在 Activity 中获取位图,然后将其传递给 GameScreen 的构造函数?
Have you tried just getting the bitmap in the Activity and then passing that to the constructor of your GameScreen?