Android dynamic skinning framework implementation summary

Android dynamic skinning framework implementation summary

Advantages of dynamic skinning

  • Reduce APK size

  • You can update the skin without updating the apk

  • Can change skins in real time

Overall implementation process

  1. Monitor the creation process of View by setting LayoutInflector.Factory2, and collect View and properties that need to be skinned

  2. Load skin package resources and package them into AssetManager and Resources

  3. Traverse the View that needs to be skinned, obtain the resource name and type through the original attribute resource id, and then take out the latest skin resource from the Resources object of the skin package according to the resource name and type, and apply the new resource to the View

implementation details

  • What is the process of collecting Views that need to be skinned?

    • When parsing the xml layout file, the Create View method of LayoutInflactor.Factory2 will be called first. If it returns null, the system will use the default Create View method, so we can judge whether the View contains custom attributes in Factory2, according to the custom attributes Value to decide whether to support skin update;

    • If it is not needed, it will return null. If it is needed, we will create a View by ourselves and apply the latest resources in the skin package.

    • When collecting a View that needs to be skinned, we need to save its set attributes and corresponding values, and then obtain the corresponding resource type and resource name according to the resource id of the attribute. When changing the skin, go to the skin package corresponding to the resource name and type. Get the corresponding resource from the Resources object and apply it to the View

  • How to load skin pack resources?

    Build the AssetManager object through reflection, and call its addAssetPathmethod to add the path where the skin package is located, and then build a new Resources object through AssetManager to manage the resources in the skin package

Guess you like

Origin blog.csdn.net/guangdeshishe/article/details/129968726