In the Andengine, we load the picture resources are usually used in the assets directory loading method, the other way to use very little, perhaps because the example is only to provide the reason for this way, in fact, Andengine also provide us with the other two ways, One is commonly used in the app from the Res directory to load the picture resources (through the R.java file load), the other is loaded from the SD card or sandbox. The latter two may be smaller, but some need to be used in other ways, such as loading users ' photo resources and so on ...
1. Load resources from the assets directory, which is the most basic and most commonly used way, in many cases this is used, do not explain, the code
The traditional way to load resources under the asset directory
Assetsbitmaptextureatlas = new Bitmaptextureatlas (Gettexturemanager (), n/a);
Assetstextureregion = (textureregion) bitmaptextureatlastextureregionfactory.createfromasset ( Assetsbitmaptextureatlas, this
, "Assets_image.png", 0, 0);
Assetsbitmaptextureatlas.load ();
2. Load the resources from the Res directory, in fact, the principle and asset is similar, just to read through the R.java file way
The traditional way to load the Res directory resources, but the resources under the RES directory will generate an index
Resbitmaptextureatlas = new Bitmaptextureatlas (Gettexturemanager (), 128, );
Restextureregion = (textureregion) Bitmaptextureatlastextureregionfactory.createfromresource ( Resbitmaptextureatlas, this
, r.drawable.res_image, 0, 0);
Resbitmaptextureatlas.load ();
This is rarely used, and is usually placed in the assets directory.
3. Load resources from SD card, this is a key point, because some of the requirements will be used, but may not have noticed
(PS: Loading from the sandbox is the same principle, but the directory is different)
Loading resources from SD card, loading from sandbox is also the principle of
file Resfile = new file ("/mnt/sdcard/storage_image.png");
Sdbitmaptexturealatlas = new Bitmaptextureatlas (Gettexturemanager (), n/a);
Filebitmaptextureatlassource Filebitmaptextureatlassource = filebitmaptextureatlassource.create (resFile);
Sdtextureregion = (textureregion) bitmaptextureatlastextureregionfactory.createfromsource (SdBitmapTextureAlAtlas, Filebitmaptextureatlassource, 0, 0);
Sdbitmaptexturealatlas.load ();
In fact, the principle is very simple, andengine has provided us with a class called Filebitmaptextureatlassource, And the inheritance relationship is filebitmaptextureatlassource inherit from Basetextureatlassource and implement Ibitmaptextureatlassource interface, the heart of the classmate may find, In the various create** () methods of bitmaptextureatlastextureregionfactory, the incoming source parameter is implemented on the Ibitmaptextureatlassource interface.
Brief introduction, want to deepen the classmate continue to see Andengine source bar.
Attached to my weak and weak demo source, click to download