Make full use of Android XML resources-array Resources

Source: Internet
Author: User

1. Use of integer arrays in programs,

<! -- The information array of each small image is X, Y, W, H --> <integer-array name = "box"> <item> 250 </item> <item> 2 </item> <item> 65 </item> <item> 47 </item> </integer-array>

Background of array resources:

Recently, I am playing a game. Many small pictures of the game are on a large image. to use them, you need to know the pictures X, Y, W, and h on each small image.

So I am confused: If I have a picture, I can use the R. drawable. Image ID. This time, many pictures are on a large image. How should I use it? I thought about three solutions.

1. bitmap. createbitmap (BMP, ox, oy, mW, mh) is used in every part of the program; that is, the size of the position of the dead cell graph (obviously not available)

2. Define an Enum to define a name for each small image, and then append the X, Y, W, and h Information (it is difficult for us to change the image)

3. use XML to record all the small images on this big image, including the name, X, Y, W, h, and then Parse XML to save all the small images X, Y, W, H (yes)

I have adopted the third set of solutions and successfully implemented them, but I always feel more relaxed, because all my images generate bitmap for unified management,

/*** The ID of each initialized bitmap is unique, including the small image on the big image * For example, bodies1 defines the ID of all the small images on bodies1.png, * Ensure that all IDs do not overlap */Private Static list <integer> resids; // record all resource IDS/*** record all initialized bitmap sets. * Including small images in a large image */Private Static hashmap <integer, bitmap> bmp smap ;//

When I load the image for the first time, each image must use a unique ID. I am so confused that I am used to the ID automatically generated by Android.

Later, I used Enum to manually set the ID.

/*** Enumeration of bodies1 piece ID * @ author jianbinzhu **/public static Enum bodies1 {/*** Zheng Wu series * // wooden box ("box ", 0xf0000001), box_small ("box_small", 0xf0000002), // wood ("Wood", 0xf0000003), wood_small ("wood_small", 0xf0000004 ), // triangle ("Triangle", 0xf0000005), trim ("triangle_small", 0xf0000006), // metal ("Metal", 0xf0000007), metal_small ("metal_small ", 0xf0000008), // wheel ("Wheel", 0xf0000009), wheel_small ("wheel_small", 0xf000000a), private final string name; // image name private final int ID; // idprivate bodies1 (string name, int ID) {This. name = Name; this. id = ID;} public int GETID () {return ID;} Public String getname () {return name ;}}

When using bodies1,

A few days ago, I suddenly thought that android XML could define array resources,

So I did it.

<! -- The information array of each small image is X, Y, W, H --> <integer-array name = "box"> <item> 250 </item> <item> 2 </item> <item> 65 </item> <item> 47 </item> </integer-array> <integer-array name = "box_small"> <item> 250 </item> <item> 160 </item> <item> 29 </item> <item> 21 </item> </integer-array>

Then read the data in this way, which perfectly solved my problem.

/*** Obtain the thumbnail information array ID on img_bodies1.png * @ Param ID: X, Y, W, H * @ return */public static bitmap getbodies1bitmap (int id) {bitmap BMP = getbitmap (ID); If (BMP = NULL) {int [] A = res. getintarray (ID); BMP = bitmap. createbitmap (getbitmap (R. drawable. img_bodies1), a [0], a [1], a [2], a [3]); Put (ID, BMP);} return BMP ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.