原文地址::http://blog.csdn.net/wzcqr0501/article/details/7301227
在平板上android系統預設的案頭表徵圖太小了,需要將其放大。之前在launcher中做了簡單的圖片放大,帶
來了表徵圖模糊的問題,重新研究源碼尋求解決辦法。
(1)解決思路是先找到應用程式的表徵圖等資訊是怎麼擷取的。這就需要學習PackageManager的相關源碼。
PackageManger類包含以下功能:
安裝,卸載應用
查詢permission相干資訊
查詢Application相干資訊(application,activity,receiver,service,provider及響應屬性等)從
AndroidManifest.xml擷取的這些的資訊
查詢已安裝應用
增長,刪除permission
清除使用者資料、緩衝,程式碼片段
可以通過getPackageManager()方法獲得。
此類是一個抽象類別。它的實現過程可以參考http://blog.csdn.net/ljsbuct/article/details/6636433中的分析。
PackageItemInfo類
說明: AndroidManifest.xml檔案中所有節點的基類,提供了這些節點的基本資料:a label、icon、 meta-data。
常用欄位:
public int icon 獲得該資源圖片在R檔案中的值 (對應於android:icon屬性)
public int labelRes 獲得該label在R檔案中的值(對應於android:label屬性)
public String name 獲得該節點的name值 (對應於android:name屬性)
public String packagename 獲得該應用程式的包名 (對應於android:packagename屬性)
常用方法:
Drawable loadIcon(PackageManager pm) 獲得當前應用程式的映像
CharSequence loadLabel(PackageManager pm) 獲得當前應用程式的label
PackageItemInfo類中的這些方法時利用PackageManager中的方法實現的。
PackageItemInfo類並不直接使用,而是由子類繼承然後調用相應方法。例如ActivityInfo類、ServiceInfo類和ApplicationInfo類等。
(2)我們來具體看下Launcher中時如何擷取應用程式icon的:
在IconCache.java中的
private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
.....
entry.icon = Utilities.createIconBitmap(
info.activityInfo.loadIcon(mPackageManager), mContext);
}
也就是調用ActivityInfo類的loadIcon來擷取drawable型的icon資訊。
接著我們看Utilities中的createIconBitmap方法,其中有
if (sourceWidth > 0 && sourceWidth > 0) {
// There are intrinsic sizes.
if (width < sourceWidth || height < sourceHeight) {
// It's too big, scale it down.
final float ratio = (float) sourceWidth / sourceHeight;
if (sourceWidth > sourceHeight) {
height = (int) (width / ratio);
} else if (sourceHeight > sourceWidth) {
width = (int) (height * ratio);
}
} else if (sourceWidth < width && sourceHeight < height) {
// It's small, use the size they gave us.
width = sourceWidth;
height = sourceHeight;
}
}
這段代碼對icon大小處理,如果icon是固有尺寸大於我們定義的尺寸就進行裁剪,如果小於我們定義的尺寸就採用其固有尺寸。
而尺寸是在這裡定義的:
sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
定義這個app_icon_size是在\frameworks\base\core\res\res\values中的dimens.xml中。
<!-- The standard size (both width and height) of an application icon that will be displayed in the app launcher and elsewhere. -- >
<dimen name="app_icon_size">48dip</dimen>
(3)接下來我們在launcher源碼中自己定義一個app_icon_size為72dip,編譯運行應用程式列表中的表徵圖果然變大了,但是擋住了下面的文字。
在layout/aplication_boxed.xml中把android:layout_height從88dip改成108dip。再次運行可以看到應用程式列表大表徵圖效果是不錯的。
在平板上android系統預設的案頭表徵圖太小了,需要將其放大。之前在launcher中做了簡單的圖片放大,帶
來了表徵圖模糊的問題,重新研究源碼尋求解決辦法。
(1)解決思路是先找到應用程式的表徵圖等資訊是怎麼擷取的。這就需要學習PackageManager的相關源碼。
PackageManger類包含以下功能:
安裝,卸載應用
查詢permission相干資訊
查詢Application相干資訊(application,activity,receiver,service,provider及響應屬性等)從
AndroidManifest.xml擷取的這些的資訊
查詢已安裝應用
增長,刪除permission
清除使用者資料、緩衝,程式碼片段
可以通過getPackageManager()方法獲得。
此類是一個抽象類別。它的實現過程可以參考http://blog.csdn.net/ljsbuct/article/details/6636433中的分析。
PackageItemInfo類
說明: AndroidManifest.xml檔案中所有節點的基類,提供了這些節點的基本資料:a label、icon、 meta-data。
常用欄位:
public int icon 獲得該資源圖片在R檔案中的值 (對應於android:icon屬性)
public int labelRes 獲得該label在R檔案中的值(對應於android:label屬性)
public String name 獲得該節點的name值 (對應於android:name屬性)
public String packagename 獲得該應用程式的包名 (對應於android:packagename屬性)
常用方法:
Drawable loadIcon(PackageManager pm) 獲得當前應用程式的映像
CharSequence loadLabel(PackageManager pm) 獲得當前應用程式的label
PackageItemInfo類中的這些方法時利用PackageManager中的方法實現的。
PackageItemInfo類並不直接使用,而是由子類繼承然後調用相應方法。例如ActivityInfo類、ServiceInfo類和ApplicationInfo類等。
(2)我們來具體看下Launcher中時如何擷取應用程式icon的:
在IconCache.java中的
private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info) {
.....
entry.icon = Utilities.createIconBitmap(
info.activityInfo.loadIcon(mPackageManager), mContext);
}
也就是調用ActivityInfo類的loadIcon來擷取drawable型的icon資訊。
接著我們看Utilities中的createIconBitmap方法,其中有
if (sourceWidth > 0 && sourceWidth > 0) {
// There are intrinsic sizes.
if (width < sourceWidth || height < sourceHeight) {
// It's too big, scale it down.
final float ratio = (float) sourceWidth / sourceHeight;
if (sourceWidth > sourceHeight) {
height = (int) (width / ratio);
} else if (sourceHeight > sourceWidth) {
width = (int) (height * ratio);
}
} else if (sourceWidth < width && sourceHeight < height) {
// It's small, use the size they gave us.
width = sourceWidth;
height = sourceHeight;
}
}
這段代碼對icon大小處理,如果icon是固有尺寸大於我們定義的尺寸就進行裁剪,如果小於我們定義的尺寸就採用其固有尺寸。
而尺寸是在這裡定義的:
sIconWidth = sIconHeight = (int) resources.getDimension(android.R.dimen.app_icon_size);
定義這個app_icon_size是在\frameworks\base\core\res\res\values中的dimens.xml中。
<!-- The standard size (both width and height) of an application icon that will be displayed in the app launcher and elsewhere. -- >
<dimen name="app_icon_size">48dip</dimen>
(3)接下來我們在launcher源碼中自己定義一個app_icon_size為72dip,編譯運行應用程式列表中的表徵圖果然變大了,但是擋住了下面的文字。
在layout/aplication_boxed.xml中把android:layout_height從88dip改成108dip。再次運行可以看到應用程式列表大表徵圖效果是不錯的。