Analysis of the implementation of built-in skin-changing functions in Android apps _android

Source: Internet
Author: User
Tags throwable zip advantage

The Android platform API does not specifically provide a simple mechanism for skin rejuvenation, which may be that foreign software is more functional and easy to use and less popular for skin changes. The system does not provide direct support and can only be studied on its own.
Change the skin, you can think of the dynamic replacement of resources (text, color, font size, pictures, layout files ...). )。 This can be done dynamically using the programming language, such as Setbackgroundresource, Settextsize, SetTextColor, and so on. But it's not possible for every activity to change the skin of all the controls on the page by calling these functions, which are difficult to maintain, extend, and violate the principles of UI and Code separation (the UI in Android development is arranged in XML files). Typically, skin resources are provided outside the main program apk to reduce the size of the main program and to provide new skin extensions at any time.
In simple terms, software skins include icons, fonts, layouts, interactive styles, and so on, changing the skin to include some or all of the resources.

Mainstream application skin-changing methods:
There are a lot of software in China 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. Before writing this skin-changing series, I've also made reference to some of the other people's summaries. The knowledge points are quite scattered, so I have sorted them and further optimized and extended them. Of course, due to limited energy, part of the skin changes I have only written the realization of the main function. If there is a mistake or lack of detail, I hope you can make an opinion or expand it yourself.
In the following article, I will provide the code to explain the main ways of implementation, and do a holistic overview.
At present, the mainstream of skin-changing from the function can be divided into several ways to achieve,

1 software built-in multiple skins, can not be added or modified by the user:
Minimum degree of freedom, the software implementation is relatively simple compared to the following several.
If your programs and resources are small, you can put enough skin resources in the main program APK.
Typical application: The Apad of the QQ space on the tablet computer actually only changes the background of the activity, or the resources of this part.

2 The official provides the skin for downloading, the user can use to download the skin:

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.
Typical application: Ink weather download of the skin is a zip format compression package, in the application of the skin resources released to the application of ink in the directory, the replacement of the skin of the skin resources will replace the old skin resources every time it is loaded from the mobile phone hard disk read pictures, The naming of these picture resources is consistent with the naming of resources in the program, and once these resources are not found, you can choose to find them in the system default.
This implementation is a direct read of the external resource file, the background resource of the replacement interface that is displayed through code when the program runs.
The advantage of this approach is that the skin resource format is very arbitrary can be a zip or a custom format, as long as the program can parse to the resources on the line, the disadvantage is that the need to read and parse the file, resulting in poor efficiency.

3 The skin resources exist in the main program, such as APK, that is, the implementation of the APK split. This is similar to the browser-plug-in relationship. This approach is required when considering the need for an application to expand. This is not only reflected in the apk of the skin.
Typical application: Mobile phone QQ skin-changing realization way
Q Skin is a no interface apk application, this skin application resources and the main program resource naming consistent, through the main program and skin program sharing process to achieve the main program in the skin Program access to resources, in the program running through the code display specified skin resources, The disadvantage is that each activity in the main program to increase the complexity of the use of the skin logic, the advantage is more efficient, and the application has a good scalability, reduce the coupling of the program. Includes other similar extensions that can be implemented in this way.

4 The official provision of skin production tools or methods, the user can make homemade skin:
Roughly divided into two situations:
A. The application master list is a GridView that allows the user to select the background color and the style of the button in the settings. directly to replace.
B. Another is 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 and provide other users with downloads. Typically packaged in. zip format, extensions can be customized by company requirements. For example, ink weather skin extension is mja, Sogou method of skin extension is SGA, their file format is actually zip. The following application is similar to the second skin-changing method.
The advantage of this method is: the user has a sense of participation, freedom is higher. Users can customize the skin of the software according to their preferences.

5 Rewrite the SDK resource class:
Here are some ideas for:
In Android, resources refer mainly to pictures and MP3 types of files, as well as all elements that the user's UI contains. Google in the design of the Android system, the UI interface and logical code to build separate: interface design through the form of XML description, the specific program and application logic is implemented through the code, the front-end engineer is responsible for HTML and CSS design and architecture, Back-end engineers are specifically concerned with JSP and Java code execution.
What is the status of resource access in the Android performance architecture? When developing Android, developers often use the resource bundle Framework.jar and framework-res.apk provided by the framework, as well as the component "Resource Manager" file system related to core resources.
The APK itself is a simple file format, and is also a compressed file package. You can release the APK file by extracting the package: first you need APK's original data meta INF, manifest, and res directory. Part contains the application of the picture resource, and layout is also used when the resource is released.
When the file is installed, the system will remove the file, unpack it and put it in the Dalvik cache. There are many Dex files under the cache, and the corresponding classes are automatically loaded when the user opens the application. In the process of loading, if the system needs to access apk, it needs to be decompressed, which usually leads to low efficiency. If the Dex file is placed in the Dalvik cache, the load efficiency can be greatly improved.
Each process has a shared class and shared resource about the framework, but there is only one system level resource in the physical memory space. The framework classes and resources are read-only, while the Android operating system design has no virtual memory and swap mechanism for the hard disk, so saving memory space is a very important task.

Application built-in resources to achieve skin-changing function
through the application of built-in resources to achieve skin change, the typical application of the QQ space in the realization of skin. The application scenario is: The application is not big, and the page is less, the style is relatively simple, generally only uses realizes some resources or only uses realizes the background the replacement.
This kind of skin-changing way to achieve the idea:
1. Put several sets of skin in the Res/drawable directory, and then use Sharedpreferences to record the current skin resource ID. Then load the activity background when the program starts.
2. The main implementation in the Skin manager Skinmanager class. Adds the ID of the skin resource to the collection. The same type of the same schedule skin replacement, such as initializing the skin, obtaining the current skin symbol and the specific corresponding resources of the replacement skin.

Next look at the effect chart:

The built-in skin is relatively simple to implement, the following directly on the code:

Androidmainfest.xml

<?xml version= "1.0" encoding= "Utf-8"?> <manifest xmlns:android= 
"http://schemas.android.com/apk/res/" Android " 
 package=" Com.tony.skindemo " 
 android:versioncode=" 1 " 
 android:versionname=" 1.0 "> 
 
 <USES-SDK android:minsdkversion= "8"/> 
 
 <application 
  android:icon= "@drawable/ic_launcher" 
  Android:label= "@string/app_name" > 
  <activity 
   android:label= "@string/app_name" 
   android:name= "Com.tony.skindemo.SkinDemoActivity" > 
   <intent-filter > 
    <action android:name= " Android.intent.action.MAIN "/> 
 
    <category android:name=" Android.intent.category.LAUNCHER "/>" 
   </intent-filter> 
  </activity> 
   
   
 </application> 
 
</manifest> 

Layout file:
Main.xml

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "android:orientation=" vertical " > <textview android:textcolor= "#ff00ff" android:text= "program Skin Replacement" android:layout_width= "Fill_parent" and roid:layout_height= "Wrap_content"/> <radiogroup android:id= "@+id/skin_options" android:layout_width= Arent "android:layout_height=" wrap_content "> <radiobutton android:layout_weight=" 1 "android:id= "@+id/radiobutton1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:text= "Style 1 "/> <radiobutton android:layout_weight=" 1 "android:id=" @+id/radiobutton2 "android:layout_width=" WR Ap_content "android:layout_height=" wrap_content "android:text=" Style 2 "/> <radiobutton android:layout_ weight= "1" android:id= "@+id/radiobUtton3 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" Style 3 "/> 
   <radiobutton android:layout_weight= "1" android:id= "@+id/radiobutton4" android:layout_width= "Wrap_content" 
   android:layout_height= "wrap_content" android:text= "Style 4"/> <radiobutton android:layout_weight= "1" Android:id= "@+id/radiobutton5" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and 

 roid:text= "Style 5"/> </RadioGroup> </LinearLayout>

Program Master Activity

Package Com.tony.skindemo; 
Import android.app.Activity; 
Import Android.os.Bundle; 
Import android.view.MotionEvent; 
Import Android.view.Window; 
Import Android.view.WindowManager; 
Import Android.widget.RadioButton; 
Import Android.widget.RadioGroup; 
 
Import Android.widget.RadioGroup.OnCheckedChangeListener; 
 public class Skindemoactivity extends activity {private Skinsettingmanager Msettingmanager; 
 Private RadioButton RadioButton1; 
 Private RadioButton RadioButton2; 
 Private RadioButton RadioButton3; 
 Private RadioButton RadioButton4; 
 
 Private RadioButton radioButton5; 
 
  @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); 
  Remove the title bar requestwindowfeature (Window.feature_no_title); Complete full screen display of the form//cancel the status bar GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, Windowmanager.layoutparams . 
 
  Flag_fullscreen); 
  Setcontentview (R.layout.main); Initialize Skin Msettingmanager = new Skinsettingmanager(this); 
 
  Msettingmanager.initskins (); 
  Set Skin by radio button (customizable, such as navigation bar, also can add preview function, no longer implemented) RadioButton1 = (RadioButton) Findviewbyid (R.id.radiobutton1); 
  RadioButton2 = (RadioButton) Findviewbyid (R.id.radiobutton2); 
  RadioButton3 = (RadioButton) Findviewbyid (R.id.radiobutton3); 
  RadioButton4 = (RadioButton) Findviewbyid (R.ID.RADIOBUTTON4); 
  RadioButton5 = (RadioButton) Findviewbyid (R.ID.RADIOBUTTON5); 
  Radiogroup Radiogroup = (radiogroup) Findviewbyid (r.id.skin_options); Radiogroup.setoncheckedchangelistener (New Oncheckedchangelistener () {@Override public void oncheckedchanged (Rad Iogroup Group, int checkedid) {switch (checkedid) {case R.id.radiobutton1:msettingmanager.changeskin ( 
     1); 
 
    Break 
     Case R.id.radiobutton2:msettingmanager.changeskin (2); 
    Break 
     Case R.id.radiobutton3:msettingmanager.changeskin (3); 
    Break 
     Case R.id.radiobutton4:msettingmanager.changeskin (4); 
    Break Case R.id.radiobutton5:msettingmanager.changeskin (5); 
    Break 
    Default:break; 
 } 
   } 
  }); 
  Here in order to achieve a simple implementation, to achieve the skin change public boolean ontouchevent (Motionevent event) {msettingmanager.toggleskins (); 
 Return Super.ontouchevent (event); 

 } 
 
 
}

Skin Manager:  
 

Import android.app.Activity; 
 
Import android.content.SharedPreferences; /** * Skin Manager * @author Tony */public class Skinsettingmanager {public final static String Skin_pref = "s 
  
 Kinsetting "; 
  
 Public Sharedpreferences skinsettingpreference; Private int[] Skinresources = {r.drawable.default_wallpaper, r.drawable.wallpaper_c,r.drawable.wallpaper_d, 
  
 R.drawable.wallpaper_f, r.drawable.wallpaper_g}; 
  
  
 Private activity mactivity; 
  Public Skinsettingmanager (activity activity) {this.mactivity = activity; 
 Skinsettingpreference = Mactivity.getsharedpreferences (Skin_pref, 3); 
  /** * Get the current program's skin serial number * * @return/public int getskintype () {String key = "Skin_type"; 
 Return Skinsettingpreference.getint (key, 0); /** * To write the skin serial number to the global settings * * @param j/public void Setskintype (int j) {Sharedpreferences.editor Edito 
  R = Skinsettingpreference.edit (); 
   
  String key = "Skin_type"; Editor.putint (Key, j); 
 Editor.commit (); /** * Get current skin background map Resource ID * * @return/public int getcurrentskinres () {int skinlen = SKINRESOURCES.L 
  Ength; 
  int getskinlen = Getskintype (); 
  if (Getskinlen >= skinlen) {getskinlen = 0; 
 return Skinresources[getskinlen]; 
  public void Toggleskins () {int skintype = Getskintype (); 
  if (Skintype = = skinresources.length-1) {skintype = 0; 
  }else{Skintype + +; 
  } setskintype (Skintype); 
  Mactivity.getwindow (). setbackgrounddrawable (NULL); 
  try {Mactivity.getwindow (). Setbackgrounddrawableresource (Getcurrentskinres ()); 
 
  catch (Throwable e) {e.printstacktrace (); }/** * is used to initialize the skin */public void Initskins () {Mactivity.getwindow (). Setbackgrounddrawableresou 
 Rce (Getcurrentskinres ()); 
  /** * Immediately toggles a background skin/public void Changeskin (int id) {setskintype (ID); 
  Mactivity.getwindow (). setbackgrounddrawable (NULL); try {MactivitY.getwindow (). Setbackgrounddrawableresource (Getcurrentskinres ()); 
  catch (Throwable e) {e.printstacktrace (); 
 } 
 } 
 
}

In this way, the basic skills of the skin through the program can be completed. If you want to implement in your own application, still need to pay attention to the following points (implementation is not complex, no longer write specific implementation):
1.  to achieve multiple activity of the replacement skin. You need to take advantage of the custom MyApplication class to inherit from application. and add the collection properties of the activity. Used to store all the activity for the application. Modify the Skinmanager, remove the collection from the application when the skin is replaced, traverse and replace the skin.
2. Can optimize the user experience, through the navigation bar way into the replacement skin interface, and can be added to the preview function, when determined to modify the configuration, to complete the replacement of skin function.
3. Add Style.theme and other resources to achieve more complex skin replacement. Specific implementation of the same replacement background.
 

Related Article

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.