不錯的文章:
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