Android 美化文件夹(一)

来源:互联网 发布:ssd win10自动优化 编辑:程序博客网 时间:2024/05/16 12:49

转自:http://dev.10086.cn/cmdn/wiki/index.php?doc-view-7178.html

Android原生自带的桌面文件夹样式及其简单,没有iPhone那种可以显示文件夹内文件图标缩略图的功能,今天我们来简单的实现一个。


从launcher源码中很容易变可以看出需要修改的文件,主要修改FolderIcon.Java这个文件。修改后的代码如下:

java代码:


  1. public class FolderIcon extends BubbleTextView implements DropTarget {   
  2.   
  3. private UserFolderInfo mInfo;   
  4. private Launcher mLauncher;   
  5. private Drawable mCloseIcon;   
  6. private Drawable mOpenIcon;   
  7.   
  8. // add by hmg for FolderIcon {   
  9. private IconCache mIconCache;   
  10. private static final int ICON_COUNT = 4//可显示的缩略图数   
  11. private static final int NUM_COL = 2// 每行显示的个数   
  12. private static final int PADDING = 1//内边距   
  13. private static final int MARGIN = 7//外边距   
  14. // add by hmg for FolderIcon }   
  15.   
  16. public FolderIcon(Context context, AttributeSet attrs) {   
  17. super(context, attrs);   
  18. mIconCache = ((LauncherApplication) mContext.getApplicationContext()).getIconCache();  
  19. }   
  20.   
  21. public FolderIcon(Context context) {   
  22. super(context);   
  23. mIconCache = ((LauncherApplication) mContext.getApplicationContext()).getIconCache();   
  24. }   
  25.   
  26. static FolderIcon fromXml(int resId, Launcher launcher, ViewGroup group,   
  27. UserFolderInfo folderInfo) {   
  28. FolderIcon icon = (FolderIcon) LayoutInflater.from(launcher).inflate( resId, group, false);   
  29.   
  30.   
  31. // final Resources resources = launcher.getResources();   
  32. // Drawable d = resources.getDrawable(R.drawable.ic_launcher_folder);   
  33. // icon.mCloseIcon = d;   
  34. // icon.mOpenIcon =   
  35. // resources.getDrawable(R.drawable.ic_launcher_folder_open);   
  36. // icon.setCompoundDrawablesWithIntrinsicBounds(null, d, null, null);   
  37.   
  38. icon.setText(folderInfo.title);   
  39. icon.setTag(folderInfo);   
  40. icon.setOnClickListener(launcher);   
  41. icon.mInfo = folderInfo;   
  42. icon.mLauncher = launcher;   
  43.   
  44. icon.updateFolderIcon(); //更新图标   
  45. folderInfo.setFolderIcon(icon); //设置FolderIcon   
  46. return icon;   
  47. }   
  48.   
  49. // add by hmg25 for FolderIcon {   
  50.   
  51. /**  
  52. * Author : hmg25 Version: 1.0 Description : 更新FolderIcon显示的文件缩略图  
  53. */   
  54.   
  55. public void updateFolderIcon() {   
  56. float x, y;   
  57. final Resources resources = mLauncher.getResources();   
  58. Bitmap closebmp = BitmapFactory.decodeResource(resources,   
  59. R.drawable.icon_folder); //获取FolderIcon关闭时的背景图   
  60. Bitmap openbmp = BitmapFactory.decodeResource(resources,   
  61. R.drawable.icon_folder_open); //获取FolderIcon打开时的背景图   
  62.   
  63. int iconWidth = closebmp.getWidth(); //icon的宽度   
  64. int iconHeight = closebmp.getHeight();   
  65. Bitmap folderclose = Bitmap.createBitmap(iconWidth, iconHeight,   
  66. Bitmap.Config.ARGB_8888);   
  67. Bitmap folderopen = Bitmap.createBitmap(iconWidth, iconHeight,   
  68. Bitmap.Config.ARGB_8888);   
  69. Canvas canvas = new Canvas(folderclose);   
  70. canvas.drawBitmap(closebmp, 00null); //绘制背景   
  71. Matrix matrix = new Matrix(); // 创建操作图片用的Matrix对象   
  72. float scaleWidth = (iconWidth - MARGIN * 2) / NUM_COL - 2 * PADDING;   
  73. //计算缩略图的宽(高与宽相同)   
  74.   
  75. float scale = (scaleWidth / iconWidth); // 计算缩放比例   
  76. matrix.postScale(scale, scale); // 设置缩放比例   
  77.   
  78. for (int i = 0; i < ICON_COUNT; i++) {   
  79. if (i < mInfo.contents.size()) {   
  80. x = MARGIN + PADDING * (2 * (i % NUM_COL) + 1) + scaleWidth* (i % NUM_COL);   
  81.   
  82. y = MARGIN + PADDING * (2 * (i / NUM_COL) + 1) + scaleWidth* (i / NUM_COL);   
  83.   
  84. ShortcutInfo scInfo = (ShortcutInfo) mInfo.contents.get(i);   
  85. Bitmap iconbmp = scInfo.getIcon(mIconCache); //获取缩略图标   
  86. Bitmap scalebmp = Bitmap.createBitmap(iconbmp, 00, iconWidth,   
  87. iconHeight, matrix, true);   
  88. canvas.drawBitmap(scalebmp, x, y, null);   
  89.   
  90. }   
  91.   
  92. }   
  93.   
  94. mCloseIcon = new FastBitmapDrawable(folderclose); //将bitmap转换为Drawable   
  95. setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, nullnull);   
  96. canvas = new Canvas(folderopen);   
  97. canvas.drawBitmap(folderclose, 00null);   
  98. canvas.drawBitmap(openbmp, 00null);   
  99. mOpenIcon = new FastBitmapDrawable(folderopen); //绘制open图片   
  100. }   
  101.   
  102. // add by hmg25 for FolderIcon }   
  103. public boolean acceptDrop(DragSource source, int x, int y, int xOffset,   
  104. int yOffset, DragView dragView, Object dragInfo) {   
  105. final ItemInfo item = (ItemInfo) dragInfo;   
  106. final int itemType = item.itemType;   
  107. return (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION || itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT) && item.container != mInfo.id;   
  108. }   
  109.   
  110. public Rect estimateDropLocation(DragSource source, int x, int y,   
  111. int xOffset, int yOffset, DragView dragView, Object dragInfo, Rect recycle) {   
  112. return null;   
  113. }   
  114.   
  115. public void onDrop(DragSource source, int x, int y, int xOffset,   
  116. int yOffset, DragView dragView, Object dragInfo) {   
  117. ShortcutInfo item;   
  118. if (dragInfo instanceof ApplicationInfo) {   
  119.   
  120. // Came from all apps -- make a copy   
  121. item = ((ApplicationInfo) dragInfo).makeShortcut();   
  122. else {   
  123. item = (ShortcutInfo) dragInfo;   
  124. }   
  125.   
  126. mInfo.add(item);   
  127. LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 00,0);   
  128. updateFolderIcon(); //拖拽放入时更新   
  129. }   
  130.   
  131. public void onDragEnter(DragSource source, int x, int y, int xOffset,   
  132. int yOffset, DragView dragView, Object dragInfo) {   
  133. setCompoundDrawablesWithIntrinsicBounds(null, mOpenIcon, nullnull);   
  134. }   
  135.   
  136. public void onDragOver(DragSource source, int x, int y, int xOffset,   
  137. int yOffset, DragView dragView, Object dragInfo) {   
  138. }   
  139.   
  140. public void onDragExit(DragSource source, int x, int y, int xOffset,   
  141. int yOffset, DragView dragView, Object dragInfo) {   
  142. setCompoundDrawablesWithIntrinsicBounds(null, mCloseIcon, nullnull);   
  143. }   
  144.   


原创粉丝点击