This is the 13th day of my participation in the November Gwen Challenge. Check out the event details: The last Gwen Challenge 2021

Introduction to basic view components

View is the Android View control base class, common View components in daily development are inherited from View, such as TextView, Button, ListView and so on. Therefore, View exists as the most primitive and basic View component class in Android development, and various other View components are built on top of it. There is also a View base component class, ViewGrop, which also inherits from View. The difference is that a ViewGroup can be made up of multiple child views (the child views here can also be ViewGrop), thus forming the concept of a View tree. So the Android View component, the Button is definitely a View component, and the LinearLayout and RelativeLayout are ViewGroup components.

classDiagram
View  <|-- ViewGroup
View  <|-- TextView
View  <|-- Button
View  <|-- ImageView

TextView  <|-- EditText

ViewGroup <|-- LinearLayout
ViewGroup <|-- RelativeLayout
ViewGroup <|-- FrameLayout


View coordinates

The View position parameter has four attribute values: left, top, right, and bottom, respectively representing the upper-left abscissa, upper-left abscissa, lower-right abscissa, and lower-right abscissa. Because the origin of the View coordinate system on Android is in the upper-left corner, that is, the forward directions of the X and y axes are to the right and down. In addition, a View’s coordinate system is relative to its parent View, but if the current View is relative to the root View, its coordinate system is relative to the root View.

Touch the attribute

Gesture events

Mobile phones are all touch screens, so the View component can’t do without touch events. When the user clicks on the phone screen, a series of touch events will be triggered in the View View:

  1. ACTION_DOWN Press down from the view
  2. ACTION_MOVE Moves on the view
  3. ACTION_UP lifts from the view

Generally, gesture events are triggered mainly in the above three situations, which are also inevitable. ACTION_DOWN and ACTION_UP are generally in one-to-one correspondence (except when ACTION_UP is not executed in unexpected circumstances, ACTION_CANCEL may be executed).

Minimum slip distance determination

So how does the system distinguish between a swipe and a click? On Android, there is a TouchSlop, which represents the minimum distance to swipe and measures whether or not the current slide on the screen is less than the slide gate value. If less than the sliding gate value, the system determines that the current operation is not a sliding operation. Of course TouchSlop is also available for developers to use for gesture determination, using viewConfiguration.get (contenxt).getScaledTouchSlop() to get gate values.

Speed of tracking

Android also offers a method class called VelocityTracker that tracks the speed of a slide, allowing you to calculate how fast your finger is moving horizontally and vertically.

The basic method calls are as follows:

VelocityTracker velocityTracker = VelocityTracker.obtain();
velocityTracker.addMovement(event);// Event Events can be obtained from onTouchEvent
velocityTracker.computeCurrentVelocity(100); // Speed value within 100ms slide speed pixel /100ms
float x = velocityTracker.getXVelocity();
float y = velocityTracker.getYVelocity();
// Remember to recycle the released resources after use
velocityTracker.clear();
velocityTracker.recycle(); //
Copy the code

Signal detection

In addition, Android also provides a method class GestureDetector that can assist developers to detect actions such as click, swipe, long press and double click of View. It may not be commonly used in actual development (GestureDetector supports double click events in the old VERSION of THE API, but it seems to have been removed in the later version). Usually developers detect View action events is to add gesture methods, commonly used gesture operation methods are setOnClickListener, setOnLongClickListener, etc., of course, the double click function for developers can also design their own implementation.

View view = new View(this);
view.setOnClickListener(this); / / click
view.setOnLongClickListener(this);/ / long press

// The GestureDetector implementation intercepts the View onTouch event
GestureDetector gestureDetector = new GestureDetector(this.new GestureDetector.OnGestureListener() {
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {}@Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }

    @Override
    public void onLongPress(MotionEvent e) {}@Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        return false; }}); view.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        returngestureDetector.onTouchEvent(event); }});Copy the code