随笔 - 62  文章 - 0  评论 - 114  阅读 - 18万

高仿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   nuliniao  阅读(2230)  评论(2编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
< 2012年12月 >
25 26 27 28 29 30 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示