Android Desktop Folder beautification

Source: Internet
Author: User

By he minggui (http://blog.csdn.net/hmg25) reprint please indicate the source

 

Haha, I haven't written a blog for a long time. It's so miserable in the blog ~~ People become lazy ......

Android native Desktop Folder style and its simplicity, without the iphone that can display the file icon thumbnails in the folder, today we will implement a simple.

The effect is as follows:

 

From the launcher source code, you can easily see the file to be modified, mainly modifying the FolderIcon. java file. The modified code is as follows:

Public class foldericon extends bubbletextview implements droptarget {<br/> private userfolderinfo minfo; <br/> private launcher mlauncher; <br/> private drawable mcloseicon; <br/> private drawable mopenicon; <br/> // Add by HMG for foldericon {<br/> private iconcache miconcache; <br/> Private Static final int icon_count = 4; // number of visible thumbnails <br/> Private Static final int num_col = 2; // The number displayed per line <br/> private Static final int padding = 1; // padding <br/> Private Static final int margin = 7; // margin <br/> // Add by HMG for foldericon} <br/> Public foldericon (context, attributeset attrs) {<br/> super (context, attrs); <br/> miconcache = (launcherapplication) mcontext. getapplicationcontext () <br/>. geticoncache (); <br/>}< br/> Public foldericon (context) {<br/> super (context); <br/> miconcache = (Lau Ncherapplication) mcontext. getapplicationcontext () <br/>. geticoncache (); <br/>}< br/> static foldericon fromxml (INT resid, launcher, viewgroup group, <br/> userfolderinfo folderinfo) {<br/> foldericon icon = (foldericon) layoutinflater. from (launcher ). inflate (<br/> resid, group, false); <br/> // final resources Resources = launcher. getresources (); <br/> // drawable d = resources. getdrawable (R. Drawable. ic_launcher_folder); <br/> // icon. mcloseicon = D; <br/> // icon. mopenicon = <br/> // resources. getdrawable (R. drawable. ic_launcher_folder_open); <br/> // icon. setcompounddrawableswithintrinsicbounds (null, D, null, null); <br/> icon. settext (folderinfo. title); <br/> icon. settag (folderinfo); <br/> icon. setonclicklistener (launcher); <br/> icon. minfo = folderinfo; <br/> icon. mlauncher = launcher; </P> <P> icon. updatefoldericon (); // update the icon <br/> folderinfo. setfoldericon (icon); // set foldericon <br/> return icon; <br/>}< br/> // Add By hm25 for foldericon {<br/>/** <br/> * Author: hm25 version: 1.0 description: update the file thumbnails displayed by foldericon <br/> */<br/> Public void updatefoldericon () {<br/> float X, Y; <br/> final resources Resources = mlauncher. getresources (); <br/> bitmap closebmp = bitmapfactory. decodereso Urce (resources, <br/> r. drawable. icon_folder); // obtain the background image when foldericon is disabled <br/> bitmap openbmp = bitmapfactory. decoderesource (resources, <br/> r. drawable. icon_folder_open); // obtain the background image when foldericon is opened </P> <p> int iconwidth = closebmp. getwidth (); // icon width <br/> int iconheight = closebmp. getheight (); <br/> bitmap folderclose = bitmap. createbitmap (iconwidth, iconheight, <br/> bitmap. config. argb_8888); <br/> bitmap fold Eropen = bitmap. createbitmap (iconwidth, iconheight, <br/> bitmap. config. argb_8888); <br/> canvas = new canvas (folderclose); <br/> canvas. drawbitmap (closebmp, 0, 0, null); // draw the background <br/> matrix = new matrix (); // create a matrix object for image Operations <br/> float scalewidth = (iconwidth-margin * 2)/num_col-2 * padding; // calculate the width of the thumbnail (the same as the width) <br/> float scale = (scalewidth/iconwidth); // calculate the zoom ratio <br/> matrix. posts Cale (scale, scale); // set the scale ratio <br/> for (INT I = 0; I <icon_count; I ++) {<br/> if (I <minfo. contents. size () {<br/> X = margin + padding * (2 * (I % num_col) + 1) + scalewidth <br/> * (I % num_col ); <br/> Y = margin + padding * (2 * (I/num_col) + 1) + scalewidth <br/> * (I/num_col ); <br/> shortcutinfo scinfo = (shortcutinfo) minfo. contents. get (I); <br/> bitmap iconbmp = scinfo. getIcon (miconcache ); // Obtain the thumbnail <br/> bitmap scalebmp = bitmap. createbitmap (iconbmp, 0, 0, iconwidth, <br/> iconheight, matrix, true); <br/> canvas. drawbitmap (scalebmp, X, Y, null); <br/>}< br/> mcloseicon = new fastbitmapdrawable (folderclose ); // convert bitmap to drawable <br/> setcompounddrawableswithintrinsicbounds (null, mcloseicon, null, null); <br/> canvas = new canvas (folderopen); <br/> canvas. drawbitmap (folderclose, 0, 0, null); <br/> canvas. drawbitmap (openbmp, 0, 0, null); <br/> mopenicon = new fastbitmapdrawable (folderopen ); // draw an open image <br/>}< br/> // Add By hm25 for foldericon} <br/> Public Boolean acceptdrop (dragsource source, int X, int y, int xoffset, <br/> int yoffset, dragview, object draginfo) {<br/> final iteminfo item = (iteminfo) draginfo; <br/> final int itemtype = item. itemtype; <br/> return (I Temtype = launchersettings. favorites. item_type_application | itemtype = launchersettings. favorites. item_type_shortcut) <br/> & item. container! = Minfo. ID; <br/>}< br/> Public rect estimatedroplocation (dragsource source, int X, int y, <br/> int xoffset, int yoffset, dragview, object draginfo, <br/> rect recycle) {<br/> return NULL; <br/>}< br/> Public void ondrop (dragsource source, int X, int y, int xoffset, <br/> int yoffset, dragview, object draginfo) {<br/> shortcutinfo item; <br/> If (draginfo instanceof applicationinfo) {<br/> // came from all apps -- make a copy <br/> item = (applicationinfo) draginfo ). makeshortcut (); <br/>}else {<br/> item = (shortcutinfo) draginfo; <br/>}< br/> minfo. add (item); <br/> launchermodel. addormoveitemindatabase (mlauncher, item, minfo. ID, 0, 0, <br/> 0); <br/> updatefoldericon (); // update upon dragging <br/>}< br/> Public void ondragenter (dragsource source, int X, int y, int xoffset, <br/> int yoffset, dragview, object draginfo) {<br/> setcompounddrawableswithintrinsicbounds (null, mopenicon, null, null); <br/>}< br/> Public void ondragover (dragsource source, int X, int y, int xoffset, <br/> int yoffset, dragview, object draginfo) {<br/>}< br/> Public void ondragexit (dragsource source, int X, int y, int xoffset, <br/> int yoffset, dragview, object draginfo) {<br/> setcompounddrawableswithintrinsicbounds (null, mcloseicon, null, null ); <br/>}< br/> 

 

When you drag a file into a folder, the system returns the onDrop in FolderIcon. Therefore, updateFolderIcon () is added ();

The code above allows you to update the thumbnail display in real time when you drag an icon into a folder. You have not updated the display when you drag the icon out of the folder. Therefore, you need to modify the display in other places. The tracking code shows that the onDropCompleted method in UserFolder is returned when the folder is dragged and left. You need to modify UserFolder. java:

Public void onDropCompleted (View target, boolean success) {<br/> if (success) {<br/> ShortcutsAdapter adapter = (ShortcutsAdapter) mContent. getAdapter (); <br/> adapter. remove (mDragItem); <br/> (UserFolderInfo) mInfo ). mFolderIcon. updateFolderIcon (); // add by hm25 update when dragging and leaving <br/>}< br/> public void onDrop (DragSource source, int x, int y, int xOffset, int yOffset, <br/> DragView dragView, Object dragInfo) {<br/> ShortcutInfo item; <br/> if (dragInfo instanceof ApplicationInfo) {<br/> // Came from all apps -- make a copy <br/> item = (ApplicationInfo) dragInfo ). makeShortcut (); <br/>}else {<br/> item = (ShortcutInfo) dragInfo; <br/>}< br/> (ShortcutsAdapter) mContent. getAdapter ()). add (item); <br/> LauncherModel. addOrMoveItemInDatabase (mLauncher, item, mInfo. id, 0, 0, 0); <br/> (UserFolderInfo) mInfo ). mFolderIcon. updateFolderIcon (); // add by hm25 drag and drop the file directly to the opened folder for update <br/>}< br/> 

 

From the code above, we can see that in order to pass the FolderIcon object, we also need to add a mFolderIcon member for UserFolderInfo and modify UserFolderInfo. java:

 Protected FolderIcon mFolderIcon = null; // add by hm25 for Folder </p> <p> // add by hm25 for Folder {<br/> void setFolderIcon (FolderIcon icon) <br/>{< br/> mFolderIcon = icon; <br/>}< br/> // add by hm25 for Folder} <br/> 

 

The above code is in android2.2,
480*320 testing, other resolutions can be modified

Private static final int ICON_COUNT = 4; // number of visible thumbnails

Private
Static final int NUM_COL = 2; // number of entries displayed in each row

Private
Static final int PADDING = 1; // PADDING

Private
Static final int MARGIN = 7; // MARGIN

.

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.