自定义进度视图未在ListView

时间:2016-05-18 05:27:42

标签: android listview

我遇到一个问题,即所有自定义进度都没有在ListView中显示进度。 以下是mucustomize进度视图:

public class LProgressBar extends View {
    private final int DEFAULT_HEIGHT = dp2px(10);

    private static final int DEFAULT_REACHED_COLOR    = 0xFFE66059;
    private static final int DEFAULT_UNREACHED_COLOR  = 0xFF7A9B5B;
    private static final int DEFAULT_ANIMATE_DURATION = 1000;
    private static final int DEFAULT_MAX_VALUE        = 100;
    private static final int DEFAULT_PROGRESS         = 0;

    private static final Boolean DEBUG = false;

    private int mHeight;
    private int mRealWidth;

    private int mReachedColor;
    private int mUnreachedColor;

    private int mDuration;

    private float mProgress;
    private int mMaxValue;

    private boolean debug;

    private Paint mUnreachedPaint;
    private Paint mReachedPaint;

    public LProgressBar(Context context) {
        this(context, null);
    }

    public LProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        obtainStyledAttributes(attrs);
        reset();
    }

    private void obtainStyledAttributes(AttributeSet attrs) {
        final TypedArray attr = getContext().obtainStyledAttributes(attrs, R.styleable.LProgressBar);

        mHeight = (int) attr.getDimension(R.styleable.LProgressBar_ruman_height, DEFAULT_HEIGHT);

        mReachedColor = attr.getColor(R.styleable.LProgressBar_ruman_reached_color, DEFAULT_REACHED_COLOR);
        mUnreachedColor = attr.getColor(R.styleable.LProgressBar_ruman_unreached_color, DEFAULT_UNREACHED_COLOR);

        debug = attr.getBoolean(R.styleable.LProgressBar_ruman_debug, DEBUG);

        mDuration = (int) attr.getDimension(R.styleable.LProgressBar_ruman_duration, DEFAULT_ANIMATE_DURATION);

        mProgress = (float) attr.getFloat(R.styleable.LProgressBar_ruman_progress, DEFAULT_PROGRESS);
        mMaxValue = (int) attr.getDimension(R.styleable.LProgressBar_ruman_maxvalue, DEFAULT_MAX_VALUE);

        attr.recycle();
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);

        int width;
        int height;

        if (widthMode == MeasureSpec.EXACTLY) {
            width = widthSize;
        } else {
            width = 100;
            if (widthMode == MeasureSpec.AT_MOST)
                width = Math.min(widthSize, 100);
        }

        if (heightMode == MeasureSpec.EXACTLY)
            height = heightSize;
        else {
            height = mHeight;
            if (heightMode == MeasureSpec.AT_MOST)
                height = Math.min(heightSize, mHeight);
        }

        setMeasuredDimension(width, height);
        mRealWidth = getMeasuredWidth();
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        canvas.drawLine(0, 0, mRealWidth, 0, mUnreachedPaint);

        canvas.drawLine(0, 0, mProgress, 0, mReachedPaint);
    }

    private void reset() {
        initUnreachedPaint();
        initReachedPaint();
    }

    private void initUnreachedPaint() {
        mUnreachedPaint = new Paint();
        mUnreachedPaint.setColor(mUnreachedColor);
        mUnreachedPaint.setStrokeWidth(dp2px(mHeight));
    }

    private void initReachedPaint() {
        mReachedPaint = new Paint();
        mReachedPaint.setColor(mReachedColor);
        mReachedPaint.setStrokeWidth(dp2px(mHeight));
    }

    public synchronized void setProgress(float progress) {

        float radio = (float) progress * 1.0f / mMaxValue * 1.0f;

        mProgress = (float) mRealWidth * radio * 1.0f;

        invalidate();
    }

    private void startProgressAnim() {
        ValueAnimator animator = ValueAnimator.ofFloat(0, mProgress);
        animator.setDuration(mDuration);
        animator.setInterpolator(new DecelerateInterpolator());
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                mProgress = value;
                invalidate();
            }
        });
        animator.start();
    }

    /**
     * dp2px
     *
     * @param dpValue dp
     * @return px
     */
    private int dp2px(int dpValue) {
        return (int) getContext().getResources().getDisplayMetrics().density * dpValue;
    }

}

我把它放在ListView中,我打电话给lProgressBar.setProgress(50.0f);但是没有工作,所有进展都是0.我试着打电话notifyDataSetChanged()仍然无效。帮帮我。

0 个答案:

没有答案