5

I am working on a game and I want to set the layout of game such that it works on Multiple device screens. So, instead of fetching images from asset's folder, I making looking for some way to fetch it from the drawable folder. So, that later on I can get the images according to the screen of the device.

Update:

I tried it using

mFaceTextureRegionLifeLine = (TiledTextureRegion) TextureRegionFactory.createFromResource(mTextureLifeLine, this, R.drawable.icon, 100, 100);

And, it fired me with and error below-

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

So, if it is possible let me know. Thanks, Suri Sahani.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242

3 Answers3

4

Here's the method that will do this for you:

public static TextureRegion createFromResource(final Texture pTexture, final Context pContext, final int pDrawableResourceID, final int pTexturePositionX, final int pTexturePositionY)

If you need to create a tiled texture region, use this:

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)

Hope this helps.

Egor
  • 39,695
  • 10
  • 113
  • 130
  • Thanks for reply, I tried that before also and it fired me an error, please look at my update question. – Lalit Poptani Sep 05 '11 at 13:40
  • @suri sahani, Last two parameters are the number of tiles you have in columns and rows. For example if you have 6 tiles: 2 rows with 3 tiles in each, you should pass (3, 2) as parameters. – Egor Sep 05 '11 at 14:04
  • Thanks a lot! Its working fine. but do tell me what are tiles. – Lalit Poptani Sep 05 '11 at 14:13
  • @suri sahani, So what for do you need to make a tiled region? Actually, tiles are parts of an image, needed for animated sprites. Check the AnimatedSprites example in AndEngineExamples to see what effect does it give. – Egor Sep 05 '11 at 14:14
  • Well, I want to animate the images later on the Scene and that all I had done fetching images from assets folder. Thanks a lot. – Lalit Poptani Sep 05 '11 at 14:21
3

If you simply specify the drawble id (Such as R.drawable.icon) then depending on your device it will pull it from the folder in /res/ which is most appropriate to the device:

ie,

/res/drawable/icon.png 
/res/drawable-large/icon.png

Would mean large devices used the second png.

See the android site for more information on supporting multiple screens.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
Graeme
  • 25,714
  • 24
  • 124
  • 186
1

If you want fetch images from drawable folder, you should try this ->

Context ctx = getApplicationContext();
Resources r = ctx.getResources();
Drawable d = r.getDrawable(R.drawable.image_name);
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Rocker
  • 669
  • 5
  • 15