高仿Android 点心桌面皮肤实现方式

不错的帖子:

android之ViewFlipper实现左右滑动动画效果
http://www.eoeandroid.com/thread-235446-1-1.html

Android TreeView 树形结构
http://www.eoeandroid.com/thread-235033-1-1.html

前段时间有空研究了一下Launcher,做了一个自己的桌面,像点心桌面那样可以切换皮肤的,所以在这里和大家分享一下应用皮肤的实现方式。我是这样实现的,主的应用是一个apk,皮肤是一个apk,在主的应用中读取已安装的apk。
先附上几张切图

  

  

先来讲解一下皮肤apk吧,下面是皮肤apk的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.app.GreenSkinExample"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".GreenSkin"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <action android:name="com.app.skinexample.Theme"/>
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
 
    </application>
    <uses-sdk android:minSdkVersion="4" />
 
</manifest>

其中<action android:name="com.app.skinexample.Theme"/>是比较重要的,我们就靠它在主应用中找到此皮肤。然后皮肤apk的res文件夹里放需要的图片、文字、颜色资源。

在主应用apk中,我们用以下代码片段来读取皮肤apk

public static final String THEME_ACTION = "com.app.skinexample.Theme" ;
public List<ResolveInfo> themes ;
        public void GetAllThemes()
        {
                Intent mIntent = new Intent(THEME_ACTION) ;
                PackageManager pm = getPackageManager() ;
                 
                themes = pm.queryIntentActivities(mIntent, 0) ;
                 
        }

最重要的,如何在主apk中使用皮肤apk替换原有的呢,看一下代码

public Resources themeResources = null ;
public String themePackage ;
public void ThemeLoading() 
{
    PackageManager pm = getPackageManager() ;
 
 try {
                                 themeResources = pm.getResourcesForApplication(themePackage) ;
                          } 
                    catch (NameNotFoundException e) {
                                 //The saved theme was uninstalled so we save the default one
                                   
                           }
Drawable icon = null ;
                 
                int resource_id=themeResources.getIdentifier (resourceName, "drawable", themePackage);
                if(resource_id!=0){
                        icon=themeResources.getDrawable(resource_id);
                }
 
}

仿点心桌面皮肤实现方式程序源码:

ThemeUtil.java

public static void DisplayToast(Context context,String text)
    {
        Toast toast ;
        toast = Toast.makeText(context ,
                text, Toast.LENGTH_SHORT);
               toast.setGravity(Gravity.CENTER, 0, 0);
               toast.show();
    }

    
    public static void loadThemeResource(Resources themeResources,String themePackage, 
            String item_name, View item,int themeType) 
    {
        Drawable d=null;
        if(themeResources!=null)
        {
            int resource_id=themeResources.getIdentifier (item_name, "drawable", themePackage);
            if(resource_id!=0)
            {
                d=themeResources.getDrawable(resource_id);
                if(themeType==ThemeAttributes.THEME_ITEM_FOREGROUND && item instanceof ImageView)
                {
                    Drawable tmp=((ImageView)item).getDrawable();
                    if(tmp!=null){
                        tmp.setCallback(null);
                        tmp=null;
                    }
                    ((ImageView)item).setImageDrawable(d);
                    
                }
                else
                {
                    Drawable tmp=item.getBackground();
                    if(tmp!=null){
                        tmp.setCallback(null);
                        tmp=null;
                    }
                    item.setBackgroundDrawable(d);
                    
                }
            }
        }
    }
    
    
    public static Drawable loadThemeDrawable(Resources themeResources , String resourceName , String themePackage)
    {
        Drawable icon = null ;
        
        int resource_id=themeResources.getIdentifier (resourceName, "drawable", themePackage);
        if(resource_id!=0){
            icon=themeResources.getDrawable(resource_id);
        }
        
        return icon ;
    }
    
    public static int loadThemeColor(Resources themeResources , String resourceName , String themePackage)
    {
        int color = 0 ;
        int resource_id=themeResources.getIdentifier (resourceName, "color", themePackage);
        if(resource_id!=0){
            color=themeResources.getColor(resource_id);
        }
        
        return color ;
        
    }

首发地址:http://www.eoeandroid.com/thread-231199-1-1.html

 

 源码下载:LauncherSkinExample.zip

 

posted on 2012-12-06 15:49  nuliniao  阅读(2225)  评论(2编辑  收藏  举报