Android中實(shí)現(xiàn)根據(jù)資源名獲取資源ID
接觸過(guò)Android開(kāi)發(fā)的同學(xué)們都知道在Android中訪問(wèn)程序資源基本都是通過(guò)資源ID來(lái)訪問(wèn)。這樣開(kāi)發(fā)起來(lái)很簡(jiǎn)單,并且可以不去考慮各種分辨率,語(yǔ)言等不同資源顯式指定。
痛點(diǎn)
但是,有時(shí)候也會(huì)有一些問(wèn)題,比如我們根據(jù)服務(wù)器端的值取圖片,但是服務(wù)器端絕對(duì)不會(huì)返回給我們的是資源id,最多是一種和文件名相關(guān)聯(lián)的值,操作資源少的時(shí)候,可以維護(hù)一個(gè)容器進(jìn)行值與資源ID的映射,但是多的話(huà),就需要另想辦法了。
便捷的方法
在這種情況下,使用文件名來(lái)得到資源ID顯得事半功倍。 通過(guò)調(diào)用Resources的getIdentifier可以很輕松地得到資源ID。 幾個(gè)簡(jiǎn)單的示例:
Resources res = getResources();
final String packageName = getPackageName();
int imageResId = res.getIdentifier("ic_launcher", "drawable", packageName);
int imageResIdByAnotherForm = res.getIdentifier(packageName + ":drawable/ic_launcher", null, null);
int musicResId = res.getIdentifier("test", "raw", packageName);
int notFoundResId = res.getIdentifier("activity_main", "drawable", packageName);
Log.i(LOGTAG, "testGetResourceIds imageResId = " + imageResId
+ ";imageResIdByAnotherForm = " + imageResIdByAnotherForm
+ ";musicResId=" + musicResId
+ ";notFoundResId =" + notFoundResId);
運(yùn)行結(jié)果
I/MainActivity( 4537): testGetResourceIds imageResId = 2130837504;imageResIdByAnotherForm = 2130837504;musicResId=2130968576;notFoundResId =0
看一看API
直接API
1.這個(gè)方法用來(lái)使用資源名來(lái)獲取資源ID
2.完整的資源名為package:type/entry,如果資源名這個(gè)參數(shù)有完整地指定,后面的defType和defPackage可以省略。
3.defType和defPackage省略時(shí),需要將其設(shè)置成null
4.注意這個(gè)方法不提倡,因?yàn)橹苯油ㄟ^(guò)資源ID訪問(wèn)資源會(huì)更加效率高
5.如果資源沒(méi)有找到,返回0,在Android資源ID中0不是合法的資源ID。
**
* Return a resource identifier for the given resource name. A fully
* qualified resource name is of the form "package:type/entry". The first
* two components (package and type) are optional if defType and
* defPackage, respectively, are specified here.
*
* <p>Note: use of this function is discouraged. It is much more
* efficient to retrieve resources by identifier than by name.
*
* @param name The name of the desired resource.
* @param defType Optional default resource type to find, if "type/" is
* not included in the name. Can be null to require an
* explicit type.
* @param defPackage Optional default package to find, if "package:" is
* not included in the name. Can be null to require an
* explicit package.
*
* @return int The associated resource identifier. Returns 0 if no such
* resource was found. (0 is not a valid resource ID.)
*/
public int getIdentifier(String name, String defType, String defPackage) {
try {
return Integer.parseInt(name);
} catch (Exception e) {
// Ignore
}
return mAssets.getResourceIdentifier(name, defType, defPackage);
}
間接API
實(shí)際上上述API調(diào)用的是AssetManager.class中的native方法。
/**
* Retrieve the resource identifier for the given resource name.
*/
/*package*/ native final int getResourceIdentifier(String type,
String name,
String defPackage);
相關(guān)文章
利用Android中BitmapShader制作自帶邊框的圓形頭像
這篇文章給大家介紹了一下如何利用BitmapShader制作圓形頭像,可以自定義要顯示的圖片,邊框顏色和邊框?qū)挾鹊?,有需要的朋友們可以參考借鑒。2016-09-09
android橫豎屏切換時(shí)候Activity的生命周期
曾經(jīng)遇到過(guò)一個(gè)面試題,讓你寫(xiě)出橫屏切換豎屏Activity的生命周期?,F(xiàn)在給大家分析一下他切換時(shí)具體的生命周期是怎么樣的2013-01-01
android實(shí)現(xiàn)圖片反轉(zhuǎn)效果
這篇文章主要介紹了android實(shí)現(xiàn)圖片反轉(zhuǎn)效果的方法,需要的朋友可以參考下2015-09-09
Android編程實(shí)現(xiàn)仿易信精美彈出框效果【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿易信精美彈出框效果,涉及Android窗口及動(dòng)畫(huà)操作相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-01-01
完全解析Android多線(xiàn)程中線(xiàn)程池ThreadPool的原理和使用
本篇文章給大家通過(guò)原理和實(shí)例詳細(xì)講述了Android多線(xiàn)程中線(xiàn)程池ThreadPool的原理和使用,對(duì)此有興趣的朋友可以跟著參考學(xué)習(xí)下。2018-04-04
簡(jiǎn)單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果
這篇文章主要為大家詳細(xì)介紹了簡(jiǎn)單實(shí)用的Android UI微博動(dòng)態(tài)點(diǎn)贊效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-10-10
Flutter實(shí)現(xiàn)一個(gè)支持漸變背景的Button示例詳解
這篇文章主要為大家介紹了Flutter實(shí)現(xiàn)一個(gè)支持漸變背景的Button示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
android studio3.0以上如何通過(guò)navicat訪問(wèn)SQLite數(shù)據(jù)庫(kù)文件
這篇文章主要介紹了android studio3.0以上如何通過(guò)navicat訪問(wèn)SQLite數(shù)據(jù)庫(kù)文件,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06

