Drag animation effects for custom GridLayout implementation entries

Source: Internet
Author: User

1, custom GridLayout implementation to increase the item excessive animation operation

 Public classMygridlayoutextendsGridLayout { PublicMygridlayout (Context context) {//super (context);             This(Context,NULL); }         PublicMygridlayout (Context context, AttributeSet attrs) {//Super (context, attrs);             This(context,attrs,-1); }         PublicMygridlayout (context context, AttributeSet attrs,intdefstyleattr) {            Super(context, attrs, defstyleattr); //set an entry in GridLayout to increase the transition animation ***********************             This. Setlayouttransition (Newlayouttransition ()); }    }

2. Create an entry

Private voidAddtextview (String string) {TextView TextView=NewTextView (GetContext ());        Textview.settext (string);        Textview.settextcolor (Color.Black);        Textview.setbackgroundresource (R.DRAWABLE.SHAPE_SELECITEM_BLACK_BG);        Textview.setgravity (Gravity.center); Textview.setpadding (15, 5, 15, 5); Gridlayout.layoutparams params=NewLayoutparams (); Params.setmargins (5, 5, 5, 5); //set the params to TextView and use the margins effecttextview.setlayoutparams (params); //add TextView to Gridlayout show         This. AddView (TextView); }

3, the implementation of drag-and-drop effect

3.1 Setting long-press events for entries

  

 Public void Addtextview (String string) {            ....             // set TextView long-press events for drag-and-drop effects             Textview.setonlongclicklistener (longclicklistener);             // add TextView to Gridlayout show             This . AddView (TextView);        }

3.2 Long Press to achieve drag effect in event

  

PrivateOnlongclicklistener Longclicklistener =NewOnlongclicklistener () {//Long Press the method called by the control//parameters: Controls that are long pressed@Override Public BooleanOnlongclick (View v) {//set the control to start dragging//parameter 1: Dragging the data to be displayed, general null//parameter 2: Drag and drop the displayed shadow style, Dragshadowbuilder (v): Sets the style effect of the drop shadow according to the dragged control//Parameter 3: The state of the dragged control, general null//Parameter 4: Labeling of other settings for dragging, General 0V.startdrag (NULL,NewDragshadowbuilder (v),NULL, 0); //returns true to handle long-press events, false is not handled            return true; }        };

4. The implementation of drag-and-drop mobile operation

Core idea: Create a rectangle according to TextView, the rectangle contains the coordinates of the upper-left and lower-right corner of the original TextView, when moving, judge whether the coordinates of the press are within the coordinates of the rectangle, if it moves to the corresponding rectangle position, that is, the corresponding TextView position

/**Drag-and- drop monitoring operation **/    PrivateOndraglistener Draglistener =NewOndraglistener () {//the method to invoke when the drag-and-drop operation is executed//parameter 1: dragging a control//parameter 2: Dragged events@Override Public BooleanOndrag (View V, dragevent event) {Switch(Event.getaction ()) { CaseDragevent.action_drag_started://Start draggingSystem.out.println ("Start dragging"); //Create a rectangle based on TextViewCreaterect ();  Break;  CaseDragevent.action_drag_entered://after you start dragging the control, enter the drag control scope eventSystem.out.println ("Enter drag control range");  Break;  CaseDragevent.action_drag_exited://after you start dragging the control, leave the drag control scope eventSystem.out.println ("Leave Drag Control range");  Break;  CaseDragevent.action_drag_location://moves the control or the action that is taken by the drag control after the control has been draggedSYSTEM.OUT.PRINTLN ("Drag Control Move"); //gets the position where the dragged control moves, depending on the drag-and-drop coordinates .                    intindex =GetIndex (event); //MyGridLayout.this.getChildAt (index)! = CurrentView: Determines whether the TextView moved to the position and the dragged TextView are consistent, do not move uniformly, and move without inconsistency                    if(Index! =-1 && currentview! =NULL&& Mygridlayout. This. Getchildat (index)! =CurrentView) {                        //implementation of Gridlayout mobile operations//Delete the dragged feature of the original position TviewMygridlayout. This. Removeview (CurrentView); //Add the dragged TextView to the moving positionMygridlayout. This. AddView (CurrentView, index);//Add a View object to that location                    }                 Break;  CaseDragevent.action_drag_ended://End DragSystem.out.println ("Pull Over"); //set drag TextView background to black border background                    if(CurrentView! =NULL) {currentview.setenabled (true); }                 Break;  CaseDragevent.action_drop://end drag, within the scope of the control, release the finger, do not operate outside the scope of the controlSystem.out.println ("Loosen Your fingers");  Break; }            return true;    }    }; /*** Create a rectangle based on TextView * * December 30, 2016 morning 11:44:08*/    protected voidCreaterect () {//Getchildcount (): Gets the number of child controls for GridlayoutRects =Newrect[ This. Getchildcount ()]; //create a corresponding number of rectangles based on the number of children         for(inti = 0; I < This. Getchildcount (); i++) {        //gets the View object of the child control, based on the index of the child controlView view = This. Getchildat (i); //Create a rectangle//parameter: x and y coordinates of the upper-left corner//parameter 3,4: coordinates of x and y in the lower right cornerRect rect =NewRect (View.getleft (), View.gettop (), View.getright (), View.getbottom ()); //save to line to arrayRects[i] =rect; }    }    /*** To get the position of the drag control according to the drag-and-drop coordinates * * December 30, 2016 morning 11:50:20*/    protected intGetIndex (Dragevent event) { for(inti = 0; i < rects.length; i++) {            //determines whether the pressed coordinates contain the contents of the coordinate range of the rectangle            if(Rects[i].contains (int) Event.getx (), (int) (Event.gety () )) {returni; }        }        return-1; }

Drag animation effects for custom GridLayout implementation entries

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.