现在的位置: 首页 > 综合 > 正文

Android中XML数组资源

2013年07月18日 ⁄ 综合 ⁄ 共 7595字 ⁄ 字号 评论关闭

1570人阅读评论(0)收藏举报

1.Integer数组在程序中的使用,

  1. <!-- 每一张小图片的信息数组依次为:x,y,w,h --> 
  2.     <integer-arrayname="box"> 
  3.         <item>250</item> 
  4.         <item>2</item> 
  5.         <item>65</item> 
  6.         <item>47</item> 
  7.     </integer-array> 

使用数组资源的背景:

最近在做一款游戏,游戏的许多小图片都在一张大图上,要使用它们需要知道每张小图在大图上的:x,y,w,h

于是我就纠结了:要是一张一张的图片我可以使用 R.drawable.图片ID ,这回是好多图片在一个大图上,我该怎么使用呢?想了三种方案

1.在程序每个地方都采用Bitmap.createBitmap(bmp, ox, oy, mw, mh); 即写死小图位置大小(显然不可取)

2.定义一个enum为每张小图定义一个name,然后附加x,y,w,h信息(美工改图麻烦)

3.采用xml记录这张大图上的所有小图包括名称、x,y,w,h,然后有序初始化的时候解析xml保存所有小图x,y,w,h(算是还可以)

我采取了第三套方案,也成功的实现了,但是总感觉变捏,因为我的所有图片都是生成bitmap统一管理的,

  1. /**
  2.      * 每一个已初始化的位图的ID都是唯一的,包括大图上的小图
  3.      * 比如 Bodies1 定义了bodies1.png 上所有的小图的ID,
  4.      * 必须保证所有的ID不会重合
  5.      */ 
  6.     private static List<Integer> resIds; //记录所有资源ID 
  7.      
  8.     /**
  9.      * 记录所有已初始化的位图的集合.
  10.      * 包括 大图上的小图
  11.      */ 
  12.     private static HashMap<Integer, Bitmap> bmpsMap;// 

第一次加载,每张图片都必须使用一个唯一的ID,这个我就纠结了,习惯了使用android自动生成的ID

后来我使用了枚举enum手动设定ID

  1. /**
  2.      * Bodies1 上图片ID对应枚举
  3.      * @author JianbinZhu
  4.      *
  5.      */ 
  6.     public staticenum Bodies1{ 
  7.         /**
  8.          * 投郑物系列
  9.          */ 
  10.         //木箱 
  11.         box("box",
    0xf0000001), box_small("box_small",0xf0000002), 
  12.         //木桶 
  13.         wood("wood",
    0xf0000003), wood_small("wood_small",0xf0000004), 
  14.         //木三角 
  15.         triangle("triangle",0xf0000005), triangle_small("triangle_small",0xf0000006), 
  16.         //铁桶 
  17.         metal("metal",
    0xf0000007), metal_small("metal_small",0xf0000008), 
  18.         //轮子 
  19.         wheel("wheel",
    0xf0000009), wheel_small("wheel_small",0xf000000a), 
  20.          
  21.         private final String name; //图片名 
  22.         private finalint id;  
    //给的ID 
  23.          
  24.         private Bodies1(String name,int id) { 
  25.             this.name = name; 
  26.             this.id = id; 
  27.         } 
  28.          
  29.         public int getId() { 
  30.             return id; 
  31.         } 
  32.          
  33.         public String getName() { 
  34.             return name; 
  35.         } 
  36.     } 

使用的时候传Bodies1参数,

几天突然想到Android xml 可以定义数组资源,

于是我就这样做

  1. <!-- 每一张小图片的信息数组依次为:x,y,w,h --> 
  2.     <integer-arrayname="box"> 
  3.         <item>250</item> 
  4.         <item>2</item> 
  5.         <item>65</item> 
  6.         <item>47</item> 
  7.     </integer-array> 
  8.     <integer-arrayname="box_small"> 
  9.         <item>250</item> 
  10.         <item>160</item> 
  11.         <item>29</item> 
  12.         <item>21</item> 
  13.     </integer-array> 

然后这样读取,非常完美解决了我的问题

  1. /**
  2.      * 获取 img_bodies1.png 上的小图
  3.      * @param id 小图信息数组ID:x,y,w,h
  4.      * @return
  5.      */ 
  6.     public static Bitmap getBodies1Bitmap(int id) { 
  7.         Bitmap bmp = getBitmap(id); 
  8.          
  9.         if(bmp ==
    null){ 
  10.             int[] a = res.getIntArray(id); 
  11.             bmp = Bitmap.createBitmap(getBitmap(R.drawable.img_bodies1), a[0], a[1], a[2], a[3]); 
  12.             put(id, bmp); 
  13.         } 
  14.          
  15.         return bmp; 
  16.     } 

1.Integer数组在程序中的使用,

  1. <!-- 每一张小图片的信息数组依次为:x,y,w,h --> 
  2.     <integer-arrayname="box"> 
  3.         <item>250</item> 
  4.         <item>2</item> 
  5.         <item>65</item> 
  6.         <item>47</item> 
  7.     </integer-array> 

使用数组资源的背景:

最近在做一款游戏,游戏的许多小图片都在一张大图上,要使用它们需要知道每张小图在大图上的:x,y,w,h

于是我就纠结了:要是一张一张的图片我可以使用 R.drawable.图片ID ,这回是好多图片在一个大图上,我该怎么使用呢?想了三种方案

1.在程序每个地方都采用Bitmap.createBitmap(bmp, ox, oy, mw, mh); 即写死小图位置大小(显然不可取)

2.定义一个enum为每张小图定义一个name,然后附加x,y,w,h信息(美工改图麻烦)

3.采用xml记录这张大图上的所有小图包括名称、x,y,w,h,然后有序初始化的时候解析xml保存所有小图x,y,w,h(算是还可以)

我采取了第三套方案,也成功的实现了,但是总感觉变捏,因为我的所有图片都是生成bitmap统一管理的,

  1. /**
  2.      * 每一个已初始化的位图的ID都是唯一的,包括大图上的小图
  3.      * 比如 Bodies1 定义了bodies1.png 上所有的小图的ID,
  4.      * 必须保证所有的ID不会重合
  5.      */ 
  6.     private static List<Integer> resIds; //记录所有资源ID 
  7.      
  8.     /**
  9.      * 记录所有已初始化的位图的集合.
  10.      * 包括 大图上的小图
  11.      */ 
  12.     private static HashMap<Integer, Bitmap> bmpsMap;// 

第一次加载,每张图片都必须使用一个唯一的ID,这个我就纠结了,习惯了使用android自动生成的ID

后来我使用了枚举enum手动设定ID

  1. /**
  2.      * Bodies1 上图片ID对应枚举
  3.      * @author JianbinZhu
  4.      *
  5.      */ 
  6.     public staticenum Bodies1{ 
  7.         /**
  8.          * 投郑物系列
  9.          */ 
  10.         //木箱 
  11.         box("box",
    0xf0000001), box_small("box_small",0xf0000002), 
  12.         //木桶 
  13.         wood("wood",
    0xf0000003), wood_small("wood_small",0xf0000004), 
  14.         //木三角 
  15.         triangle("triangle",0xf0000005), triangle_small("triangle_small",0xf0000006), 
  16.         //铁桶 
  17.         metal("metal",
    0xf0000007), metal_small("metal_small",0xf0000008), 
  18.         //轮子 
  19.         wheel("wheel",
    0xf0000009), wheel_small("wheel_small",0xf000000a), 
  20.          
  21.         private final String name; //图片名 
  22.         private finalint id;  
    //给的ID 
  23.          
  24.         private Bodies1(String name,int id) { 
  25.             this.name = name; 
  26.             this.id = id; 
  27.         } 
  28.          
  29.         public int getId() { 
  30.             return id; 
  31.         } 
  32.          
  33.         public String getName() { 
  34.             return name; 
  35.         } 
  36.     } 

使用的时候传Bodies1参数,

几天突然想到Android xml 可以定义数组资源,

于是我就这样做

  1. <!-- 每一张小图片的信息数组依次为:x,y,w,h --> 
  2.     <integer-arrayname="box"> 
  3.         <item>250</item> 
  4.         <item>2</item> 
  5.         <item>65</item> 
  6.         <item>47</item> 
  7.     </integer-array> 
  8.     <integer-arrayname="box_small"> 
  9.         <item>250</item> 
  10.         <item>160</item> 
  11.         <item>29</item> 
  12.         <item>21</item> 
  13.     </integer-array> 

然后这样读取,非常完美解决了我的问题

  1. /**
  2.      * 获取 img_bodies1.png 上的小图
  3.      * @param id 小图信息数组ID:x,y,w,h
  4.      * @return
  5.      */ 
  6.     public static Bitmap getBodies1Bitmap(int id) { 
  7.         Bitmap bmp = getBitmap(id); 
  8.          
  9.         if(bmp ==
    null){ 
  10.             int[] a = res.getIntArray(id); 
  11.             bmp = Bitmap.createBitmap(getBitmap(R.drawable.img_bodies1), a[0], a[1], a[2], a[3]); 
  12.             put(id, bmp); 
  13.         } 
  14.          
  15.         return bmp; 
  16.     } 

抱歉!评论已关闭.