我正在制作游戏,我想设置游戏的布局,使其适用于多个设备屏幕。因此,我没有从asset's folder
获取图片,而是在寻找某种方法从drawable folder
中获取图片。所以,稍后我可以根据设备的屏幕获取图像。
更新
我用
尝试了mFaceTextureRegionLifeLine = (TiledTextureRegion) TextureRegionFactory.createFromResource(mTextureLifeLine, this, R.drawable.icon, 100, 100);
而且,它解雇了我并且错误如下 -
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): FATAL EXCEPTION: main
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.engine/com.engine.BallDemo}: java.lang.ClassCastException: org.anddev.andengine.opengl.texture.region.TextureRegion
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.os.Looper.loop(Looper.java:123)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at java.lang.reflect.Method.invoke(Method.java:521)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at dalvik.system.NativeStart.main(Native Method)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): Caused by: java.lang.ClassCastException: org.anddev.andengine.opengl.texture.region.TextureRegion
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at com.engine.BallDemo.onLoadResources(BallDemo.java:132)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at org.anddev.andengine.ui.activity.BaseGameActivity.onCreate(BaseGameActivity.java:57)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-05 19:02:38.923: ERROR/AndroidRuntime(4161): ... 11 more
所以,如果有可能请告诉我。 谢谢,Suri Sahani。
答案 0 :(得分:4)
以下是为您执行此操作的方法:
public static TextureRegion createFromResource(final Texture pTexture, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY)
如果您需要创建平铺纹理区域,请使用:
public static TiledTextureRegion createTiledFromResource(final Texture pTexture, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY, final int pTileColumns, final int pTileRows)
希望这有帮助。
答案 1 :(得分:3)
如果您只是指定了抽奖ID(例如R.drawable.icon
),那么根据您的设备,它会从/res/
中最适合该设备的文件夹中提取它:
即,
/res/drawable/icon.png
/res/drawable-large/icon.png
意味着大型设备使用第二个png。
有关supporting multiple screens的更多信息,请访问Android网站。
答案 2 :(得分:1)
如果你想从drawable文件夹中获取图片,你应该试试这个 - >
Context ctx = getApplicationContext();
Resources r = ctx.getResources();
Drawable d = r.getDrawable(R.drawable.image_name);