Android switch Topic 1

Source: Internet
Author: User

 

The recent spring blossoms are another season of gold, three silver, and four silver. With a long sigh, I still try my best to maintain my skills. Recently, I encountered a very tangled problem on the android topic. How can I ensure that android is more smooth with multiple skins. After three experiments, they are different. First, this article explains how to change the subject by using plug-ins:

Introduction: Create a project, put the resource file into it, package it into an apk, put it in the Assets of the main program, and then install the app when switching the skin, obtain the Resource object in the "Resource" Object Based on the package name of the skin apk.

Advantage: Your skin is an apk, which can be downloaded at any time and installed at any time. You can control the dynamic update of the version, which is flexible and convenient, because the memory installed on your mobile phone is not affected by the download of the memory card. It can also be independent from the main program, so it can reduce the size of the main program.

Disadvantage: You must install the SDK for the first time. If you download the SDK, it cannot be used.

Next, we will introduce the related technical points:
1. How to obtain skin Resource?

At this time, we use PackageManger. Code:

PackageManager packageManager = getPackageManager (); try {PackageInfo packageInfo = packageManager. getPackageInfo (com. springsky. face1, PackageManager. GET_ACTIVITIES); resources = packageManager. getResourcesForApplication (packageInfo. applicationInfo); resource = new ReflectResource (resources, packageInfo. packageName);} catch (NameNotFoundException e) {Toast. makeText (this, the skin is not installed, please install the skin, Toast. LENGTH_LONG ). show (); e. printStackTrace ();}

 

In the preceding method, you first use getPackageManager () to obtain the Package Manager, and then use the package name to obtain the Packageinfo object. In the Packageinfo object, the program's icon and version permissions are ensured, at present, most program management tools are amazing. They rely on PackageManager. From the PackageInfo object of the PackageInfo package, we can get the Resource object. In fact, we have already achieved half of the success. Because of this, we have basically obtained the skin resources. Next, go to step 2.

2. How to obtain the ID of each image resource?

In the Resource object we get, there is a method: res. getIdentifier (name, defType, defPackage) is very important. The int returned by getIdentifier can be understood as a unique identifier. That is, our ID. The following three parameters mean:

Name: ID name

DefType: type ()

DefPackage: package name

In fact, we only need to obtain the Image Based on the name + Type + package name of our ID.

In fact, it is not difficult to focus on the defType parameter. The value here is also fixed, for example, picture = "drawable", and others. Just read the introduction carefully.

3. How to obtain resource objects?

In fact, the way to check the Resource object is to find a method: getDrawable (id); this method is really hard to understand, so it is basically OK.

 

 

 

Below is a simple tool class.

Package com. springsky. facedemo; import java. io. IOException; import java. io. inputStream; import org. xmlpull. v1.XmlPullParser; import org. xmlpull. v1.XmlPullParserException; import android. content. context; import android. content. res. resources; import android. graphics. drawable. drawable; import android. util. attributeSet; import android. util. log; import android. util. xml; import android. view. layoutInflater; import Droid. view. view; import android. view. animation. alphaAnimation; import android. view. animation. animation; import android. view. animation. animationSet; import android. view. animation. rotateAnimation; import android. view. animation. scaleAnimation; import android. view. animation. translateAnimation;/***** @ author spring sky * resource tool class * Email: vipa1888@163.com * Creation Time: 4:08:00 */public class ReflectResource {Private Resources res; // The resprivate String apkPackageName In the retrieved resource apk; // The package name in the resource apk is public ReflectResource (Resources res, String apkPackageName) {this. res = res;this.apk PackageName = apkPackageName;}/*** get the ID number in the layout file ** @ param layoutName * layout name */public int getResApkLayoutId (String layoutName) {return res. getIdentifier (layoutName, layout, apkPackageName);}/*** Get layout file ** @ param conte Xt * Context * @ params layoutName * @ return view */public View getResApkLayoutView (context, String layoutName) {LayoutInflater inflater = (LayoutInflater) Context. getSystemService (Context. LAYOUT_INFLATER_SERVICE); return inflater. inflate (res. getLayout (getResApkLayoutId (layoutName), null);}/*** get the ID number of the control view ** @ param widgetName * Control name */public int getResApkWidgetViewID (String widgetName) {retu Rn res. getIdentifier (widgetName, id, apkPackageName);}/*** get the control in the layout file ** @ params layout, layout in the resource apk (view) * @ params widgetName Control name * @ return widgetView */public View getResApkWidgetView (View layout, String widgetName) {return layout. findViewById (getResApkWidgetViewID (widgetName);}/*** get the id of the drawable file ** @ param DrawableName * image name */public int getDrawableId (String imgName) {return res. getIdenti Fier (imgName, drawable, apkPackageName);}/*** get image resource ** @ param imgName * @ return drawable */public Drawable getResApkDrawable (String imgName) {int id = getDrawableId (imgName); if (id> 0) {return res. getDrawable (id);} Log. I (getResApkDrawable, imgName + not found in the skin plug-in); return null ;} /*** get the ID number of the string file ** @ param stringName * Name of the String in the String file */public int getResApkStringId (string stringName) {return res. g EtIdentifier (stringName, string, apkPackageName);}/*** get String string ** @ param stringName * @ return String */public String getResApkString (string stringName) {return res. getString (getResApkStringId (stringName);}/*** get the ID number in the anim file ** @ param animationName */public int getResApkAnimId (String animationName) {return res. getIdentifier (animationName, anim, apkPackageName);}/*** get anim file XmlPullParser ** @ Param animationName * @ return XmlPullParser */public XmlPullParser getResApkAnimXml (String animationName) {return res. getAnimation (getResApkAnimId (animationName);}/*** get the animation anim ** @ params animationName * @ param Animation */public animation getResApkAnim (Context context, String animationName) {Animation animation = null; XmlPullParser parser = getResApkAnimXml (animationName); AttributeSet Trs = Xml. asAttributeSet (parser); try {animation = createAnimationFromXml (context, parser, null, attrs);} catch (XmlPullParserException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} return animation;}/*** get anim Animation */private animation Animation (Context c, XmlPullParser parser, AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {Ani Mation anim = null; int type; int depth = parser. getDepth (); while (type = parser. next ())! = XmlPullParser. END_TAG | parser. getDepth ()> depth) & type! = XmlPullParser. END_DOCUMENT) {if (type! = XmlPullParser. START_TAG) {continue;} String name = parser. getName (); if (name. equals (set) {anim = new AnimationSet (c, attrs); createAnimationFromXml (c, parser, (AnimationSet) anim, attrs);} else if (name. equals (alpha) {anim = new AlphaAnimation (c, attrs);} else if (name. equals (scale) {anim = new ScaleAnimation (c, attrs);} else if (name. equals (rotate) {anim = new RotateAnimation (c, attrs);} else If (name. equals (translate) {anim = new TranslateAnimation (c, attrs);} else {throw new RuntimeException (Unknown animation name: + parser. getName ();} if (parent! = Null) {parent. addAnimation (anim) ;}} return anim;}/*** get the ID number in the color file ** @ param colorName */public int getResApkColorId (String colorName) {return res. getIdentifier (colorName, color, apkPackageName);}/*** get the color value ** @ param colorName * @ return int */public int getResApkColor (String colorName) {return res. getColor (getResApkColorId (colorName);}/*** get the ID number in The dimens file ** @ param dimenName */public int getResApkDimensId (String dimenName) {return res. getIdentifier (dimenName, dimen, apkPackageName);}/*** obtain the dimens file's median ** @ param dimenName * @ return float */public float getResApkDimens (String dimenName) {return res. getDimension (getResApkDimensId (dimenName);} public InputStream getResApkRaw (String string String) {return res. openRawResource (getResApkRawId (string);} private int getResApkRawId (String string) {return res. getIdentifier (string, raw, apkPackageName );}}

The above tool class basically provides various methods to obtain all the resources of the Resource, so you can rest assured.

The specific interface has only one line of code:

findViewById(R.id.btn_bg).setBackgroundDrawable(resource.getResApkDrawable(more_item_bg));
In this way, the skin replacement function can be completed!

 

Summary: In general, if the project can accept this method to install apk, I strongly recommend this apk, because it can be used as a plug-in with clear layers and can be used in multiple projects at the same time. If you have any questions, contact me.

 

 

Face1Resource Project is a skin Project

The FaceDemo project is the main program for skin testing. Install the skin before use.
 

 

 

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.