Examples of how to create a custom Resource

Learned from the Resource constructor Resources (AssetManager assets, DisplayMetrics metrics, Configuration config), need to get the apk file Resource objects external resources app, you first need to create the corresponding AssetManager object.

public final class AssetManager implements AutoCloseable {
/**
* Create a new AssetManager containing only the basic system assets.
* Applications will not generally use this method, instead retrieving the
* appropriate asset manager with {@link Resources#getAssets}. Not for
* use by applications.
* {@hide}
*/
public AssetManager() {
synchronized (this) {
if (DEBUG_REFS) {
mNumRefs = 0;
incRefsLocked(this.hashCode());
}
init(false);
if (localLOGV) Log.v(TAG, "New asset manager: " + this);
ensureSystemAssets();
}
}
/**
* Add an additional set of assets to the asset manager. This can be
* either a directory or ZIP file. Not for use by applications. Returns
* the cookie of the added asset, or 0 on failure.
* {@hide}
*/
//添加额外的asset路径
public final int addAssetPath(String path) {
synchronized (this) {
int res = addAssetPathNative(path);
if (mStringBlocks != null) {
makeStringBlocks(mStringBlocks);
}
return res;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
. 19
20 is
21 is
22 is
23 is
24
25
26 is
27
28
29
30
31 is
32
33 is
34 is
35
can be created by reflection corresponding AssertManager, thereby creating the Resource instances corresponding to the code is as follows:

private final static Resources loadTheme(String skinPackageName, Context context){
String skinPackagePath = Environment.getExternalStorageDirectory() + "/" + skinPackageName;
File file = new File(skinPackagePath);
Resources skinResource = null;
if (!file.exists()) {
return skinResource;
}
try {
//创建AssetManager实例
AssetManager assetManager = AssetManager.class.newInstance();
Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
addAssetPath.invoke(assetManager, skinPackagePath);
//构建皮肤资源Resource实例
Resources superRes = context.getResources();
skinResource = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());

} The catch (Exception E) {
skinResource = null;
}
return skinResource;
}
. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
2, how to know the current property values id in the host Resource of
in the Resource of the source code can be found

Resources {class public
/ **
* by a resource name, return type, and package names id identifying a resource.
* @Param Description Name The name of the resource
* @param defType type the name of the resource
* @param defPackage package name
*
* @return returns a resource id, 0 identifies the resource was not found
* /
public int getIdentifier (String name, String DEFTYPE, String defPackage ) {
IF (name == null) {
the throw a NullPointerException new new ( "null name IS");
}
the try {
return the Integer.parseInt (name);
} the catch (Exception E) {
// the Ignore
}
return mAssets.getResourceIdentifier (name, deftype, defPackage);
}
}

. 1
2
. 3
. 4
. 5
. 6
. 7
. 8
. 9
10
. 11
12 is
13 is
14
15
16
. 17
18 is
. 19
20 is
21 is
22 is
that is an arbitrary apk file only needs to know the package name (as specified in the manifest.xml package name for finding Java classes and resources), type the name of the resource, resource description name.
For example: There is a defType A package as "color", name of color_red_1 properties, color can be acquired in the name of the resource bundle B by Resource # getIdentifier.

//将skina重View的背景色设置为com.example.skinb中所对应的颜色
if (attrValue.startsWith("@") && attrName.contains("background")){
int resId = Integer.parseInt(attrValue.substring(1));
int originColor = mContext.getResources().getColor(resId);
if (mResource == null){
return originColor;
}
String resName = mContext.getResources(http://www.my516.com).getResourceEntryName(resId);
int skinRealResId = mResource.getIdentifier(resName, "color", "com.example.skinb");
int skinColor = 0;
try{
skinColor = mResource.getColor(skinRealResId);
}catch (Exception e){
Log.e(TAG, "", e);
skinColor = originColor;
}
view.setBackgroundColor(skinColor);
}
--------------------- 

Guess you like

Origin www.cnblogs.com/ly570/p/11284697.html