这些用法都是英文的字面意思,所以解释部分就放在代码的注释那里了,要画图,就要继承View类重写onDraw方法,要注意的是构造方法也重写一下吧,话不多说直接上代码~~~
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
public MyView(Context context, AttributeSet set) {
super(context, set);
}
@Override
//重写该方法进行绘图
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//把整张画布绘制成白色
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
//去锯齿
paint.setAntiAlias(true);
//设置画笔颜色
paint.setColor(Color.BLUE);
//设置画笔风格
paint.setStyle(Paint.Style.STROKE);
//设置画笔大小
paint.setStrokeWidth(3);
//绘制圆形,这里cx,cy参数是指圆心的坐标,更JavaSE的不一样
canvas.drawCircle(40, 40, 30, paint);
//绘制正方形(左上 、右下)
canvas.drawRect(10, 80, 70, 140, paint);
//绘制矩形
canvas.drawRect(10, 150, 70, 190, paint);
//绘制圆角矩形
RectF re1 = new RectF(10, 200, 70, 230);
//下面方法参数解释
//rx The x-radius of the oval used to round the corners
//ry The y-radius of the oval used to round the corners
canvas.drawRoundRect(re1, 15, 15, paint);
//绘制椭圆
RectF re11 = new RectF(10, 240, 70, 270);
canvas.drawOval(re11, paint);
//绘制三角形--需要定义一个Path对象来绘制路径
Path path1 = new Path();
path1.moveTo(10, 340);
path1.lineTo(70, 340);
path1.lineTo(40, 290);
path1.close();
canvas.drawPath(path1, paint);
//绘制五边形--需要定义一个Path对象来绘制路径
Path path2 = new Path();
path2.moveTo(26, 360);
path2.lineTo(54, 360);
path2.lineTo(70, 392);
path2.lineTo(40, 420);
path2.lineTo(10, 392);
path2.close();
canvas.drawPath(path2, paint);
//改变风格再绘制
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
//绘制圆形
canvas.drawCircle(120, 40, 30, paint);
//绘制正方形
canvas.drawRect(90, 80, 150, 140, paint);
//绘制矩形
canvas.drawRect(90, 150, 150, 190, paint);
//绘制圆角矩形
RectF re2 = new RectF(90, 200, 150, 230);
canvas.drawRoundRect(re2, 15, 15, paint);
//绘制椭圆
RectF re21 = new RectF(90, 240, 150, 270);
canvas.drawOval(re21, paint);
//绘制三角形
Path path3 = new Path();
path3.moveTo(90, 340);
path3.lineTo(150, 340);
path3.lineTo(120, 290);
path3.close();
canvas.drawPath(path3, paint);
//绘制五角形
Path path4 = new Path();
path4.moveTo(106, 360);
path4.lineTo(134, 360);
path4.lineTo(150, 392);
path4.lineTo(120, 420);
path4.lineTo(90, 392);
path4.close();
canvas.drawPath(path4, paint);
//改变样式--设置渐变
Shader mShader = new LinearGradient(0, 0, 40, 60,
new int[]{Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW},
null, Shader.TileMode.REPEAT);
paint.setShader(mShader);
//设置阴影
paint.setShadowLayer(45, 10, 10, Color.GRAY);
//绘制圆形
canvas.drawCircle(200, 40, 30, paint);
//绘制正方形
canvas.drawRect(170, 80, 230, 140, paint);
//绘制圆角矩形
RectF re3 = new RectF();
canvas.drawRoundRect(re3, 15, 15, paint);
//绘制椭圆
RectF re31 = new RectF(170, 240, 230, 270);
canvas.drawOval(re31, paint);
//绘制三角形
Path path5 = new Path();
path5.moveTo(170, 340);
path5.lineTo(230, 340);
path5.lineTo(200, 290);
path5.close();
canvas.drawPath(path5, paint);
//绘制五边形
Path path6 = new Path();
path6.moveTo(186, 360);
path6.moveTo(214, 360);
path6.moveTo(230, 392);
path6.moveTo(200, 420);
path6.moveTo(170, 392);
path6.close();
canvas.drawPath(path6, paint);
//改变风格,绘制字体
paint.setTextSize(10);
paint.setShader(null);
// canvas.drawText(getResources().getString(R.string.circle), 240, 50, paint);
canvas.drawText(getResources().getString(R.string.square), 240, 120, paint);
// canvas.drawText(getResources().getString(R.string.rect), 240, 175, paint);
// canvas.drawText(getResources().getString(R.string.round_rect), 230, 220, paint);
// canvas.drawText(getResources().getString(R.string.oval), 240, 260, paint);
// canvas.drawText(getResources().getString(R.string.triangle), 240, 325, paint);
// canvas.drawText(getResources().getString(R.string.pentagon), 240, 390, paint);
}
}