Resources for the skin-changing framework

Source: Internet
Author: User

The skin needs the corresponding resources, then resources have what source.

When 1.APK is packaged, several default resources are packaged into the APK.

2. Download from the network, put to the specified path.


According to the Skinmanager of the previous chapter:

@Override protected Resources doinbackground (String ... params) {try {if (params.length = = 1) {S
            Tring Skinpkgpath = params[0]; LOG.E ("Loadskin", Skinpkgpath);///storage/emulated/0/android/data/ren.solid.materialdesigndemo/cache/skin/skin_
            Brown.skin File File = new file (Skinpkgpath);
            if (file = = NULL | |!file.exists ()) {return null;
            } Packagemanager mPm = Context.getpackagemanager ();
            PackageInfo mInfo = Mpm.getpackagearchiveinfo (Skinpkgpath, packagemanager.get_activities);

            Skinpackagename = Minfo.packagename;
            Assetmanager Assetmanager = AssetManager.class.newInstance ();
            Method Addassetpath = Assetmanager.getclass (). GetMethod ("Addassetpath", String.class);


            Addassetpath.invoke (Assetmanager, Skinpkgpath);
            Resources Superres = Context.getresources (); Skinresource = new Resources (Assetmanager,Superres.getdisplaymetrics (), superres.getconfiguration ());

            Skinconfig.saveskinpath (context, Skinpkgpath);
            Skinpath = Skinpkgpath;
            Isdefaultskin = false;
        return skinresource;
    return null;
        catch (Exception e) {e.printstacktrace ();
    return null; }
}
He did three things.

First step: He gets the package name for the specified path:

Packagemanager mPm = Context.getpackagemanager ();
            PackageInfo mInfo = Mpm.getpackagearchiveinfo (Skinpkgpath, packagemanager.get_activities);
            Skinpackagename = Minfo.packagename;
Step two: Use the asset loader to load the corresponding asset resource:
Assetmanager Assetmanager = AssetManager.class.newInstance ();
            Method Addassetpath = Assetmanager.getclass (). GetMethod ("Addassetpath", string.class);
            Addassetpath.invoke (Assetmanager, Skinpkgpath);
Step three: Generate the resources object from the loader:

            Resources Superres = Context.getresources ();
            Skinresource = new Resources (Assetmanager, Superres.getdisplaymetrics (), superres.getconfiguration ());
After the completion of these three steps will be able to obtain the corresponding resources resource objects, we can use the resources in the resource, corresponding to the skin can be achieved.


The three steps above are essentially finding the path to the resource and generating the corresponding resources object.

when 1.APK is packaged, several default resources are packaged into the APK.

MYAPPLICATION.APK (main application) skin.apk (apk files containing only resources, skin apk,apk essentially compressed files) the structure of their Android studio is this:

There is nothing in the structure of the skin, except for the resource files that need to be replaced: the structure is as follows:



How to create a assets folder on the internet there is no say. Then the application package, Android to the mobile phone, assets folder under the file is saved in the system of unified management Assets file path, this area I found in Baidu for a long time, but also tried a lot of eventually can not be found in the form of a path, Instead, you can only invoke the Assetsmanager open method to turn on the InputStream data stream of the corresponding file name.


Only then, there is only one idea, that is, the form of asynchronous InputStream data stream copy to the application of the cache folder (this path to get online also have, do not say), the specific code is as follows:

public class Copyutil {public static Boolean copyapkfromassets (context con
        Text, String fileName) {Boolean copyisfinish = false;
        String path= Context.getexternalcachedir () + File.separator + "skin.apk";
            try {InputStream is = Context.getassets (). open (FileName);//Key code file = new file (path);
            File.createnewfile ();
            FileOutputStream fos = new FileOutputStream (file);
            byte[] temp = new byte[1024];
            int i = 0;
            while ((i = is.read (temp)) > 0) {fos.write (temp, 0, i);
            } fos.close ();
            Is.close ();
        Copyisfinish = true;
        catch (IOException e) {e.printstacktrace ();
    return copyisfinish;
        public static Boolean Isexistfile (String path) {file F=new file (path);
    return f.exists (); }
}
With the above code, we can copy the skin.apk to the following path
storage/emulated/0/android/data/com.example.myapplication/cache/skin.apk

Then invoke the Skinmanager load function to load the assets resource asynchronously to get the resources object.


2. Download from the network, put to the specified path .

The principle of the same 1, but through the network to download skin.apk to the following path

storage/emulated/0/android/data/com.example.myapplication/cache/skin.apk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.