Nitro's

Dec 3, 2013 - Comments - dev tech life

Android 图片数组定义和读取

位置:packages/apps/Launcher2

1、图片数组定义、资源读取

如果有多张图片,这些图片的使用与顺序无关,可以采取这种方式。

drawable-nodpi中有3张图片,wallpaper_1.jpg、wallpaper_2.jpg、wallpaper_3.jpg

XML中定义数组ID

<string-array>
    <item>wallpaper_1</item>
    <item>wallpaper_2</item>
    <item>wallpaper_3</item>
</string-array>

Java代码获取图片资源ID

final Resources resources = context.getResources();
final String packageName = resources.getResourcePackageName(R.array.wallpaper);

String[] wallPaperNames = resources.getStringArray(R.array.wallpaper);

for(String wallPaperName : wallPaperNames){
    //图片资源ID
    int res = resources.getIdentifier(wallPaperName, "drawable", packageName);
}

注:context.getPackageName会返回original-package,resources.getPackageName会返回真正的包路径。

Tags: android

Android Activity 启动方式 谷歌眼镜以旧换新计划

comments powered by Disqus