In Android development, you often encounter a view that requires it to support sliding requirements. Here are six ways to introduce the Android view move through this article.
Layout ()
If you pass the coordinates of the sliding target position to layout (), this will rearrange the position of the view, which is visually a sliding effect of view.
public class Dragview extends view{
private int lastx;
private int lasty;
Public Dragview (context, AttributeSet attrs) {
Super (context, attrs);
}
public boolean ontouchevent (Motionevent event) {
//get the horizontal and ordinate
int x = (int) event.getx () at the finger;
int y = (int) event.gety ();
Switch (event.getaction ()) {case
motionevent.action_down:
lastx = x;
Lasty = y;
break;
Case Motionevent.action_move:
//Calculate the moving distance
int offx = X-LASTX;
int offy = y-lasty;
Call the layout method to reposition its position
layout (GetLeft () +OFFX, GetTop () +offy, GetRight () +offx , Getbottom () +offy);
break;
return true;
}
Offsetleftandright () Offsettopandbottom ()
In fact, these two methods are to move around and move up and down the package, the incoming is the offset.
public boolean ontouchevent (Motionevent event) {
//get the horizontal and ordinate
int x = (int) event.getx () at the finger;
int y = (int) event.gety ();
Switch (event.getaction ()) {case
motionevent.action_down:
lastx = x;
Lasty = y;
break;
Case Motionevent.action_move:
//Calculate the moving distance
int offx = X-LASTX;
int offy = y-lasty;
Offsetleftandright (OFFX);
Offsettopandbottom (offy);
break;
return true;
Layoutparams
public boolean ontouchevent (Motionevent event) {
//get the horizontal and ordinate
int x = (int) event.getx () at the finger;
int y = (int) event.gety ();
Switch (event.getaction ()) {case
motionevent.action_down:
lastx = x;
Lasty = y;
break;
Case Motionevent.action_move:
//Calculate the moving distance
int offx = X-LASTX;
int offy = y-lasty;
Viewgroup.marginlayoutparams MLP =
(marginlayoutparams) getlayoutparams ();
Mlp.leftmargin = GetLeft () +offx;
Mlp.topmargin = GetTop () +offy;
Setlayoutparams (MLP);
break;
return true;
}
Scrollto () Scrollby ()
Sceollto (X,y) should be in the moving endpoint coordinates
Scrollby (Dx,dy) incoming is the increment of the move.
The value passed through the Scrollby should be the opposite of the increment you need!
public boolean ontouchevent (Motionevent event) {
//get the horizontal and ordinate
int x = (int) event.getx () at the finger;
int y = (int) event.gety ();
Switch (event.getaction ()) {case
motionevent.action_down:
lastx = x;
Lasty = y;
break;
Case Motionevent.action_move:
//Calculate the moving distance
int offx = X-LASTX;
int offy = y-lasty;
(View) GetParent ()). Scrollby (-offx,-offy);
break;
return true;
Scroller
Step One:
Initializes the Scroller object, that is, Mscroller = new Scroller (context)
Step Two:
Rewrite the Computescroll () method to implement the simulation slide. You can copy the following end template code:
public void Computescroll () {
super.computescroll ();
if (Mscroller.computescrolloffset ()) {
(View) GetParent ()). Scrollto (Mscroller.getcurrx (), Mscroller.getcurry ( ));
}
Invalidate ()//must be called
Step Three:
Open the simulation process and startscroll the method in the appropriate place (generally 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)
What needs to be stated is:
The 1.computeScrollOffset method is used to determine whether the entire slide has been completed, and returns True, indicating that it was not completed, or that the slide was completed.
The 2.getCurrY () and Getcurrx () obtain the current sliding coordinates.
3. Finally, you must use the Invalidate method to refresh. Because the Computescroll method is not invoked automatically, it is invoked in the draw method. Therefore, you must use the invalidate refresh, you will call the draw method, naturally will call the Computescroll method. This way, the loop call is implemented.
4. In Startscroll, the offset is the same as using the offset in the Scrollby method, that is, 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 values, and if you want to offset a negative value, fill in the corresponding positive!
public class Dragview extends view{private int lastx;
private int lasty;
Private Scroller Mscroller;
Public Dragview (context, AttributeSet attrs) {Super (context, attrs);
Mscroller = new Scroller (context);
public boolean ontouchevent (Motionevent event) {//Get the horizontal and ordinate int x = (int) event.getx () at the finger;
int y = (int) event.gety ();
Switch (event.getaction ()) {Case MotionEvent.ACTION_DOWN:lastX = x;
Lasty = y;
Break
Case Motionevent.action_move://calculate the moving distance int offx = X-LASTX;
int offy = Y-lasty;
View ViewGroup = (view) getparent ();
(View) GetParent ()). Scrollby (-offx,-offy);
Break
Case 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.G
ETSCROLLX (),-viewgroup.getscrolly ());
Break
return true; } public void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {(View) getparent ()). Scrollto (Mscroller.getcurrx (), MSCROLLER.GETCU
Rry ());
} invalidate ()//Must be called}
Here's an introduction to the Android view class
It's not a control translation, it's just an understanding translation. This article is only used for personal review, does not guarantee the accuracy and correctness of translation.
The basic concept of a View
1. Basic Description:
This class is the infrastructure component of the user interface. View represents a rectangular area on the screen that is responsible for drawing the area and event handling.
View is the base class for all widget classes, and widget classes are used to create interactive UI artifacts (buttons, input boxes, and so on).
The ViewGroup subclass of the view class is the layout base class, and layout is an invisible container that holds view (or ViewGroup) and defines the layout properties of these views.
You can say that the view class is the most important class in the user interface class.
2. Use view
Organization of the 2.1 View:
The view used in the same window is stored in a tree, either by dynamically increasing the deletion of the view or by defining a view tree in the XML file to construct the tree.
2.2. Main actions:
2.2.1 Settings Property: You can set the view's properties either through the view and its subclasses, or set the view's properties in the XML file.
2.2.2 Set Focus: The Requestfocus method forces the view to gain focus.
2.2.3 Settings Listener: You can set listener to monitor specific view events, such as gaining or losing focus, clicking Events, and so on.
2.2.4 Settings Visibility: can hide or show view, setvisibility.
2.3. Attention MATTERS:
The Android framework is responsible for measuring laying out and drawing view. Unless you want to achieve a viewgroup yourself,
Otherwise, you should not show the methods that invoke the implementation of these functions.
Second implementation of Custom view
To implement a custom view, you need to rewrite some of the standard methods of view.
The framework invokes these methods and believes that these methods should be implemented for all view.
These methods do not have to be fully rewritten, in fact, you can only rewrite the OnDraw function
Properties and actions for three view:
1 Properties:
1.1 ids:views has an integer corresponding to the ID used to find the specified view in the view number.
You can define a unique ID in the layout file and call Findviewbyid in the activity's OnCreate function to find this view.
In the entire tree, view can be not unique, but in a specified range of lookups we can be sure that it is unique
2. Location:
View is a rectangular area that uses the coordinates of the left & and the length and width to represent a view. We can use the method GetLeft () GetTop () GetRight () Getbottom () getwidth () and other functions
To get their location information.