Code example for EditText

Methods: EditText.invalidate, getWidth

0
 
    public void setState(int 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 
        //Log.v(TAG, "setBounds resetThumbPos left:"+String.valueOf(viewWidth - mThumbW)+" right:"+String.valueOf(viewWidth)+" button:"+String.valueOf(mThumbH)); 
        mThumbDrawable.setBounds(viewWidth - mThumbW, 0, viewWidth, mThumbH);
        mThumbDrawable.setAlpha(ScrollFade.ALPHA_MAX);
    } 
    private void useThumbDrawable(Context context, Drawable drawable) {
        mThumbDrawable = drawable;
        /*mThumbW = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                48, context.getResources().getDisplayMetrics()); 
        mThumbH = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
                52, context.getResources().getDisplayMetrics());*/ 
        mThumbW = context.getResources().getDimensionPixelSize(
                R.dimen.fastscroll_thumb_width); 
        mThumbH = context.getResources().getDimensionPixelSize(
                R.dimen.fastscroll_thumb_height); 
        mChangedBounds = true;
    } 
 
    private void init(Context context) {
        // Get both the scrollbar states drawables 
        final Resources res = context.getResources();
        useThumbDrawable(context, res.getDrawable(
                R.drawable.scrollbar_handle_accelerated_anim2)); 
        //mScrollCompleted = true; 
 
        getSectionsFromIndexer(); 
 
        mScrollFade = new ScrollFade();
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setTextAlign(Paint.Align.CENTER);
        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;
    } 
 
    void stop() { 
        setState(STATE_NONE);
    } 
 
    boolean isVisible() { 
        return !(mState == STATE_NONE);
    } 
 
    public void draw(Canvas canvas) {
        //Log.v(TAG, "draw status "+String.valueOf(mState)); 
        if (mState == STATE_NONE) {
            // No need to draw anything 
            return; 
        } 
 
        final int y = mThumbY + mList.getScrollY();
        final int viewWidth = mList.getWidth();
        final FastScroller.ScrollFade scrollFade = mScrollFade;
        final int x = mList.getScrollX();
 
        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;
            //Log.v(TAG, "setBounds draw left:"+String.valueOf(left)+" right:"+String.valueOf(viewWidth)+" button:"+String.valueOf(mThumbH)); 
            mThumbDrawable.setBounds(left, 0, viewWidth, mThumbH);
            mChangedBounds = true;
        } 
        //Log.v(TAG, "setBounds draw x:"+String.valueOf(x)+" y:"+String.valueOf(y)); 
        canvas.translate(x, y);
        mThumbDrawable.draw(canvas);
        canvas.translate(-x, -y);
 
        // If user is dragging the scroll bar, draw the alphabet overlay 
        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) {
            //Log.v(TAG, "setBounds onSizeChanged left:"+String.valueOf(w - mThumbW)+" right:"+String.valueOf(w)+" button:"+String.valueOf(mThumbH)); 
            mThumbDrawable.setBounds(w - mThumbW, 0, w, mThumbH);
        } 
    } 
 
 
 
    void onScroll(EditText view, int firstVisibleItem, int visibleItemCount,
            int totalItemCount) {
 
        //Log.d(TAG, "onScroll firstVisibleItem:"+String.valueOf(firstVisibleItem) 
        //        +" visibleItemCount:"+String.valueOf(visibleItemCount) 
        //        +" totalItemCount:"+String.valueOf(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;
        } 
        //Log.d(TAG, "onScroll mLongList:"+String.valueOf(mLongList)+" mState:"+String.valueOf(mState)); 
        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;
            } 
        } 
        //mScrollCompleted = true; 
        //Log.d(TAG, "onScroll firstVisibleItem:"+String.valueOf(firstVisibleItem)+" mVisibleItem:"+String.valueOf(mVisibleItem)); 
        if (firstVisibleItem == mVisibleItem) {
            return; 
        } 
        mVisibleItem = firstVisibleItem;
        if (mState != STATE_DRAGGING) {
            setState(STATE_VISIBLE);
            mHandler.postDelayed(mScrollFade, 1500);
        } 
    } 
 
    SectionIndexer getSectionIndexer() {
        return mSectionIndexer;
    } 
 
    Object[] getSections() {
        if (mSections == null ) {
            getSectionsFromIndexer(); 
        } 
        return mSections;
    } 
 
    private void getSectionsFromIndexer() { 
        mSectionIndexer = null;
        mSections = new String[] { " " };
    } 
 
    private void scrollTo(float position) {
        int count = mList.getLineCount();
        //mScrollCompleted = false; 
        int index = (int) (position * count);
        try { 
            int offset = mList.getLayout().getLineStart(index);
            //Log.v(TAG, "scrollTo:"+String.valueOf(index)+" offset:"+String.valueOf(offset)); 
            mList.setSelection(offset, offset);
        }catch(Exception e) {
             
        } 
 
    } 
 
    private void cancelFling() { 
        // Cancel the list fling 
        MotionEvent cancelFling = MotionEvent.obtain(0, 0, MotionEvent.ACTION_CANCEL, 0, 0, 0);
        mList.onTouchEvent(cancelFling);
        cancelFling.recycle();
    } 
 
    boolean onInterceptTouchEvent(MotionEvent ev) {
        //Log.d(TAG, "onInterceptTouchEvent mState:"+String.valueOf(mState)+" ev.getAction():"+String.valueOf(ev.getAction())); 
        if (mState > STATE_NONE && ev.getAction() == MotionEvent.ACTION_DOWN) {
            if (isPointInside(ev.getX(), ev.getY())) {
                setState(STATE_DRAGGING);
                return true; 
            } 
        } 
        return false; 
    } 
 
    boolean onTouchEvent(MotionEvent me) {
        //Log.d(TAG, "onTouchEvent mState:"+String.valueOf(mState)); 
        if (mState == STATE_NONE) {
            return false; 
        } 
 
        final int action = me.getAction();
        //Log.d(TAG, "onTouchEvent action:"+String.valueOf(action)); 
        if (action == MotionEvent.ACTION_DOWN) {
            if (isPointInside(me.getX(), me.getY())) {
                setState(STATE_DRAGGING);
                if (mSections == null ) {
                    getSectionsFromIndexer(); 
                } 
                if (mList != null) {
                } 
 
                cancelFling(); 
                return true; 
            } 
        } else if (action == MotionEvent.ACTION_UP) {
            if (mState == STATE_DRAGGING) {
                setState(STATE_VISIBLE);
                final Handler handler = mHandler;
                handler.removeCallbacks(mScrollFade);
                handler.postDelayed(mScrollFade, 1000);
                return true; 
            } 
        } else if (action == MotionEvent.ACTION_MOVE) {
            if (mState == STATE_DRAGGING) {
                final int viewHeight = mList.getHeight();
                // Jitter 
                int newThumbY = (int) me.getY() - mThumbH / 2;
                if (newThumbY < 0) {
                    newThumbY = 0;
                } else if (newThumbY + mThumbH > viewHeight) {
                    newThumbY = viewHeight - mThumbH;
                } 
                //Log.v(TAG, "onTouchEvent: Math.abs(mThumbY - newThumbY): "+String.valueOf(Math.abs(mThumbY - newThumbY))); 
                if (Math.abs(mThumbY - newThumbY) < 2) {
                    return true; 
                } 
                mThumbY = newThumbY;
                // If the previous scrollTo is still pending 
//                if (mScrollCompleted) { 
                    scrollTo((float) mThumbY / (viewHeight - mThumbH));
//                } 
                return true; 
            } 
        } 
        return false; 
    } 
 
    boolean isPointInside(float x, float y) {
        return x > mList.getWidth() - mThumbW && y >= mThumbY && y <= mThumbY + mThumbH;
    } 
 
    public class ScrollFade implements Runnable {
 
        long mStartTime;