public class Player extends ViewGroup {
private RectF rect = new RectF();
private Paint paint;
public Player(Context context,String pname) {
super(context);
setWillNotDraw(false);
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Style.FILL);
paint.setColor(getResources().getColor(R.color.red));
}
public void onDraw(Canvas canvas) {
canvas.drawRoundRect(rect, 10, 10, paint);
canvas.drawCircle(rect.centerX(), rect.centerY(), 10, paint);
//canvas.drawColor(Color.RED);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int wspec = MeasureSpec.makeMeasureSpec(
getMeasuredWidth(), MeasureSpec.EXACTLY);
int hspec = MeasureSpec.makeMeasureSpec(
getMeasuredHeight(), MeasureSpec.EXACTLY);
for(int i=0; i<getChildCount(); i++){
View v = getChildAt(i);
v.measure(wspec, hspec);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
rect.set(l, t,r, b);
}
}
第三个命令确实绘制了一个红色矩形,其边界是由参数 rect 给出的 rect (l,t,r,b) = (412,415,735,754),并且由于某种原因,前两个命令对帆布!
我已经确定矩形是一个实际的矩形,因为我提到它的值是 (412,415,735,754),它确实是一个有效的矩形,你看到我是如何定义油漆的,为什么它不画呢?
花了2个小时试图弄清楚,认真...
谢谢!
顺便说一句,该类扩展了 ViewGroup ,因为它最终意味着实现一个视图容器。