List Files And Directories
Obtaining operationsCompositionPartsFilehandle has four methods.
1. classpath
The file is usually read-only relative to classpath.
2. Internal
The internal file path is relativeProgramThe root directory or the android assets folder.
3. External
The external file path is relative to the root directory of the SD card.
4. Absolute
The assets folder itself is the folder for storing resources. Compared with the resource folder, its resources do not generate the ID in R, which is suitable for storing images.
Therefore, GDX is used. files. internal ("image1.jpg") to obtain the image and then call batch. draw (texture, 20, 10); draw a graph, where 20 and 10 are coordinates and Cartesian coordinates are taken as the origin in the lower left corner.
Complete code:
Package COM. cnblogs. htynkn; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. g2d. spritebatch; public class firstgame implements applicationlistener {// spritebatch private spritebatch batch for plotting; // texture private texture; @ override public void create () {batch = new spritebatch (); // instantiate texture = new texture (GDX. files. internal ("image1.jpg");} @ override public void dispose () {// todo auto-generated method stub} @ override public void pause () {// todo auto-generated method stub} @ override public void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); // clear screen batch. begin (); batch. draw (texture, 20, 10); batch. end () ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}
Effect:
We can see that the image cannot be completely displayed, and we often use a part of the image in actual operations, or set multiple image resources in one image file.
To display part of the image, you can use the textureregion class.
The most common method is draw (textureregion region, float X, float y, float width, float height)
Specify the start point and length.
Modify the Code:
Package COM. cnblogs. htynkn; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. g2d. spritebatch; import COM. badlogic. GDX. graphics. g2d. textureregion; public class firstgame implements applicationlistener {// spritebatch private spritebatch batch for plotting; // texture private texture; // region private textureregion region; @ override public void create () {batch = new spritebatch (); // instantiate texture = new texture (GDX. files. internal ("image1.jpg"); region = new textureregion (texture, 200,200,);} @ override public void dispose () {// todo auto-generated method stub} @ override public void pause () {// todo auto-generated method stub} @ override public void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); // clear screen batch. begin (); batch. draw (Region, 0, 0); batch. end () ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}
Effect:
You may think that textureregion is not powerful enough. It doesn't matter. You can also use it.Sprite.
Sprite not only includes the textureregion function, but also can specify the position and color.
Key code:
Sprite = new sprite (texture, 80, 80,400,300); Sprite. setposition (10, 10); // position Sprite. setrotation (15 );
If you think about the previous examples, you can find that the sprite function is the set above. But Sprite is more convenient. It describes everything with an object.
The complete code is as follows:
Package COM. cnblogs. htynkn; import COM. badlogic. GDX. applicationlistener; import COM. badlogic. GDX. GDX; import COM. badlogic. GDX. graphics. gl10; import COM. badlogic. GDX. graphics. texture; import COM. badlogic. GDX. graphics. g2d. sprite; import COM. badlogic. GDX. graphics. g2d. spritebatch; public class firstgame implements applicationlistener {// spritebatch private spritebatch batch for plotting; // texture private texture; // sprite private sprite; @ override public void create () {batch = new spritebatch (); // instantiate texture = new texture (GDX. files. internal ("image1.jpg"); sprite = new sprite (texture, 80, 80,400,300); Sprite. setposition (10, 10); // position Sprite. setrotation (15); // rotate} @ override public void dispose () {// todo auto-generated method stub} @ override public void pause () {// todo auto-generated method stub} @ override public void render () {GDX. GL. glclear (gl10.gl _ color_buffer_bit); // clear screen batch. begin (); Sprite. draw (batch); batch. end () ;}@ override public void resize (INT width, int height) {// todo auto-generated method stub} @ override public void resume () {// todo auto-generated method stub }}
Effect:
You can also use the setcolor method of Sprite to color the image.
Setcolor (float R, float g, float B, float)
The color expression is a number between 0 and 1.
There is basically so much content to draw. The next article is about 2D scenarios.
Conclusion:
1. The hybrid mode is enabled by default. This means that when the drawing is finished, the translucent part has been mixed. When the blending is disabled, anything on the scene will be replaced by a texture, which is suitable for drawing a large background.
Batch. disableblending (); backgroundsprite. Draw (batch); batch. enableblending ();
2. Performance Optimization
Spritebatch has a constructor that can specify the maximum number of buffers. If the value is too low, additional GPU calls may occur. If the value is too high, excessive memory usage will occur.
In spritebatch, a field is maxspritesinbatch. You can set a high buffer number first, and then observe the value of maxspritesinbatch to determine the appropriate buffer value.
There is also a field rendercils. When the end is called, its value indicates the number of times the in and end are sent to the GPU.
Another constructor can specify the buffer size and quantity. Reasonable settings can greatly improve performance.