zoukankan      html  css  js  c++  java
  • android 桌面文件夹ui美化

    http://blog.csdn.net/hmg25/article/details/6574575

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

    效果如下:

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

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

     

    将文件拖拽进入文件夹时响应FolderIcon中的onDrop,所以添加updateFolderIcon();

    以上代码可以实现将图标拖拽进文件夹时实时更新缩略图显示,还没有对拖拽出文件夹时更新显示,所以还需要修改其他地方。跟踪代码可以看出拖拽离开文件夹时响应UserFolder中方法onDropCompleted,需要修改UserFolder.java:

    1. public void onDropCompleted(View target, boolean success) {  
    2.        if (success) {  
    3.            ShortcutsAdapter adapter = (ShortcutsAdapter)mContent.getAdapter();  
    4.            adapter.remove(mDragItem);  
    5.           ((UserFolderInfo)mInfo).mFolderIcon.updateFolderIcon();  //add by hmg25 拖拽离开时更新  
    6.        }  
    7.    }  
    8. ublic void onDrop(DragSource source, int x, int y, int xOffset, int yOffset,  
    9.            DragView dragView, Object dragInfo) {  
    10.        ShortcutInfo item;  
    11.        if (dragInfo instanceof ApplicationInfo) {  
    12.            // Came from all apps -- make a copy  
    13.            item = ((ApplicationInfo)dragInfo).makeShortcut();  
    14.        } else {  
    15.            item = (ShortcutInfo)dragInfo;  
    16.        }  
    17.        ((ShortcutsAdapter)mContent.getAdapter()).add(item);  
    18.        LauncherModel.addOrMoveItemInDatabase(mLauncher, item, mInfo.id, 0, 0, 0);  
    19.        ((UserFolderInfo)mInfo).mFolderIcon.updateFolderIcon(); //add by hmg25  将文件直接拖拽到打开的文件夹更新         

     

    从以上代码可以看出为了传递FolderIcon对象,所以我们还需要为UserFolderInfo添加一个mFolderIcon成员,修改UserFolderInfo.java:

    1. protected FolderIcon mFolderIcon = null; //add by hmg25 for Folder  
    2.    
    3.  //add by hmg25 for Folder {  
    4.     void setFolderIcon(FolderIcon icon)  
    5.     {  
    6.     mFolderIcon=icon;  
    7.     }  
    8.  //add by hmg25 for Folder }  
    9.    

     

    以上代码是在android2.2, 480*320下测试的,其他分辨率的可以修改

       private static final int ICON_COUNT = 4;  //可显示的缩略图数

        private static final int NUM_COL = 2;    // 每行显示的个数

        private static final int PADDING = 1;    //内边距

        private static final int MARGIN = 7;     //外边距

    的值。

  • 相关阅读:
    喜得千金
    ASP.NET MVC +EasyUI 权限设计(四)角色动作
    SQL Server 2008 远程过程调用失败
    ASP.NET MVC +EasyUI 权限设计(三)基础模块
    ASP.NET MVC +EasyUI 权限设计(二)环境搭建
    ASP.NET MVC +EasyUI 权限设计(一)开篇
    Entity Framework学习笔记(六)----使用Lambda查询Entity Framework(1)
    暂停更新Blog
    Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载
    Exp9 Web安全基础
  • 原文地址:https://www.cnblogs.com/greywolf/p/2831236.html
Copyright © 2011-2022 走看看