Six ways to move Android view

Source: Internet
Author: User
Tags gety

If you pass the coordinates of the target position to layout (), the position of the view will be re-arranged, visually a sliding effect of the view.

Layout

PublicClass DragviewExtends view{Privateint lastx;Privateint lasty;Public Dragview(context context, AttributeSet attrs) {Super (context, attrs); }Public Boolean Ontouchevent(Motionevent event) {Gets the horizontal and vertical coordinates of the fingerint x = (int) event.GetX ();int y = (int) event.GetY ();Switch (event.Getaction ()) {case motionevent. action_down:lastx = x; lasty = y; break; case motionevent.//Calculate moving distance  int offx = X-LASTX; int offy = y-lasty; //call the layout method to reposition its location layout (gettop () +offy, getright () +offx, getbottom () + Offy); break;} return true;}}    

Layoutparams
Public Boolean Ontouchevent(Motionevent event) {Gets the horizontal and vertical coordinates of the fingerint x = (int) event.GetX ();int y = (int) event.GetY ();Switch (event.Getaction ()) {Case Motionevent.action_down:lastx = x; lasty = y; break; case motionevent.//Calculate moving distance  int offx = X-LASTX; int offy = y-lasty; ViewGroup. marginlayoutparams MLP = (marginlayoutparams) getlayoutparams (); Mlp.getleft () +offx; Mlp.gettop () +offy; setlayoutparams (MLP); break;} return true;}    
ScrollTo () Scrollby ()

Sceollto (x, y) incoming should be the end coordinate of the move

Scrollby (Dx,dy) is the increment of the move that is passed in.

The value passed in by Scrollby should be the opposite number of the increment you need!

Public Boolean Ontouchevent(Motionevent event) {Gets the horizontal and vertical coordinates of the fingerint x = (int) event.GetX ();int y = (int) event.GetY (); switch (event.  Getaction ()) {case motionevent.  ACTION_DOWN:LASTX = x; Lasty = y; Break ; Case Motionevent. action_move: //Calculate the distance of the move int offx = X-LASTX; int offy = y-lasty; (View) getParent ()). Scrollby (-offx,-offy); Break ;} return true;}               
Scroller

Step One:

? Initializes the Scroller object, which is Mscroller = new Scroller (context)

Step Two:

? Override the Computescroll () method to achieve a simulated swipe. You can copy the following last template code:

public  void computeScroll () {super.if (Mscroller.getparent ()). getcurry ()); } invalidate (); //must be called}           

Step Three:

Open the simulation process and startscroll the method in the right place (usually in move). It has two overloaded methods as follows:

startScroll(int startX,int startY, int dx,int dy,int duration)

startScroll(int startX,int startY,int dx,int dy)

It should be stated that:

    1. The Computescrolloffset method is used to determine if the entire slide has been completed and returns true, otherwise the slide is completed.
    2. Getcurry () and Getcurrx () get the current sliding coordinates.
    3. Finally, you have to use the Invalidate method to refresh. Because the Computescroll method is not called automatically, it is called in the draw method. So you have to use invalidate refresh to call the Draw method, and you will naturally call the Computescroll method. This will make the loop call.
    4. In Startscroll, the offset is the same as using the offset usage in the Scrollby method, which means that you must also fill in the opposite number of the distance you actually want to move. That is, you actually want it to offset a positive value, fill in the corresponding negative value here, if you want to offset a negative value, here fill in the corresponding positive value!
PublicClass DragviewExtends view{Privateint lastx;Privateint lasty;Private Scroller Mscroller;Public Dragview(context context, AttributeSet attrs) {Super (context, attrs); Mscroller =NewScroller (context); }Public Boolean Ontouchevent(Motionevent event) {Gets the horizontal and vertical coordinates of the fingerint x = (int) event.GetX ();int y = (int) event.GetY ();Switch (event.Getaction ()) {Case Motionevent.ACTION_DOWN:LASTX = x; Lasty = y;BreakCase Motionevent.Action_move:Calculate the distance to moveint offx = X-LASTX;int offy = Y-lasty; View ViewGroup = (view)GetParent (); (View)GetParent ()).Scrollby (-offx,-offy);BreakCase Motionevent.Action_up:view ViewGroup = (View)GetParent ();Turn on the slide and let it go back to the Origin mscroller.Startscroll (ViewGroup.GETSCROLLX (), ViewGroup.Getscrolly (),-viewgroup.GETSCROLLX (),-viewgroup.Getscrolly ());Break }Returntrue;} public span class= "DT" >void computescroll  () {super.if (Mscroller.getparent ()). getcurry ()); } invalidate (); //must be called}}           


Offsetleftandright () Offsettopandbottom ()

In fact, these two methods are on the left and right move and move up and down the package, the incoming is the offset.

Public Boolean Ontouchevent(Motionevent event) {Gets the horizontal and vertical coordinates of the fingerint x = (int) event.GetX ();int y = (int) event.GetY (); switch (event.  Getaction ()) {case motionevent.  ACTION_DOWN:LASTX = x; Lasty = y; Break ; Case Motionevent. action_move: //Calculate the distance of the move int offx = X-LASTX; int offy = y-lasty; offsetleftandright (OFFX); Offsettopandbottom (Offy); Break ;} return true;}               


Detailed: "Android--Scroller" http://www.cnblogs.com/yydcdut/p/4472340.html

Viewdraghelper

Viewdraghelper can read this article: "Android-Viewdraghelper" http://www.cnblogs.com/yydcdut/p/4945052.html

I'm the dividing line of the king of the Land Tiger.

Reference: http://www.cnblogs.com/fuly550871915/p/4985053.html

Six ways to move Android view

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.