CustomTextView canvas 未绘制

CustomTextView canvas not drawing

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();

    }
}

好的,我知道问题出在哪里了,是 drawRect 函数中的错误。 drawRect 函数需要更改为:

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

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