Android游戏加载资源通用类
开发游戏,通常需要在Assets中加载图片和多语言包,下面有个封装类。
/**
* 图片加载类.
*
* @author 凉
*
*/
public class Assets {
private static Map<String, Map<String, String>> drawables = new HashMap<String, Map<String, String>>();
private static Map<String, String> raws = new HashMap<String, String>();
/**
* 如果是中文,则从drawable-zh目录加载图片.如果没有,在从drawable里面加载.
*
* @param name
* @return
*/
public static String get(String name) {
String result = null;
if (isLunarSetting()) {
Map map = (Map) drawables.get("drawable-zh");
if (map != null) {
String str3 = get(map, name);
MyLog.d("", ">>>>>>>>>>>path>>>cn>>drawables:" + str3
+ " name:" + name);
if (str3 != null) {
result = str3;
} else {
String str4 = get((Map) drawables.get("drawable"), name);
MyLog.d("", ">>>>>>>>>>>path>>>en>1>drawables:" + str4
+ " name:" + name);
if (str4 != null) {
result = str4;
}
}
} else {
String str2 = get((Map) drawables.get("drawable"), name);
MyLog.d("", ">>>>>>>>>>>path>>>en>2>drawables:" + str2
+ " name:" + name);
if (str2 != null) {
result = str2;
}
}
} else {
String str1 = get((Map) drawables.get("drawable"), name);
MyLog.d("", ">>>>>>>>>>>path>>>en>3>drawables:" + str1 + " name:"
+ name);
if (str1 != null) {
result = str1;
}
}
return result;
}
private static String get(Map<String, String> map, String name) {
String str = null;
if (map.get(name + ".png") != null)
str = (String) map.get(name + ".png");
else {
if (map.get(name + ".jpg") != null) {
str = (String) map.get(name + ".jpg");
}
}
return str;
}
private static String getLanguageEnv() {
Locale local = Locale.getDefault();
String language = local.getLanguage();
String country = local.getCountry().toLowerCase();
if ("zh".equals(language)) {
if ("cn".equals(country))
language = "zh-CN";
else if ("tw".equals(country)) {
language = "zh-TW";
}
} else if ("pt".equals(language)) {
if ("br".equals(country)) {
language = "pt-BR";
} else if ("pt".equals(country)) {
language = "pt-PT";
}
}
return language;
}
/**
* 获取.plist资源.
*
* @param name
* @return
*/
public static String getRaw(String name) {
return (String) raws.get(name + ".plist");
}
/**
* 把文件夹res下面的drawable和raw下面的文件路径全部缓存起来.
*
* @param context
* @throws IOException
*/
public static void init(Context context) throws IOException {
String[] arraystr = context.getAssets().list("res");
int i = arraystr.length;
for (int j = 0; j < i; j++) {
String str1 = arraystr[j];
String[] strings;
HashMap drawable;
if (str1.startsWith("drawable")) {
strings = context.getAssets().list("res/" + str1);
drawable = new HashMap();
int n = strings.length;
for (int i1 = 0; i1 < n; i1++) {
String str3 = strings[i1];
drawable.put(str3, "res/" + str1 + "/" + str3);
}
drawables.put(str1, drawable);
} else if (str1.equals("raw")) {
String[] lists = context.getAssets().list("res/" + str1);
for (String str2 : lists)
raws.put(str2, "res/" + str1 + "/" + str2);
}
}
}
/**
* 是否是中文.
*
* @return
*/
public static boolean isLunarSetting() {
String str = getLanguageEnv();
return ((str != null) && ((str.trim().equals("zh-CN")) || (str.trim()
.equals("zh-TW"))));
}
}