CustomTextView画布不绘图

时间:2015-10-22 11:29:15

标签: android android-canvas custom-component

Drawable不在自定义TextView中绘图。任何人都可以帮我解决问题。我试图构建的是一个显示进度的texview。下面提到的代码只是一个示例。

public class ProgressableTextview extends TextView {

    Paint mPaint;
    int mProgress = 70;
    int maxProgress = 100;
    int mRadiud = 10;

    public ProgressableTextview(Context context) {
        super(context);
        init();
    }

    public ProgressableTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressableTextview(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    public void init(){

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.FILL);

    }

    @Override
    public void onDraw(Canvas canvas) {

        canvas.drawColor(Color.GRAY);
        canvas.drawRect(0, 0, (mProgress/maxProgress)*getWidth(), getMeasuredHeight(), mPaint);
        canvas.save();
        canvas.translate(getLeft(), 0);
        super.onDraw(canvas);
        canvas.restore();

    }
}

1 个答案:

答案 0 :(得分:0)

好的,我想出了问题是什么,这是drawRect函数中的错误。 drawRect函数需要更改为:

canvas.drawRect(0, 0, ((float) mProgress/maxProgress) * getWidth(), getMeasureHeight(), mPaint);

因为mProgress是一个整数除以整数返回零,这就是问题所在。