android view 绘制图形
public class DrawBaseView extends View {
private Paint paintF, paintS, paintTF, paintTS, paintTFAS;
public DrawBaseView(Context context) {
super(context);
init();
}
public DrawBaseView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawBaseView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paintF = new Paint(Paint.ANTI_ALIAS_FLAG);
paintF.setStyle(Paint.Style.FILL);
paintF.setStrokeWidth(3);
paintF.setColor(Color.RED);
paintS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintS.setStyle(Paint.Style.STROKE);
paintS.setStrokeWidth(3);
paintS.setColor(Color.RED);
paintTF = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTF.setStyle(Paint.Style.FILL);
paintTF.setStrokeWidth(3);
paintTF.setTextSize(32);
paintTF.setColor(Color.RED);
paintTS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTS.setStyle(Paint.Style.STROKE);
paintTS.setStrokeWidth(3);
paintTS.setColor(Color.YELLOW);
paintTFAS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTFAS.setStyle(Paint.Style.FILL_AND_STROKE);
paintTFAS.setStrokeWidth(3);
paintTFAS.setColor(Color.YELLOW);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//设置画布颜色
canvas.drawColor(Color.BLACK);
//画点
canvas.drawPoint(40, 40, paintF);
//画直线
canvas.drawLine(80, 40, 600, 40, paintF);
//画实心矩形
canvas.drawRect(80, 80, 300, 200, paintF);
//画空心矩形
canvas.drawRect(380, 80, 600, 200, paintS);
//画实心圆
canvas.drawCircle(190, 350, 100, paintF);
//画空心圆
canvas.drawCircle(490, 350, 100, paintS);
//画实心椭圆
canvas.drawOval(new RectF(80, 500, 300, 650), paintF);
//画和椭圆一样参数的矩形
canvas.drawRect(new RectF(80, 500, 300, 650), paintS);
//画空心椭圆
canvas.drawOval(new RectF(380, 500, 600, 650), paintS);
//画实心圆角矩形
canvas.drawRoundRect(new RectF(80, 700, 300, 830), 30, 30, paintF);
//画空心圆角矩形
canvas.drawRoundRect(new RectF(380, 700, 600, 830), 30, 30, paintS);
//画扇形
canvas.drawArc(new RectF(80, 850, 300, 1070), -120, 110, true, paintF);
canvas.drawArc(new RectF(380, 850, 600, 1070), -110, 100, true, paintS);
//画弓形
canvas.drawArc(new RectF(80, 1000, 300, 1220), -120, 110, false, paintF);
canvas.drawArc(new RectF(380, 1000, 600, 1220), -120, 110, false, paintS);
}
}
android view 绘制文本
public class DrawTextView extends View {
private Paint paintTS, paintTFAS, paintTF, paintSL, paintC, paintX, paintSH;
private Path path;
public DrawTextView(Context context) {
super(context);
init();
}
public DrawTextView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
paintTF = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTF.setStyle(Paint.Style.FILL);
paintTF.setStrokeWidth(3);
paintTF.setTextSize(34);
paintTF.setColor(Color.RED);
paintTS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTS.setStyle(Paint.Style.STROKE);
paintTS.setStrokeWidth(3);
paintTS.setTextSize(34);
paintTS.setColor(Color.RED);
paintTFAS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintTFAS.setStyle(Paint.Style.FILL_AND_STROKE);
paintTFAS.setStrokeWidth(3);
paintTFAS.setTextSize(34);
paintTFAS.setColor(Color.RED);
paintSL = new Paint(Paint.ANTI_ALIAS_FLAG);
paintSL.setStyle(Paint.Style.FILL);
paintSL.setStrokeWidth(3);
paintSL.setTextSize(32);
paintSL.setColor(Color.RED);
paintC = new Paint(Paint.ANTI_ALIAS_FLAG);
paintC.setStyle(Paint.Style.FILL);
paintC.setStrokeWidth(3);
paintC.setTextSize(32);
paintC.setColor(Color.RED);
paintX = new Paint(Paint.ANTI_ALIAS_FLAG);
paintX.setStyle(Paint.Style.FILL);
paintX.setStrokeWidth(3);
paintX.setTextSize(32);
paintX.setColor(Color.RED);
path = new Path();
paintSH = new Paint(Paint.ANTI_ALIAS_FLAG);
paintSH.setStyle(Paint.Style.FILL);
paintSH.setStrokeWidth(3);
int[] colors = new int[]{Color.RED, Color.YELLOW, Color.GREEN, Color.BLUE};
LinearGradient linearGradient = new LinearGradient(40, 700, 500, 700, colors, null, Shader.TileMode.CLAMP);
paintSH.setShader(linearGradient);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.BLACK);
canvas.drawText("实心不描边字:Paint.Style.FILL", 40, 150, paintTF);
canvas.drawText("空心只描边字:Paint.Style.STROKE", 40, 250, paintTS);
canvas.drawText("实心加描边字:Paint.Style.FILL_AND_STROKE", 40, 350, paintTFAS);
paintSL.setShadowLayer(5, 5, 5, Color.YELLOW);
canvas.drawText("阴影文字", 40, 450, paintSL);
paintX.setTextSkewX(-0.5f);
canvas.drawText("斜体文字", 40, 530, paintX);
path.moveTo(40, 600);
//path.rCubicTo(200,150,400,-150,600,0);
path.cubicTo(240, 750, 440, 450, 640, 600);
canvas.drawTextOnPath("在Path上写的字11111111112222", path, 50, 0, paintTFAS);
//画出Path
canvas.drawPath(path, paintTS);
//画渐变
canvas.drawRect(40, 700, 500, 1000, paintSH);
}
}
android view 绘制路径
public class DrawPathView extends View {
Paint paintF,paintS;
public DrawPathView(Context context) {
super(context);
init();
}
public DrawPathView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawPathView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init(){
paintF = new Paint(Paint.ANTI_ALIAS_FLAG);
paintF.setStyle(Paint.Style.FILL);
paintF.setStrokeWidth(3);
paintF.setAntiAlias(true);
paintF.setColor(Color.RED);
paintS = new Paint(Paint.ANTI_ALIAS_FLAG);
paintS.setStyle(Paint.Style.STROKE);
paintS.setAntiAlias(true);
paintS.setStrokeWidth(3);
paintS.setColor(Color.RED);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path = new Path();
canvas.translate(400,500);
path.lineTo(-100,0);
path.addCircle(0,0,100, Path.Direction.CCW);
path.lineTo(-100,200);
path.lineTo(200,200);
path.close();
canvas.drawPath(path,paintS);
}
}
网友评论