FileDocCategorySizeDatePackage
AbsSeekBar.javaAPI DocAndroid 1.5 API12272Wed May 06 22:41:56 BST 2009android.widget

AbsSeekBar

public abstract class AbsSeekBar extends ProgressBar

Fields Summary
private android.graphics.drawable.Drawable
mThumb
private int
mThumbOffset
float
mTouchProgressOffset
On touch, this offset plus the scaled value from the position of the touch will form the progress value. Usually 0.
boolean
mIsUserSeekable
Whether this is user seekable.
private int
mKeyProgressIncrement
On key presses (right or left), the amount to increment/decrement the progress.
private static final int
NO_ALPHA
private float
mDisabledAlpha
Constructors Summary
public AbsSeekBar(android.content.Context context)

    
       
        super(context);
    
public AbsSeekBar(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);
    
public AbsSeekBar(android.content.Context context, android.util.AttributeSet attrs, int defStyle)

        super(context, attrs, defStyle);

        TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.SeekBar, defStyle, 0);
        Drawable thumb = a.getDrawable(com.android.internal.R.styleable.SeekBar_thumb);
        setThumb(thumb);
        int thumbOffset =
                a.getDimensionPixelOffset(com.android.internal.R.styleable.SeekBar_thumbOffset, 0);
        setThumbOffset(thumbOffset);
        a.recycle();

        a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.Theme, 0, 0);
        mDisabledAlpha = a.getFloat(com.android.internal.R.styleable.Theme_disabledAlpha, 0.5f);
        a.recycle();
    
Methods Summary
private voidattemptClaimDrag()
Tries to claim the user's drag motion, and requests disallowing any ancestors from stealing events in the drag.

        if (mParent != null) {
            mParent.requestDisallowInterceptTouchEvent(true);
        }
    
protected voiddrawableStateChanged()

        super.drawableStateChanged();
        
        Drawable progressDrawable = getProgressDrawable();
        if (progressDrawable != null) {
            progressDrawable.setAlpha(isEnabled() ? NO_ALPHA : (int) (NO_ALPHA * mDisabledAlpha));
        }
        
        if (mThumb != null && mThumb.isStateful()) {
            int[] state = getDrawableState();
            mThumb.setState(state);
        }
    
public intgetKeyProgressIncrement()
Returns the amount of progress changed via the arrow keys.

By default, this will be a value that is derived from the max progress.

return
The amount to increment or decrement when the user presses the arrow keys. This will be positive.

        return mKeyProgressIncrement;
    
public intgetThumbOffset()

see
#setThumbOffset(int)

        return mThumbOffset;
    
protected synchronized voidonDraw(android.graphics.Canvas canvas)

        super.onDraw(canvas);
        if (mThumb != null) {
            canvas.save();
            // Translate the padding. For the x, we need to allow the thumb to
            // draw in its extra space
            canvas.translate(mPaddingLeft - mThumbOffset, mPaddingTop);
            mThumb.draw(canvas);
            canvas.restore();
        }
    
voidonKeyChange()
Called when the user changes the seekbar's progress by using a key event.

    
public booleanonKeyDown(int keyCode, android.view.KeyEvent event)

        int progress = getProgress();
        
        switch (keyCode) {
            case KeyEvent.KEYCODE_DPAD_LEFT:
                if (progress <= 0) break;
                setProgress(progress - mKeyProgressIncrement, true);
                onKeyChange();
                return true;
        
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                if (progress >= getMax()) break;
                setProgress(progress + mKeyProgressIncrement, true);
                onKeyChange();
                return true;
        }

        return super.onKeyDown(keyCode, event);
    
protected synchronized voidonMeasure(int widthMeasureSpec, int heightMeasureSpec)

        Drawable d = getCurrentDrawable();

        int thumbHeight = mThumb == null ? 0 : mThumb.getIntrinsicHeight();
        int dw = 0;
        int dh = 0;
        if (d != null) {
            dw = Math.max(mMinWidth, Math.min(mMaxWidth, d.getIntrinsicWidth()));
            dh = Math.max(mMinHeight, Math.min(mMaxHeight, d.getIntrinsicHeight()));
            dh = Math.max(thumbHeight, dh);
        }
        dw += mPaddingLeft + mPaddingRight;
        dh += mPaddingTop + mPaddingBottom;
        
        setMeasuredDimension(resolveSize(dw, widthMeasureSpec),
                resolveSize(dh, heightMeasureSpec));
    
voidonProgressRefresh(float scale, boolean fromUser)

 
        Drawable thumb = mThumb;
        if (thumb != null) {
            setThumbPos(getWidth(), thumb, scale, Integer.MIN_VALUE);
            /*
             * Since we draw translated, the drawable's bounds that it signals
             * for invalidation won't be the actual bounds we want invalidated,
             * so just invalidate this whole view.
             */
            invalidate();
        }
    
protected voidonSizeChanged(int w, int h, int oldw, int oldh)

        Drawable d = getCurrentDrawable();
        Drawable thumb = mThumb;
        int thumbHeight = thumb == null ? 0 : thumb.getIntrinsicHeight();
        // The max height does not incorporate padding, whereas the height
        // parameter does
        int trackHeight = Math.min(mMaxHeight, h - mPaddingTop - mPaddingBottom);
        
        int max = getMax();
        float scale = max > 0 ? (float) getProgress() / (float) max : 0;
        
        if (thumbHeight > trackHeight) {
            if (thumb != null) {
                setThumbPos(w, thumb, scale, 0);
            }
            int gapForCenteringTrack = (thumbHeight - trackHeight) / 2;
            if (d != null) {
                // Canvas will be translated by the padding, so 0,0 is where we start drawing
                d.setBounds(0, gapForCenteringTrack, 
                        w - mPaddingRight - mPaddingLeft, h - mPaddingBottom - gapForCenteringTrack
                        - mPaddingTop);
            }
        } else {
            if (d != null) {
                // Canvas will be translated by the padding, so 0,0 is where we start drawing
                d.setBounds(0, 0, w - mPaddingRight - mPaddingLeft, h - mPaddingBottom
                        - mPaddingTop);
            }
            int gap = (trackHeight - thumbHeight) / 2;
            if (thumb != null) {
                setThumbPos(w, thumb, scale, gap);
            }
        }
    
voidonStartTrackingTouch()
This is called when the user has started touching this widget.

    
voidonStopTrackingTouch()
This is called when the user either releases his touch or the touch is canceled.

    
public booleanonTouchEvent(android.view.MotionEvent event)

        if (!mIsUserSeekable || !isEnabled()) {
            return false;
        }
        
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                setPressed(true);
                onStartTrackingTouch();
                trackTouchEvent(event);
                break;
                
            case MotionEvent.ACTION_MOVE:
                trackTouchEvent(event);
                attemptClaimDrag();
                break;
                
            case MotionEvent.ACTION_UP:
                trackTouchEvent(event);
                onStopTrackingTouch();
                setPressed(false);
                break;
                
            case MotionEvent.ACTION_CANCEL:
                onStopTrackingTouch();
                setPressed(false);
                break;
        }
        return true;
    
public voidsetKeyProgressIncrement(int increment)
Sets the amount of progress changed via the arrow keys.

param
increment The amount to increment or decrement when the user presses the arrow keys.

        mKeyProgressIncrement = increment < 0 ? -increment : increment;
    
public synchronized voidsetMax(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
            setKeyProgressIncrement(Math.max(1, Math.round((float) getMax() / 20)));
        }
    
public voidsetThumb(android.graphics.drawable.Drawable thumb)
Sets the thumb that will be drawn at the end of the progress meter within the SeekBar

param
thumb Drawable representing the thumb

        if (thumb != null) {
            thumb.setCallback(this);
        }
        mThumb = thumb;
        invalidate();
    
public voidsetThumbOffset(int thumbOffset)
Sets the thumb offset that allows the thumb to extend out of the range of the track.

param
thumbOffset The offset amount in pixels.

        mThumbOffset = thumbOffset;
        invalidate();
    
private voidsetThumbPos(int w, android.graphics.drawable.Drawable thumb, float scale, int gap)

param
gap If set to {@link Integer#MIN_VALUE}, this will be ignored and

        int available = w - mPaddingLeft - mPaddingRight;
        int thumbWidth = thumb.getIntrinsicWidth();
        int thumbHeight = thumb.getIntrinsicHeight();
        available -= thumbWidth;

        // The extra space for the thumb to move on the track
        available += mThumbOffset * 2;

        int thumbPos = (int) (scale * available);

        int topBound, bottomBound;
        if (gap == Integer.MIN_VALUE) {
            Rect oldBounds = thumb.getBounds();
            topBound = oldBounds.top;
            bottomBound = oldBounds.bottom;
        } else {
            topBound = gap;
            bottomBound = gap + thumbHeight;
        }
        
        // Canvas will be translated, so 0,0 is where we start drawing
        thumb.setBounds(thumbPos, topBound, thumbPos + thumbWidth, bottomBound);
    
private voidtrackTouchEvent(android.view.MotionEvent event)

        final int width = getWidth();
        final int available = width - mPaddingLeft - mPaddingRight;
        int x = (int)event.getX();
        float scale;
        float progress = 0;
        if (x < mPaddingLeft) {
            scale = 0.0f;
        } else if (x > width - mPaddingRight) {
            scale = 1.0f;
        } else {
            scale = (float)(x - mPaddingLeft) / (float)available;
            progress = mTouchProgressOffset;
        }
        
        final int max = getMax();
        progress += scale * max;
        if (progress < 0) {
            progress = 0;
        } else if (progress > max) {
            progress = max;
        }
        
        setProgress((int) progress, true);
    
protected booleanverifyDrawable(android.graphics.drawable.Drawable who)

        return who == mThumb || super.verifyDrawable(who);