画一个圆使用的是drawCircle:
canvas.drawCircle(cx, cy, radius, paint);
x、y代表坐标、radius是半径、paint是画笔,就是画图的颜色;在画图的时候还要有注意,你所画的矩形是实心
paint.setStyle(Paint.Style.FILL)
还是空心paint.setStyle(Paint.Style.STROKE);
画图的时候还有一点,那就是消除锯齿:paint.setAntiAlias(true);
- 还有就是设置一种渐变颜色的矩形:
Shader mShader = new LinearGradient(0,0,100,100, new int[]{Color.RED,Color.GREEn,Color.BLUE,Color.YELLO},null,Shader.TileMode.REPEAT);
ShapeDrawable sd;
//画一个实心正方形
sd = new ShapeDrawable(new RectShape());
sd.getPaint.setShader(mShader);
sd.setBounds(20,20,100,100);
sd.draw(canvas);
//一个渐变色的正方形就完成了
- 正方形:drawRect:
canvas.drawRect(left, top, right, bottom, paint);
这里的left、top、right、bottom的值是:
left:是矩形距离左边的X轴
top:是矩形距离上边的Y轴(左上角的坐标)
right:是矩形距离右边的X轴
bottom:是矩形距离下边的Y轴(右下角的坐标)
长方形:他和正方形是一个原理,这个就不用说了
椭圆形:记住,这里的Rectf是float类型的
RectF re = new Rect(left, top, right, bottom);
canvas.drawOval(re,paint);