Android App switch skin function implementation

Source: Internet
Author: User

Original address: http://www.eoeandroid.com/thread-318159-1-1.html

Now mostAndroidThe app supports the ability to switch skins. such as the chin listening, ink weather and so on. This article describes two methods of changing the skin. 1. The first type is by installing the skinapkthe way. When the skin is installedapkafter the package, the main program only needs to read the skinapkpicture resources in the package. Can be achieved by switching the skin. To getapkpicture resources in the skin pack that need to get to the skinapkthe context of the packageContext.[Mw_shl_code=java,true] try {
CurrentContext = Context.createpackagecontext (
PackageName,//packagename is the package name that corresponds to the skin apk package.
context.context_include_code| context.context_ignore_security);

} catch (Exception e) {
E.printstacktrace ();
}[/mw_shl_code]The name and location of the picture resource should be the same. Good, like the background of the entire interface.mainbg.jpg, in the main program and skin packapkare located in theres\drawable-hdpi\mainbg.jpg.the next. Then use the following method: [mw_shl_code=java,true] public static drawable getdrawable (Context context,string drawablename) throws notfoundexception {
drawable resultdrawable =null;
Context CT =getskincontext (context);
try{
int Resid=getidentifier2 (Ct,drawablename.trim (), "drawable");
if (Resid = = 0) {
Resid=getidentifier2 (Context,drawablename.trim (), "drawable");
Resultdrawable=context.getresources (). getdrawable (RESID);
}else{
Resultdrawable=ct.getresources (). getdrawable (RESID);
}
}catch (Exception e) {
E.printstacktrace ();

}
return resultdrawable;
}[/mw_shl_code] will be able to obtain the corresponding skin resources. As you can see, the first thing to do is to get the corresponding resource by using the following methodID, and then moreIDget to the corresponding picture resource. [mw_shl_code=java,true] public static int getIdentifier2 (Context context,string name,string Deftype) throws notfoundexception{
int result=0;
try{
result = Context.getresources (). Getidentifier (name, Deftype, Context.getpackagename ());
}catch (Exception e) {
E.printstacktrace ();

}
return result;
}[/mw_shl_code]And then set the corresponding control'sdrawable.The switch skin is realized. Because the skinapkThe context of the package is often used, so I saved it with a static variable. If the skin packapkcan not find the corresponding picture resources, in the main program of the picture resources, not to find the picture, so that the interface blank. 2. The second way is to put all of the picture resources together, into a compressed package. When applying, unzip the picture toSDcard or an application.Datadirectory. The following method is used to get the image resourcesdrawable. In puttingdrawableset to the control. [Mw_shl_code=java,true]public Static drawable Createdrawablebypath (Context context,string imagePath) {
try {

if (Stringutils.isnull (imagePath)) return null;

Bitmap BM = bitmapfactory.decodefile (ImagePath);

Bm.setdensity (Context.getresources (). Getdisplaymetrics (). Density_high);

drawable DW = new bitmapdrawable (context.getresources (), BM);

return DW;

} catch (Exception e) {
E.printstacktrace ();
}catch (OutOfMemoryError ex) {
System.GC ();
Ex.printstacktrace ();
}
return null;
}[/mw_shl_code]BecauseSDsometimes the card will not load successfully. Or not at all .SDcard. All I have to unzip the image of the compressed package directly to theContext.getfilesdir (). GetPath () + File.separator + "Skin" + file.separatorthe next. The main program only implements a simple skin-changing function. There are only two interfaces, one is the main interface, its background shows a background, is the individual skin. The other one is to set the skin interface.
Click on the skin to set the interface:
The other two skin works are sunny skin and night skin respectively. These two projects are very simple, in fact, they do not needActivity. Just need to put the picturemainbg.jpgput inres\drawable-hdpi\mainbg.jpg.the next. The other two lightning skinsZipbag and snow skinZippackage, I have put it in the main program. Click on the button to unzip. Of course, for example, Cang teacher, hehe. When you click on Mr. Cang's skinZipwill simulate downloading from the network.ZipThe package implements the switch skin. [Mw_shl_code=java,true] Mprogressdialog = Progressdialog.show (Setskinactivity.this,
"",
"Downloading teacher Cang ...", true, false);

Handler.postdelayed (New Runnable () {

@Override
public void Run () {
try {
Mprogressdialog.cancel ();
Toast.maketext (Setskinactivity.this, "Cang teacher download finished!" ", Toast.length_long). Show ();


Ziputil.unzip (Getassets (). Open ("Cang.zip"),
constant.skin_dir+ "Cang.zip", Constant.skin_dir);

Constant.setappskin (Setskinactivity.this, SetSkinActivity.this.getPackageName ());
Restartmain ();

} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

}
}, 3000);
[/mw_shl_code]
Skin-changing effect: the last Source:
Android, encryption, skin <ignore_js_op>

2.0.zip

6.5 MB, download number of times: 137

Android App switch skin function implementation

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.