Android ProgressBar 之源码解析及扩展应用!

来源:互联网 发布:mac osx yosemite.vdi 编辑:程序博客网 时间:2024/05/16 17:37

Android ProgressBar 之源码解析及扩展应用!


转载请注明出处:http://blog.csdn.net/crazy1235/article/details/74194504


setProgress(int progress)

@android.view.RemotableViewMethod    public synchronized void setProgress(int progress) {        setProgressInternal(progress, false, false);    }
public void setProgress(int progress, boolean animate) {        setProgressInternal(progress, false, animate);    }

这里写图片描述

@android.view.RemotableViewMethod    synchronized boolean setProgressInternal(int progress, boolean fromUser, boolean animate) {        if (mIndeterminate) {            // Not applicable.            return false;        }        progress = MathUtils.constrain(progress, 0, mMax);        if (progress == mProgress) {            // No change from current.            return false;        }        mProgress = progress;        refreshProgress(R.id.progress, mProgress, fromUser, animate);        return true;    }
private synchronized void refreshProgress(int id, int progress, boolean fromUser,            boolean animate) {        if (mUiThreadId == Thread.currentThread().getId()) {            doRefreshProgress(id, progress, fromUser, true, animate);        } else {            if (mRefreshProgressRunnable == null) {                mRefreshProgressRunnable = new RefreshProgressRunnable();            }            final RefreshData rd = RefreshData.obtain(id, progress, fromUser, animate);            mRefreshData.add(rd);            if (mAttached && !mRefreshIsPosted) {                post(mRefreshProgressRunnable);                mRefreshIsPosted = true;            }        }    }
private synchronized void doRefreshProgress(int id, int progress, boolean fromUser,            boolean callBackToApp, boolean animate) {        final float scale = mMax > 0 ? progress / (float) mMax : 0;        final boolean isPrimary = id == R.id.progress;        if (isPrimary && animate) {            final ObjectAnimator animator = ObjectAnimator.ofFloat(this, VISUAL_PROGRESS, scale);            animator.setAutoCancel(true);            animator.setDuration(PROGRESS_ANIM_DURATION);            animator.setInterpolator(PROGRESS_ANIM_INTERPOLATOR);            animator.start();        } else {            setVisualProgress(id, scale);        }        if (isPrimary && callBackToApp) {            onProgressRefresh(scale, fromUser, progress);        }    }
private void setVisualProgress(int id, float progress) {        mVisualProgress = progress;        Drawable d = mCurrentDrawable;        if (d instanceof LayerDrawable) {            d = ((LayerDrawable) d).findDrawableByLayerId(id);            if (d == null) {                // If we can't find the requested layer, fall back to setting                // the level of the entire drawable. This will break if                // progress is set on multiple elements, but the theme-default                // drawable will always have all layer IDs present.                d = mCurrentDrawable;            }        }        if (d != null) {            final int level = (int) (progress * MAX_LEVEL);            d.setLevel(level);        } else {            invalidate();        }        onVisualProgressChanged(id, progress);    }
阅读全文
1 0
原创粉丝点击