Android View Series One: View Basics

Source: Internet
Author: User
Tags constant definition gety

1. What is a view

View is the base class for all controls in Android, an abstraction of an interface layer of controls that represents a control, in addition to view and ViewGroup (control group), ViewGroup contains many controls inside. That is, a group of view.viewgroup inherit view.

Position parameters for 2.View

The location of the view is determined by four vertices:top left right bottom
Top: Is the upper left corner ordinate.
Left: It is the upper right corner of the horizontal axis.
Right: It is the lower-left corner of the horizontal axis.
Bottom: Is the bottom right corner ordinate.
These coordinates are relative to the view's parent container, so it is a relative coordinate

Get the width of the view:

Width=right-left

Get the View height:

Height=bottom-top

How do I get the four parameters of the view?

Left=getleft ();
Right=getright ();
Top=gettop ();
Bottom=getbottom ();
In addition, the view also has four additional parameters:x ,y, translationx,translationy
X and y are the coordinates of the upper-left corner of the view.
Translationx and Translationy are the offsets of the upper left corner of the view relative to the parent container.
These four parameters are also relative to the parent container's coordinates, the default value of Translationx and Translationy is 0, and the view's four basic positional parameters, view also provides them with the Get/set method.
Conversion relationships for several parameters:
X=left+translationx
Y=top+translationy
When the view is panning, top and left represent the position information of the original upper right corner, whose value is not changed, and the four parameters of X,y,translationx and translationy are changed.

3.MotionEvent and Touchslop1.motionevent

A series of events occur when the finger touches the screen, and typical events are:
action_down– finger just touching the screen
action_move– finger moves on the screen
action_up– the moment the finger is released from the screen
Touching the screen one finger at a time produces a series of events:
Click on the screen and leave to release, the sequence of events is down->up;
Tap the screen to slide for a while, and then release the sequence of events as Down->move->...>move->up.
With the Motionevent object we can get the x and y coordinates of the Click event. The system provides two sets of methods: Getx/gety and Getrawx/getrawy.

Getx/gety and Getrawx/getrawy differences:

Getx/gety: Returns the x and Y coordinates relative to the upper-left corner of the current view,
Getrawx/getrawy: Returns the x and Y coordinates relative to the upper-left corner of the phone screen.

2.TouchSlop

Touchslop is the smallest distance that the system can recognize as sliding, and if the distance between the two slides is less than the constant when the finger is sliding on the screen, then the system does not think you are doing a slide operation.
Because the sliding distance is too short, the system does not think it is sliding.
This is a constant with different values for the device, which can be obtained by the following means:
Viewconfiguration.get (GetContext ()). Getscaledtouchslop ().
The definition of this constant can be found in the source code: in the Frameworks/base/core/res/res/values/config.xml file, "Config_viewconfigurationtouchslop" corresponds to this constant definition :

<dimen name="config_viewConfigurationTouchSlop">8dp</dimen>
4.VelocityTracker Gesturedetector and Scroller1.velocitytracker

Velocitytracker speed tracking, used to track the speed of your finger during sliding, including horizontal and vertical speeds.
Use procedure:
first , the speed of the current click event is tracked in the Ontouchevent method of the view:

VelocityTracker velocityTracker=VelocityTracker.obtain();velocityTracker.addMovement(event);

then , when we want to know the current sliding speed, we can use the following method to get the current speed:

VelocityTracker.computeCurrentVelocity(1000);int xVelocity=(int)velocityTracker.getXVelocity();int yVelocity=(int)velocityTracker.getYVelocity();

Note Three points:
1th, the speed must be calculated before acquiring the speed, that is, the getxvelocity and Getyvelocity methods must be called before the Computecurrentvelocity method;
2nd: The speed here refers to the number of pixels that the fingers have slipped over a period of time,
3rd: The speed can be negative, and when the finger slides from right to left, the horizontal velocity is negative.
The calculation formula of the velocity:
Speed = (end position-start position)/time period
With the formula and the Android coordinate system, it is known that the finger is sliding in the positive direction of the coordinate system, resulting in a negative velocity.
Computecurrentvelocity The parameter of this method represents a time unit or time interval, which is in milliseconds (ms) and the speed at which it is calculated is the number of pixels that the finger slides in the horizontal or vertical direction at that time interval.
finally , when you don't need to use it, you need to call the clear method to reset and reclaim the memory:

velocityTracker.clear();velocityTracker.recycle();
2.GestureDetector

Gesturedetector gesture detection, which is used to assist in detecting the user's click-and-swipe long-tap behavior.
Use procedure:
first , you need to create a Gesturedetector object and implement the Ongesturelistener interface, and implement the Ondoubletaplistener listener double-click behavior:

GestureDetector mGestureDetector=new GestureDetector(this);//解决长按屏幕后无法拖动的现象mGestureDetector.setIsLongpressEnabled(false);

next , take over the Ontouchevent method of the target view, add the following implementation in the Ontouchevent method to listen to the view:

boolean consume=mGestureDetector.onTouchEvent(event);return consume;

then , selectively implement the methods in Ongesturelistener and Ondoubletaplistener: Onsingletapup (click), onfling (Quick swipe), onscroll (drag), Onlongpress (Long Press) and ONDOUBLETAP (double-click).
If you just listen to the slide-related, implemented in Ontouchevent, if you want to listen to double-click this behavior, then use Gesturedetector.

3.Scroller

Scroller Elastic Sliding object, used to realize the elastic sliding of the view, scroller to achieve the transition effect of the slide, the process is not instantaneous, but at a certain time interval to complete.
How to use:

Scroller scroller=NewScroller (Mcontext);//Slow slide to specified positionPrivate void Smoothscrollto(intDESTX,intDesty) {intSCROLLX=GETSCROLLX ();intDELTA=DESTX-SCROLLX;//1000ms slide to DESTX, the effect is to slowly slideMscroller.startscroll (SCROLLX,0, Delta,0, +); Invalidate ();}@Override Public void Computescroll(){if(Mscroller.computescrolloffset ())    {ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ());    Postinvalidate (); }}

Android View Series One: View Basics

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.