Code example for AbsListView

Methods: getHeight, getWidth, invalidate

0
 
        System.out.println("State: " + state);
        switch (state) {
        case STATE_NONE:
            mHandler.removeCallbacks(mScrollFade);
            mList.invalidate();
            break; 
        case STATE_VISIBLE:
            if (mState != STATE_VISIBLE) { // Optimization
                resetThumbPos(); 
            } 
            // Fall through 
        case STATE_DRAGGING:
            mHandler.removeCallbacks(mScrollFade);
            break; 
        case STATE_EXIT:
            int viewWidth = mList.getWidth();
            mList.invalidate(viewWidth - mThumbW, mThumbY, viewWidth, mThumbY + mThumbH);
            break; 
        } 
        mState = state;
    } 
 
    public int getState() { 
        return mState;
    } 
 
    private void resetThumbPos() { 
        final int viewWidth = mList.getWidth();
        // Bounds are always top right. Y coordinate get's translated during draw 
        mThumbDrawable.setBounds(viewWidth - mThumbW, 0, viewWidth, mThumbH);
        mThumbDrawable.setAlpha(ScrollFade.ALPHA_MAX);
    } 
 
    private void useThumbDrawable(Drawable drawable) {
        mThumbDrawable = drawable;
        mThumbW = mOverlaySize * 3 / 4; // 64; //mCurrentThumb.getIntrinsicWidth();
        mThumbH = mOverlaySize * 3 / 4; // 52; //mCurrentThumb.getIntrinsicHeight();
        mChangedBounds = true;
    } 
 
    private void init(Context context) {
        // Get both the scrollbar states drawables 
        final Resources res = context.getResources();
        useThumbDrawable(res.getDrawable(R.drawable.scroll_icon));
 
        mOverlayDrawable = res.getDrawable(R.drawable.scroll_line);
 
        mScrollCompleted = true;
 
        getSections(); 
 
        mOverlayPos = new RectF();
        mScrollFade = new ScrollFade();
        mPaint = new TextPaint();
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);
        mPaint.setTextSize(mOverlaySize / 3);
        TypedArray ta = context.getTheme().obtainStyledAttributes(new int[] { android.R.attr.textColorPrimary });
        ColorStateList textColor = ta.getColorStateList(ta.getIndex(0));
        int textColorNormal = textColor.getDefaultColor();
        mPaint.setColor(textColorNormal);
        mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
 
        mState = STATE_NONE;
 
        // Send a fake onSizeChanged so that overlay position is correct if 
        // this is called after Activity is stable 
        final int w = mList.getWidth();
        final int h = mList.getHeight();
        onSizeChanged(w, h, w, h);
    } 
 
    void stop() { 
        setState(STATE_NONE);
        // No need for these any more. 
        mOverlayDrawable = null;
        mThumbDrawable = null;
    } 
 
    boolean isVisible() { 
        return !(mState == STATE_NONE);
    } 
 
    public void draw(final Canvas canvas) {
 
        if (mState == STATE_NONE) {
            // No need to draw anything 
            return; 
        } 
 
        final int y = mThumbY;
        final int viewWidth = mList.getWidth();
        final FastScroller.ScrollFade scrollFade = mScrollFade;
 
        int alpha = -1;
        if (mState == STATE_EXIT) {
            alpha = scrollFade.getAlpha();
            if (alpha < ScrollFade.ALPHA_MAX / 2) {
                mThumbDrawable.setAlpha(alpha * 2);
            } 
            int left = viewWidth - (mThumbW * alpha) / ScrollFade.ALPHA_MAX;
            mThumbDrawable.setBounds(left, 0, viewWidth, mThumbH);
            mChangedBounds = true;
        } 
 
        canvas.translate(0, y);
        mThumbDrawable.draw(canvas);
        canvas.translate(0, -y);
 
        // If user is dragging the scroll bar, draw the alphabet overlay 
        if (mState == STATE_DRAGGING && mDrawOverlay) {
            mOverlayDrawable.draw(canvas);
            final TextPaint paint = mPaint;
            float descent = paint.descent();
            final RectF rectF = mOverlayPos;
            final String text = TextUtils.ellipsize(mSectionText, paint, (mOverlayPos.right - mOverlayPos.left) * 0.8f, TextUtils.TruncateAt.END).toString();
            canvas.drawText(text, (int) (rectF.left + rectF.right) / 2,
            // Base of text at: (middle) + (half text height) - descent : so it is vertically centred 
                    (int) (rectF.bottom + rectF.top) / 2 + mOverlaySize / 6 - descent, paint);
        } else if (mState == STATE_EXIT) {
            if (alpha == 0) { // Done with exit
                setState(STATE_NONE);
            } else { 
                mList.invalidate(viewWidth - mThumbW, y, viewWidth, y + mThumbH);
            } 
        } 
    } 
 
    void onSizeChanged(int w, int h, int oldw, int oldh) {
        if (mThumbDrawable != null) {
            mThumbDrawable.setBounds(w - mThumbW, 0, w, mThumbH);
        } 
        final RectF pos = mOverlayPos;
        // Original: width was equal to height, controlled by mOverlaySize. 
        // pos.left = (w - mOverlaySize) / 2; 
        // pos.right = pos.left + mOverlaySize; 
        // 
        // Now, Make it 75% of total available space 
        pos.left = (w / 8);
        pos.right = pos.left + w * 3 / 4;
        pos.top = h / 10; // 10% from top
        pos.bottom = pos.top + mOverlaySize;
        if (mOverlayDrawable != null) {
            mOverlayDrawable.setBounds((int) pos.left, (int) pos.top, (int) pos.right, (int) pos.bottom);
        } 
    } 
 
    void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        // Are there enough pages to require fast scroll? Recompute only if total count changes 
        if (mItemCount != totalItemCount && visibleItemCount > 0) {
            mItemCount = totalItemCount;
            mLongList = mItemCount / visibleItemCount >= MIN_PAGES;
        } 
        if (!mLongList) {
            if (mState != STATE_NONE) {
                setState(STATE_NONE);
            } 
            return; 
        } 
        if (totalItemCount - visibleItemCount > 0 && mState != STATE_DRAGGING) {
            mThumbY = ((mList.getHeight() - mThumbH) * firstVisibleItem) / (totalItemCount - visibleItemCount);
            if (mChangedBounds) {
                resetThumbPos(); 
                mChangedBounds = false;
            } 
        }