android开发学习之路——连连看之加载图片(三)

    正如前面AbstractBoard类的代码中看到的,当程序需要创建N个Piece对象时,程序会直接调用ImageUtil的getPlayImages()方法去获取图片,该方法将会随机从res

drawable-mdpi目录下取得N张图片。

    为了让getPlayImages()方法从resdrawable-mdpi目录下随机取得N张图片,程序的实现思路可分为如下几步:

    1.通过反射来获取R.drawable的所有Field(Android的每张图片资源都会自动转换为R.drawable的静态Field),并将这些Field值添加到一个List集合中。

    2.从第一步得到的List集合中随机“抽取”N/2个图片ID。

    3.将第二步得到的N/2个图片ID全部复制一份,这样就得到了N个图片ID,而且每个图片ID都可以找到与之配对的。

    4.将第三步得到的N个图片ID再次“随机打乱”,并且根据图片ID加载相应的Bitmap对象,最后把图片ID及对应的Bitmap封装成PieceImage后返回。

    ImageUtil类的代码如下:srcorgcrazyitlinkutilImageUtil.java

  1 public class ImageUtil
  2 {
  3     // 保存所有连连看图片资源值(int类型)
  4     private static List<Integer> imageValues = getImageValues();
  5 
  6     //获取连连看所有图片的ID(约定所有图片ID以p_开头)
  7     public static List<Integer> getImageValues()
  8     {
  9         try
 10         {
 11             // 得到R.drawable所有的属性, 即获取drawable目录下的所有图片
 12             Field[] drawableFields = R.drawable.class.getFields();
 13             List<Integer> resourceValues = new ArrayList<Integer>();
 14             for (Field field : drawableFields)
 15             {
 16                 // 如果该Field的名称以p_开头
 17                 if (field.getName().indexOf("p_") != -1)
 18                 {
 19                     resourceValues.add(field.getInt(R.drawable.class));
 20                 }
 21             }
 22             return resourceValues;
 23         }
 24         catch (Exception e)
 25         {
 26             return null;
 27         }
 28     }
 29 
 30     /**
 31      * 随机从sourceValues的集合中获取size个图片ID, 返回结果为图片ID的集合
 32      * 
 33      * @param sourceValues 从中获取的集合
 34      * @param size 需要获取的个数
 35      * @return size个图片ID的集合
 36      */
 37     public static List<Integer> getRandomValues(List<Integer> sourceValues,
 38         int size)
 39     {
 40         // 创建一个随机数生成器
 41         Random random = new Random();
 42         // 创建结果集合
 43         List<Integer> result = new ArrayList<Integer>();
 44         for (int i = 0; i < size; i++)
 45         {
 46             try
 47             {
 48                 // 随机获取一个数字,大于、小于sourceValues.size()的数值
 49                 int index = random.nextInt(sourceValues.size());
 50                 // 从图片ID集合中获取该图片对象
 51                 Integer image = sourceValues.get(index);
 52                 // 添加到结果集中
 53                 result.add(image);
 54             }
 55             catch (IndexOutOfBoundsException e)
 56             {
 57                 return result;
 58             }
 59         }
 60         return result;
 61     }
 62 
 63     /**
 64      * 从drawable目录中中获取size个图片资源ID(以p_为前缀的资源名称), 其中size为游戏数量
 65      * 
 66      * @param size 需要获取的图片ID的数量
 67      * @return size个图片ID的集合
 68      */
 69     public static List<Integer> getPlayValues(int size)
 70     {
 71         if (size % 2 != 0)
 72         {
 73             // 如果该数除2有余数,将size加1
 74             size += 1;
 75         }
 76         // 再从所有的图片值中随机获取size的一半数量
 77         List<Integer> playImageValues = getRandomValues(imageValues, size / 2);
 78         // 将playImageValues集合的元素增加一倍(保证所有图片都有与之配对的图片)
 79         playImageValues.addAll(playImageValues);
 80         // 将所有图片ID随机“洗牌”
 81         Collections.shuffle(playImageValues);
 82         return playImageValues;
 83     }
 84 
 85     /**
 86      * 将图片ID集合转换PieceImage对象集合,PieceImage封装了图片ID与图片本身
 87      * 
 88      * @param context
 89      * @param resourceValues
 90      * @return size个PieceImage对象的集合
 91      */
 92     public static List<PieceImage> getPlayImages(Context context, int size)
 93     {
 94         // 获取图片ID组成的集合
 95         List<Integer> resourceValues = getPlayValues(size);
 96         List<PieceImage> result = new ArrayList<PieceImage>();
 97         // 遍历每个图片ID
 98         for (Integer value : resourceValues)
 99         {
100             // 加载图片
101             Bitmap bm = BitmapFactory.decodeResource(
102                 context.getResources(),  value);
103             // 封装图片ID与图片本身
104             PieceImage pieceImage = new PieceImage(bm, value);
105             result.add(pieceImage);
106         }
107         return result;
108     }
109 
110     // 获取选中标识的图片
111     public static Bitmap getSelectImage(Context context)
112     {
113         Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
114             R.drawable.selected);
115         return bm;
116     }
117 }

具体实现步骤连接:

android开发学习之路——连连看之游戏界面(一)

android开发学习之路——连连看之数据模型(二)

android开发学习之路——连连看之加载图片(三)

android开发学习之路——连连看之游戏Activity(四)

android开发学习之路——连连看之游戏逻辑(五)

原文地址:https://www.cnblogs.com/weilongfu/p/7383418.html