android launcher開發之表徵圖背景以及預設配置
1:然後我自己看了一下案頭表徵圖的載入過程:
案頭第一次載入時是預設讀取一個xml設定檔,完成配置工作。這個設定檔在Launcher目錄下,
路徑是:\Launcher\res\xml\default_workspace.xml 。這個XML檔案就是剛升級,Launcher第
一次顯示的時候,會讀取的設定檔。default_workspace。xml裡面可以配置APP捷徑、Widget、Search搜尋欄等
launcher裡面負責解析default_workspace.xml檔案的方法是 LauncherProvider.java裡面的loadFavorites方法
LauncherProvider.java裡面有loadFavorites()這個方法:
private int loadFavorites(SQLiteDatabase db, int workspaceResourceId) {
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
ContentValues values = new ContentValues();
if (LOGD) Log.v(TAG, String.format("Loading favorites from resid=0x%08x", workspaceResourceId));
PackageManager packageManager = mContext.getPackageManager();
int i = 0;
try {
XmlResourceParser parser = mContext.getResources().getXml(workspaceResourceId);
AttributeSet attrs = Xml.asAttributeSet(parser);
beginDocument(parser, TAG_FAVORITES);
final int depth = parser.getDepth();
final HashMap occupied = new HashMap();
LauncherModel model = LauncherAppState.getInstance().getModel();
。
。
。
。
return i;
}
其實就是一個分析XML和寫入資料庫的過程,LauncherProvider.java是整個Launcher的資料來源,
知道這些表徵圖如何載入出來之後對做螢幕是壞 修改背景大小 行列等都有好處。
2:Launcher 表徵圖加入預設背景。是主題功能的一個小部分也是不可缺少的一部分,下面是我今天整理的一些邏輯和代碼,Launcher表徵圖的擷取處理是在Utilities.java類裡面,
我們可以從裡面找到Bitmap createIconBitmap(Drawable icon, Context context) 方法。這個方法就是返回應用表徵圖的。
static Bitmap createIconBitmap(Bitmap icon, Context context) {
int textureWidth = sIconTextureWidth;
int textureHeight = sIconTextureHeight;
int sourceWidth = icon.getWidth();
int sourceHeight = icon.getHeight();
if (sourceWidth > textureWidth && sourceHeight > textureHeight) {
// Icon is bigger than it should be; clip it (solves the GB->ICS migration case)
return Bitmap.createBitmap(icon,
(sourceWidth - textureWidth) / 2,
(sourceHeight - textureHeight) / 2,
textureWidth, textureHeight);
} else if (sourceWidth == textureWidth && sourceHeight == textureHeight) {
// Icon is the right size, no need to change it
return icon;
} else {
// Icon is too small, render to a larger bitmap
final Resources resources = context.getResources();
return createIconBitmap(new BitmapDrawable(resources, icon), context);
}
}
可以在這裡修改表徵圖的背景 可以加入
if (tru{
Bitmap backBitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.apical_icon_bg);
int backWidth = backBitmap.getWidth();
int backHeight = backBitmap.getHeight();
if(backWidth != sIconWidth || backHeight != sIconHeight)
{
Matrix matrix = new Matrix();
matrix.postScale((float)sIconWidth/backWidth, (float)sIconHeight/backHeight);
canvas.drawBitmap(Bitmap.createBitmap(backBitmap, 0, 0, backWidth, backHeight, matrix, true),
.0f, 0.0f, null);
}else
{
canvas.drawBitmap(backBitmap, 0.0f, 0.0f, null);
}
}
直接指定一個圖片為表徵圖背景 R.drawable.apical_icon_bg;