Story Behind Android desktop (launcher application) (6)-study the accessories implemented by launcher (listview that can be dragged)

Source: Internet
Author: User
Tags gety

 Blog migration-I have migrated my blog to www.ijavaboy.com to better manage it. We are sorry for the inconvenience caused by no updates! New address of this article: Click me

In this article, we will write how launcher achieves the drag and drop of items on the desktop in Android. After studying its mechanism, the brain suddenly gets angry and wants to implement a drag-and-drop listview, after understanding the drag and drop of items in launcher, implementing the drag-and-drop listview is simply a piece of cake. Therefore, before dragging the article in launcher, it can be used as a transitional understanding. However, there are some differences here, that is, after the item is dragged on the launcher, it can be placed in an empty position, after an item in the listview is dragged, You need to swap the item in the new position with the item in the original position. Below, we will not talk nonsense anymore. Go to the Code directly. For details, see the notes:

Public class draglistview extends listview {Private Static final string tag = "draglistview"; Private Static final int invalid_position =-1; private bitmap mdragbitmap; private view mdragview; private uorderdeletezone zone; private view footer; private object srccontent; private int startposition; private paint mpaint = new paint (); Private float mlastmotionx; private float mlastmotiony; private float mtoucho Ffsetx; private float mtouchoffsety; private float mbitmapoffsetx; private float mbitmapoffsety; private Boolean mdragging = false; private rect mdragrect = new rect (); // when dragging an item, record the area that the drag goes through. // It indicates that when sliding, the movement of the hand must be greater than this distance before moving the control. Private int mscaledtouchslop; private float mtopscrollbound; // when sliding up beyond this boundary, the above items scroll down private float mbottomscrollbound; // when sliding down beyond this boundary, the following items start to scroll up public draglistview (context) {This (context, null);} public draglistview (context, attributeset attrs) {This (context, attrs, 0);} public draglistview (context, attributeset attrs, int defstyle) {super (context, attrs, defstyle);/*** when When items are dragged, change their color. */FINAL int srccolor = context. getresources (). getcolor (R. color. drag_filter); mpaint. setcolorfilter (New porterducolorfilter (srccolor, porterduff. mode. src_atop); // It is a distance, indicating that when moving, the control starts to move more than this distance. Mscaledtouchslop = viewconfiguration. Get (context). getscaledtouchslop ();} protected void dispatchdraw (canvas) {super. dispatchdraw (canvas); If (mdragging & mdragbitmap! = NULL) {final int scrollx = getscrollx (); Final int scrolly = getscrolly (); float left = scrollx + mlastmotionx-mtouchoffsetx-mbitmapoffsetx; float Top = scrolly + mlastmotiony-mtouchoffsety-mbitmapoffsety; canvas. drawbitmap (mdragbitmap, left, top, mpaint) ;}}/*** in this method, determine the location where the current user presses the event. * If the location is within 30 dip on the right, drag */Public Boolean onintercepttouchevent (motionevent event) {int action = event. getaction (); Final Float x = event. getx (); Final float y = event. gety (); If (getwidth ()-x> 30) {// It is not in the drag and drop area. If it is returned directly, Return OK. Return super. onintercepttouchevent (event);} switch (Action) {Case motionevent. action_down: mlastmotionx = x; mlastmotiony = y; break; default: break;} log. E (TAG, "touchslop:" + mscaledtouchslop); // calculate the scroll boundary/*** scroll up to 1/3, however, if the item to be dragged is located in the position of the 1/3 screen *, take the current small one. of course, you can also set it to a 1/3 screen */mtopscrollbound = math. Min (Y-mscaledtouchslop, getheight ()/3); mbottomscrollbound = math. min (Y + mscaledtouchslop, getheight () * 2/3); // mtopscrollbound = getheight ()/3; // mbottomscrollbound = getheight () * 2/3; log. E (TAG, "bound scroll:" + mtopscrollbound + "," + mbottomscrollbound); // obtain the item where the coordinates of the current event are located. The drag in listview is up and down. Therefore, it has little to do with the X coordinate. Int position = This. pointtoposition (INT) x, (INT) y); If (position = invalid_position) {return Super. oninte Rcepttouchevent (event);} startposition = position;/*** get the currently dragged item. Note that not every item in listview is a view, this view is reused */mdragview = getchildat (position-getfirstvisibleposition (); srccontent = getitematposition (position); If (mdragview! = NULL) {mdragbitmap = createviewbitmap (mdragview); // when the item is dragged, delete the item in the original position, in fact, it is to delete the content of the position in the adapter/*** note that the adapter used for the listview is the arrayadapter, which has the remove (Object item) Method *, we often know that the remove (object) method of arraylist is implemented when processing the display data of listview. * but when we use arrayadatper, if our data is transmitted to the adapter using an array, * arrays is used by default in arrayadapter. the aslist (object []) method converts an array to a list object. In this way, when we call the * removeitem method of arraylist, Java will appear. lang. unsupportedoperationexc Eption exception. This is because * arrays. after aslist (object []) is converted, the list is arrays. arraylist object instead of Java. utils. arraylist * arrays. arraylist does not implement the Remove Method. Therefore, the default Remove Method of the parent class abstractlist is executed, while the Remove Method of * abstractlist throws an unsupportedoperationexception */removeitem (position ); mdragging = true;} return true;} @ suppresswarnings ("unchecked") Private void removeitem (INT position) {arrayadapter <Object> adapter = (arrayadapter <Object>) This. getadapter (); Adapter. remove (adapter. getitem (position);} @ suppresswarnings ("unchecked") Private void additem (INT position) {arrayadapter <Object> adapter = (arrayadapter <Object>) This. getadapter (); adapter. insert (string) srccontent, position);} public Boolean ontouchevent (motionevent event) {super. ontouchevent (event); If (! Mdragging) {return false;} final int action = event. getaction (); Final float x = event. getx (); Final float y = event. gety (); Switch (Action) {Case motionevent. action_down: // mlastmotionx = x; mlastmotiony = y; break; Case motionevent. action_move: Final int scrollx = getscrollx (); Final int scrolly = getscrolly (); int left = (INT) (scrollx + callback-mtouchoffsetx-mbitmapoffsetx); int Top = (INT) (scrolly + Mlastmotiony-mtouchoffsety-mbitmapoffsety); Final int width = mdragbitmap. getwidth (); Final int Height = mdragbitmap. getheight (); mdragrect. set (left-1, top-1, width + Left + 1, top + height + 1); log. E (TAG, "distance of each move:" + (Y-mlastmotiony); // mlastmotionx = x; mlastmotiony = y; left = (INT) (scrollx + mlastmotionx-mtouchoffsetx-mbitmapoffsetx); Top = (INT) (scrolly + mlastmotiony-mtouchoffsety-mbitmapoffs Ety); mdragrect. union (left-1, top-1, width + Left + 1, top + height + 1); invalidate (mdragrect); int scrollheight = 0; If (Y <mtopscrollbound) {/*** if the position of the current move to Y is above mtopscrollbound, obtain the item of the current position below, * and base the item on the current position, move the offset of 15 dip values down. In this way, when you scroll up to drag, * the item following the scroll down ** is dragged down. */scrollheight = 15 ;} else if (Y> mbottomscrollbound) {scrollheight =-15;} If (scrollheight! = 0) {// call the listview method to implement rolling int position = This. pointtoposition (INT) x, (INT) y); If (position! = Invalid_position) {/*** move the scrollheight unit from the current position item up or down */setselectionfromtop (Position, getchildat (position-getfirstvisibleposition ()). gettop () + scrollheight) ;}} break; Case motionevent. action_up: int position = This. pointtoposition (INT) x, (INT) y); If (position = invalid_position) {position = startposition;} float listtop = getchildat (0 ). gettop (); // This is the bottom of the listview visible area, but not necessarily the position of the last item in the dataset float list Bottom = getchildat (getchildcount ()-1 ). getbottom (); If (Y <listtop) {position = 0;} else if (Y> listbottom) {/*** when dragging down, listview slides down. * here, when an item is dragged down to the bottom, it is added to the last position in the dataset. * Here, getcount () is used instead of getcount ()-1. Is to insert a */position = getadapter () to the end (). getcount ();} stopdrag (position); break;} return true;} private void stopdrag (INT newposition) {mdragging = false; If (mdragbitmap! = NULL) {mdragbitmap. recycle ();} // Add the dragged item to the new position additem (newposition); invalidate ();} /*** use the Rendering Buffer of the current view to create a bitmap * and perform certain scaling * @ Param view * @ return */private bitmap createviewbitmap (view) {final int left = view. getleft (); Final int Top = view. gettop (); // the offset of the current hold position to the view. mtouchoffsetx = mlastmotionx-left; mtouchoffsety = mlastmotiony-top; view. clearfocus (); view. setpressed (false); Boolean willnotcache = view. willnotcachedrawing (); view. setwillnotcachedrawing (false); view. builddrawingcache (); Bitmap bitmap = view. getdrawingcache (); Final int width = bitmap. getwidth (); Final int Height = bitmap. getheight (); // use a simple scale to scale the matrix = new matrix (); float scalefactor = view. getheight (); scalefactor = (scalefactor + 40)/scalefactor; matrix. setscale (1.0f, scalefactor); bitmap newbitmap = bitmap. createbitmap (bitmap, 0, 0, width, height, matrix, true); view. destroydrawingcache (); view. setwillnotcachedrawing (willnotcache); // The scaled bitmap created and the paddingmbitmapoffsetx = (newbitmap. getwidth ()-width)/2; mbitmapoffsety = (newbitmap. getheight ()-height)/2; return newbitmap ;}}

 

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.