1.自定义View
public class PaintView extends View {
Paint paint;
public PaintView(Context context) {
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint = new Paint();
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(new Rect(100, 100, 200, 200), paint);
}
}
2.在MainActivity中把PaintView设置进来
setContentView(new PaintView(this));
※canvas.drawRect(new Rect(100, 100, 200, 200), paint);
left,top,right,bottom不能设置一样的坐标值,不然画布上不能显示所要画的图形。
(canvas.drawRect(new Rect(100, 100, 100, 100), paint);)就不行