A glimpse of the source code of the AbsSeekBar parent class of the SeekBar Sliding bar in Android

Source: Internet
Author: User

A glimpse of the source code of the AbsSeekBar parent class of the SeekBar Sliding bar in Android

One of the controls in Android is ProgressBar, and one of its subclasses is AbsSeekBar. I believe many children's shoes are interested in the parent category of this drag bar! Especially when I see the progress bar of Netease cloud music, I can handle the playback and pause events. Is it envy me ~ I am here to tell you that you don't have to envy me. After reading the code analysis below, you can also make that effect. Let's go.

Next, let's list the member variables of AbsSeekBar.

// The current rectangle private final Rect mTempRect = new Rect (); // you can drag the slider private Drawable mThumb; // The list of color States private ColorStateList mThumbTintList = null; // private PorterDuff for the corresponding port. mode mThumbTintMode = null; // whether the supported cheerful tint private boolean mHasThumbTint = false; // the corresponding Mode is private boolean mHasThumbTintMode = false; // The offset of the slider private int mThumbOffset; // whether to split and track private boolean mSplitTrack;
In the variables, we need to know the following points:

1. mTempRect is a variable related to the entire SeekBar trajectory drawing.

2. Make the image of the Drawable slider on the SeekBar

3. The mThumbOffset is the slider distance from the left side of x.


For the AbsSeekBar member method, the following describes several important methods that are frequently used in actual development.

1. setThumbOffset

This method sets the slider distance from the left margin.

2,

Public synchronized void setMax (int max) {super. setMax (max); if (mKeyProgressIncrement = 0) | (getMax ()/mKeyProgressIncrement> 20 )) {// It will take the user too long to change this via keys, change it // to something more reasonable // set It to a reasonable value setKeyProgressIncrement (Math. max (1, Math. round (float) getMax ()/20 )));}}

This method is actually associated with two functions.

1. Set the maximum value of the current SeekBar.

2. Some mobile phones have buttons to the left and to the right, for example, I have met a Samsung business machine. According to the source code logic, the margin of one shift between the left and right buttons should not exceed 20.

The following code mainly describes how to draw a track and how to draw a slider.

if (track != null) {            track.setBounds(0, trackOffset, w - mPaddingRight - mPaddingLeft,                    h - mPaddingBottom - trackOffset - mPaddingTop);        }        if (thumb != null) {            setThumbPos(w, thumb, getScale(), thumbOffset);        }

It is important to draw tracks. Let's take a look;

Void drawTrack (Canvas canvas) {// get the reference of the current slider final Drawable thumbDrawable = mThumb; if (thumbDrawable! = Null & mSplitTrack) {final Insets insets = thumbDrawable. getOpticalInsets (); final Rect tempRect = mTempRect; thumbDrawable. copyBounds (tempRect); tempRect. offset (mPaddingLeft-mThumbOffset, mPaddingTop); tempRect. left + = insets. left; tempRect. right-= insets. right; final int saveCount = canvas. save (); // crop the current rectangle canvas. clipRect (tempRect, Op. DIFFERENCE); super. drawTrack (canvas); canvas. restoreToCount (saveCount);} else {super. drawTrack (canvas );}}
For the above code block, we will focus on the following points:

1. The reason why the mSlitTrack variable exists is that the color of the left and right sides of the slider is different. This variable is used to separate the left and right sides.

It is also important to draw the slider. Let's take a look at it below:

Void drawThumb (Canvas canvas) {if (mThumb! = Null) {canvas. save (); // Translate the padding. for the x, we need to allow the thumb to // draw in its extra space // mainly the canvas above the x axis. translate (mPaddingLeft-mThumbOffset, mPaddingTop); // you can call this operation to draw some Canvas objects. draw (canvas); canvas. restore ();}}

As we know, the position of the slider will shift with the progress, and the nature of the painting actually uses the Canvas, so the Canvas is constantly moving. That is, the meaning of this line of code:
canvas.translate(mPaddingLeft - mThumbOffset, mPaddingTop);
The following content mainly explains how AbsSeekBar handles touch events. The effect of playing and pausing Netease cloud music by clicking the progress bar is also related to the following explanation:

As above, let's first go to the Code:

Public boolean onTouchEvent (MotionEvent event) {if (! MIsUserSeekable |! IsEnabled () {return false;} switch (event. getAction () {case MotionEvent. ACTION_DOWN: if (isInScrollingContainer () {mTouchDownX = event. getX ();} else {// set the current status to the pressed status setPressed (true); if (mThumb! = Null) {invalidate (mThumb. getBounds (); // This may be within the padding region} onStartTrackingTouch (); trackTouchEvent (event); attemptClaimDrag ();} break; case MotionEvent. ACTION_MOVE: if (mIsDragging) {trackTouchEvent (event);} else {final float x = event. getX (); // if (Math. abs (x-mTouchDownX)> mScaledTouchSlop) {setPressed (true); if (mThumb! = Null) {invalidate (mThumb. getBounds (); // This may be within the padding region} onStartTrackingTouch (); trackTouchEvent (event); attemptClaimDrag () ;}} break; case MotionEvent. ACTION_UP: if (mIsDragging) {trackTouchEvent (event); onStopTrackingTouch (); setPressed (false );} else {// Touch up when we never crossed the touch slop threshold shold // be interpreted as a tap-seek to that location. onStartTrackingTouch (); trackTouchEvent (event); onStopTrackingTouch ();} // ProgressBar doesn' t know to repaint the thumb drawable // in its inactive state when the touch stops (because the // value has not apparently changed) invalidate (); break; case MotionEvent. ACTION_CANCEL: if (mIsDragging) {onStopTrackingTouch (); setPressed (false);} invalidate (); // see abve explanation break;} return true ;}

Do not worry about the code above. Let's hear the following:

1. If the current SeekBar has been set to be unable to touch the operation, you don't need to say much about it. return directly.

2. According to the source code, when the current control is in the pressed state, perform the following operations:

2. 1. Set the current status to Press.

2. 2. Refresh the current view.


If we need to simulate Netease cloud music, we need to deal with the logic of pausing music,

Note:

1. Determine whether the x coordinate of the current event is inside the slider. If yes, the current progress is not changed regardless of the current displacement.

2. Modify the image of the current Slider



Now, the entire source code is explained here. I believe you can see that SeekBar in Android has a better understanding than before! Thank you! Just ask me to send a mail. Goodbye,



Related Article

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.