我在向EditText子类添加自定义绘图命令时遇到问题。我已经覆盖了onDraw方法,但唯一适用于我的命令是canvas.drawColor()
我有这个代码(它仅用于测试和调试目的:)导致空白画布(白色)
public class ClearableEditText extends EditText {
private Paint mPaint;
private Path mPath;
public ClearableEditText(Context context) {
super(context);
init();
}
public ClearableEditText(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ClearableEditText(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
initPaint();
}
private void initPaint() {
mPaint = new Paint(getPaint());
mPaint.setColor(getResources().getColor(R.color.green));
mPaint.setStyle(Paint.Style.FILL);
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
DebugLog.d(canvas.getWidth()+"px "+canvas.getHeight()+"px");
canvas.drawColor(Color.WHITE);
canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), mPaint);
canvas.drawText("text", 0, 30, mPaint);
}
}
有人知道为什么drawRect()
和drawText()
命令没有做任何事情吗?我期待一些非常有趣的事情:)