android桌面文件夹美化
?? ? ? ? ? ? ? ? ? ? ? ? ?By 何明桂(http://blog.csdn.net/hmg25) 轉(zhuǎn)載請(qǐng)注明出處?
?
?? ?哈哈,好久沒有寫博客拉,blog里邊好凄涼阿~~人也變懶了……
?? ?android原生自帶的桌面文件夾樣式及其簡(jiǎn)單,沒有iphone那種可以顯示文件夾內(nèi)文件圖標(biāo)縮略圖的功能,今天我們來簡(jiǎn)單的實(shí)現(xiàn)一個(gè)。
效果如下:
?
從launcher源碼中很容易變可以看出需要修改的文件,主要修改FolderIcon.java這個(gè)文件。修改后的代碼如下:
public class FolderIcon extends BubbleTextView implements DropTarget {private UserFolderInfo mInfo;private Launcher mLauncher;private Drawable mCloseIcon;private Drawable mOpenIcon;// add by hmg for FolderIcon {private IconCache mIconCache;private static final int ICON_COUNT = 4; //可顯示的縮略圖數(shù)private static final int NUM_COL = 2; // 每行顯示的個(gè)數(shù)private static final int PADDING = 1; //內(nèi)邊距private static final int MARGIN = 7; //外邊距// add by hmg for FolderIcon }public FolderIcon(Context context, AttributeSet attrs) {super(context, attrs);mIconCache = ((LauncherApplication) mContext.getApplicationContext()).getIconCache();}public FolderIcon(Context context) {super(context);mIconCache = ((LauncherApplication) mContext.getApplicationContext()).getIconCache();}static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,UserFolderInfo folderInfo) {FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate(resId, group, false);// final Resources resources = launcher.getResources();// Drawable d = resources.getDrawable(R.drawable.ic_launcher_folder);// icon.mCloseIcon = d;// icon.mOpenIcon =// resources.getDrawable(R.drawable.ic_launcher_folder_open);// icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);icon.setText(folderInfo.title);icon.setTag(folderInfo);icon.setOnClickListener(launcher);icon.mInfo = folderInfo;icon.mLauncher = launcher; icon.updateFolderIcon(); //更新圖標(biāo)folderInfo.setFolderIcon(icon); //設(shè)置FolderIconreturn icon;}// add by hmg25 for FolderIcon {/*** Author : hmg25 Version: 1.0 Description : 更新FolderIcon顯示的文件縮略圖*/public void updateFolderIcon() {float x, y;final Resources resources = mLauncher.getResources();Bitmap closebmp = BitmapFactory.decodeResource(resources,R.drawable.icon_folder); //獲取FolderIcon關(guān)閉時(shí)的背景圖Bitmap openbmp = BitmapFactory.decodeResource(resources,R.drawable.icon_folder_open); //獲取FolderIcon打開時(shí)的背景圖int iconWidth = closebmp.getWidth(); //icon的寬度int iconHeight = closebmp.getHeight();Bitmap folderclose = Bitmap.createBitmap(iconWidth, iconHeight,Bitmap.Config.ARGB_8888);Bitmap folderopen = Bitmap.createBitmap(iconWidth, iconHeight,Bitmap.Config.ARGB_8888);Canvas canvas = new Canvas(folderclose);canvas.drawBitmap(closebmp, 0, 0, null); //繪制背景Matrix matrix = new Matrix(); // 創(chuàng)建操作圖片用的Matrix對(duì)象float scaleWidth = (iconWidth - MARGIN * 2) / NUM_COL - 2 * PADDING; //計(jì)算縮略圖的寬(高與寬相同)float scale = (scaleWidth / iconWidth); // 計(jì)算縮放比例matrix.postScale(scale, scale); // 設(shè)置縮放比例for (int i = 0; i < ICON_COUNT; i++) {if (i < mInfo.contents.size()) {x = MARGIN + PADDING * (2 * (i % NUM_COL) + 1) + scaleWidth* (i % NUM_COL);y = MARGIN + PADDING * (2 * (i / NUM_COL) + 1) + scaleWidth* (i / NUM_COL);ShortcutInfo scInfo = (ShortcutInfo) mInfo.contents.get(i); Bitmap iconbmp = scInfo.getIcon(mIconCache); //獲取縮略圖標(biāo)Bitmap scalebmp = Bitmap.createBitmap(iconbmp, 0, 0, iconWidth,iconHeight, matrix, true);canvas.drawBitmap(scalebmp, x, y, null);}}mCloseIcon = new FastBitmapDrawable(folderclose); //將bitmap轉(zhuǎn)換為DrawablesetCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);canvas = new Canvas(folderopen);canvas.drawBitmap(folderclose, 0, 0, null);canvas.drawBitmap(openbmp, 0, 0, null);mOpenIcon = new FastBitmapDrawable(folderopen); //繪制open圖片}// add by hmg25 for FolderIcon }public boolean acceptDrop(DragSource source, int x, int y, int xOffset,int yOffset, DragView dragView, Object dragInfo) {final ItemInfo item = (ItemInfo) dragInfo;final int itemType = item.itemType;return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)&& item.container != mInfo.id;}public Rect estimateDropLocation(DragSource source, int x, int y,int xOffset, int yOffset, DragView dragView, Object dragInfo,Rect recycle) {return null;}public void onDrop(DragSource source, int x, int y, int xOffset,int yOffset, DragView dragView, Object dragInfo) {ShortcutInfo item;if (dragInfo instanceof ApplicationInfo) {// Came from all apps -- make a copyitem = ((ApplicationInfo) dragInfo).makeShortcut();} else {item = (ShortcutInfo) dragInfo;}mInfo.add(item);LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0,0);updateFolderIcon(); //拖拽放入時(shí)更新}public void onDragEnter(DragSource source, int x, int y, int xOffset,int yOffset, DragView dragView, Object dragInfo) {setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, null, null);}public void onDragOver(DragSource source, int x, int y, int xOffset,int yOffset, DragView dragView, Object dragInfo) {}public void onDragExit(DragSource source, int x, int y, int xOffset,int yOffset, DragView dragView, Object dragInfo) {setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, null, null);} } ?
?
將文件拖拽進(jìn)入文件夾時(shí)響應(yīng)FolderIcon中的onDrop,所以添加updateFolderIcon();
以上代碼可以實(shí)現(xiàn)將圖標(biāo)拖拽進(jìn)文件夾時(shí)實(shí)時(shí)更新縮略圖顯示,還沒有對(duì)拖拽出文件夾時(shí)更新顯示,所以還需要修改其他地方。跟蹤代碼可以看出拖拽離開文件夾時(shí)響應(yīng)UserFolder中方法onDropCompleted,需要修改UserFolder.java:
public void onDropCompleted(View target, boolean success) {if (success) {ShortcutsAdapter adapter = (ShortcutsAdapter)mContent.getAdapter();adapter.remove(mDragItem);((UserFolderInfo)mInfo).mFolderIcon.updateFolderIcon(); //add by hmg25 拖拽離開時(shí)更新}} public void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,DragView dragView, Object dragInfo) {ShortcutInfo item;if (dragInfo instanceof ApplicationInfo) {// Came from all apps -- make a copyitem = ((ApplicationInfo)dragInfo).makeShortcut();} else {item = (ShortcutInfo)dragInfo;}((ShortcutsAdapter)mContent.getAdapter()).add(item);LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);((UserFolderInfo)mInfo).mFolderIcon.updateFolderIcon(); //add by hmg25 將文件直接拖拽到打開的文件夾更新 } ?
?
從以上代碼可以看出為了傳遞FolderIcon對(duì)象,所以我們還需要為UserFolderInfo添加一個(gè)mFolderIcon成員,修改UserFolderInfo.java:
?protected FolderIcon mFolderIcon = null; //add by hmg25 for Folder//add by hmg25 for Folder {void setFolderIcon(FolderIcon icon){mFolderIcon=icon;}//add by hmg25 for Folder }?
?
以上代碼是在android2.2, 480*320下測(cè)試的,其他分辨率的可以修改
?? private static final int ICON_COUNT = 4;? //可顯示的縮略圖數(shù)
??? private static final int NUM_COL = 2;??? // 每行顯示的個(gè)數(shù)
??? private static final int PADDING = 1;??? //內(nèi)邊距
??? private static final int MARGIN = 7;???? //外邊距
的值。
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的android桌面文件夹美化的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Debezium系列之:增加心跳检测he
- 下一篇: 情人节选什么礼物?盘点最火爆几款礼物