Android programming to achieve the skin-changing function of the example _android

Source: Internet
Author: User

An example of this article describes the method of implementing the skin-changing function of Android programming. Share to everyone for your reference, specific as follows:

This series of topic training scope: Junior Android programmer, that has J2SE Foundation and Android primary level. J2SE base is to grasp the Java syntax, 1.5, 1.6 new syntax is not fully mastered also does not matter. Understand the basic object-oriented thinking, can write a simple J2SE program, master the basic debugging methods, familiar with swing better. Android entry-level is the use of activity, Service, Broadcastreceiver, Intent, SQLite, and UI components to write simple Android applications in reference to examples.

First look at the effect of the implementation:

The skin at startup

Set the skin in Pak

Set the skin in a zip package

The purpose of this course is to learn the solution and realization of skin rejuvenation, and to master the advantages and disadvantages of different skin-changing schemes and the applicable occasions.

Hope that students do: learn a certain skill to grasp thoroughly, more practice. The best extrapolate, analogy, master analysis of problems to solve the problem of ideas and methods.

I'm going to explain the principles and the key technical points of implementation, details, optimizations, and irrelevant or rudimentary content that may not be explained in the course topics. This is free training, energy is limited, temporarily can only make ordinary become outstanding, let outstanding become outstanding. If you're already outstanding, come and teach me. Do not start training for the time being.

There are a lot of software in the country to support skin customization, which is a major difference with foreign software, foreign users pay attention to social, mail and other functions, domestic users pay attention to music, novels, skin and other functions, this section of the course to explain how the Android application of the skin function.

The function of the software can be divided into three kinds of skin:

1 software built-in multiple skin, can not be added or modified by the user;

Minimum degree of freedom, software implementation is the easiest relative to the latter two.

2 The official provision of skin for downloading, users can use the skin to download;

Users can choose to download their favorite skin, some players will crack the skin customization methods, do their own skin use, or to the Internet for everyone to use.

3 The official provision of skin production tools or methods, the user can make skin.

This way makes the user have the sense of participation, the degree of freedom is high. Users can customize the skin of the software according to their preferences. Some software websites offer customized tools or methods, and I recommend a visualizer with a visual guide. As long as the user find some pictures, modify the text of the font to replace it. Users can upload homemade skins, provide other users with downloads, and earn some virtual money or prizes. This is generally packaged as a. zip format. Extensions can be customized by the company, with tools to directly export the most convenient.

First of all, we have to understand the definition of skin, software, including icons, fonts, layout, interactive style and so on, the skin is to replace some or all of the resources included.

The three skin types mentioned above, from software implementations, are fundamentally different from whether the skin is built into the application. For the built-in implementation is relatively simple, as long as the development of applications in the process of designing a few sets of skin for users to choose. The knowledge used here is not more than the Android Foundation, not explained in detail.

This section focuses on how to achieve the separation of skin and application programs.

Skin generally contains a number of files, such as pictures, configuration and other documents, dispersed files are not conducive to transmission and use, preferably packaged. The packaged format generally selects the ZIP format. Here are two situations, one is apk, such as Adwlauncher, its desktop skin format is a apk; the other is a custom extension, for example, the ink weather skin extension is mja, Sogou input Method Skin extension is SGA, their file format is actually zip.

Here we explain separately.

A APK format

The problem now becomes an application to read resources from another apk.

In the Android system, the APK can read data to each other if they have the same signature, and the Android:shareduserid property values configured in the Androidmanifest.xml file are the same, then two apk run in the same process. Arbitrary data can be accessed from each other.

The method is as follows:

1 Application and skin programs in the Androidmanifest.xml configuration

For example: android:shareduserid= "Org.yuchen"

2 File and application apk in the same function of the skin file name to be consistent

For example: The application's background picture path:/skindemo/res/drawable-hdpi/bg.png

Then the background image file path in the skin apk should also be:

Customskin/res/drawable-hdpi/bg.png

3 Methods of accessing resources

Copy Code code as follows:
Context context = Createpackagecontext ("Com.yuchen.customskin", context.context_ignore_security);

Gets the context that corresponds to the Org.yuchen.customskin, and can access any resource in the Org.yuchen.customskin by the returned object.

For example: Apply apk to get the bg.png in the skin apk,

Copy Code code as follows:
drawable drawable = Context.getresources (). getdrawable (r.drawable.bg);

This allows you to get a reference to the picture, and other XML resource files are also captured in a similar way.

Two Custom extension of the zip format of the skin

The technical point is how to read the resources in the zip file and the skin file storage strategy.

Scenario: If the software starts every time to read the skin files on the SD card, the speed will be slow. A better approach is to provide a skin settings interface, the user selected which skin, the skin file to extract the "/data/data/[package Name]/skin" path, so no need to read across the memory, faster, And do not need to go to the ZIP compression packet every time read, do not rely on the file in the SD card, even if the skin compression package file was deleted does not matter.

Implementation method:

1. In the software Help or the official website help prompts the user to copy the skin file to the SD card specified path.

2. Provide the skin setting interface in the software. Can be in the menu or in the settings. can refer to ink, Sogou input method, QQ and other support for skin-changing software.

3. Load the skin file under the specified path, read the thumbnail, display in the Skin setting interface, extract the user's selected skin file to the "/data/data/[package Name]/skin" path.

4. The resources under the "/data/data/[package name]/skin/" path are first read in the software. If not, use the resources in APK.

I hope this article will help you with the Android program.

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.