package andy.costomview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
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;
/**
* Created by Administrator on 2016/10/28.
*/
public class DrawView extends View{
public DrawView(Context context) {
super(context);
}
public DrawView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public DrawView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint p = new Paint();
p.setTextSize(30);
p.setColor(Color.RED);
canvas.drawText("画圆:", 10, 20, p);
canvas.drawCircle(60, 20, 10, p);
p.setAntiAlias(true);
canvas.drawCircle(120, 20, 20, p);
canvas.drawText("画线及弧线:", 10, 60, p);
p.setColor(Color.GREEN);
canvas.drawLine(60, 40, 100, 40, p);
canvas.drawLine(110, 40, 190, 80, p);
p.setStyle(Paint.Style.STROKE);
RectF oval1=new RectF(150,20,180,40);
canvas.drawArc(oval1, 180, 180, false, p);
oval1.set(190, 20, 220, 40);
canvas.drawArc(oval1, 180, 180, false, p);
oval1.set(160, 30, 210, 60);
canvas.drawArc(oval1, 0, 180, false, p);
canvas.drawText("画矩形:", 10, 80, p);
p.setColor(Color.GRAY);
p.setStyle(Paint.Style.FILL);
canvas.drawRect(60, 60, 80, 80, p);
canvas.drawRect(60, 90, 160, 100, p);
canvas.drawText("画扇形和椭圆:", 10, 120, p);
Shader mShader = new LinearGradient(0, 0, 100, 100,
new int[] { Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW,
Color.LTGRAY }, null, Shader.TileMode.REPEAT);
p.setShader(mShader);
RectF oval2 = new RectF(60, 100, 200, 240);
canvas.drawArc(oval2, 200, 130, true, p);
oval2.set(210,100,250,130);
canvas.drawOval(oval2, p);
canvas.drawText("画三角形:", 10, 200, p);
Path path = new Path();
path.moveTo(80, 200);
path.lineTo(120, 250);
path.lineTo(80, 250);
path.close();
canvas.drawPath(path, p);
p.reset();
p.setColor(Color.LTGRAY);
p.setStyle(Paint.Style.STROKE);
Path path1=new Path();
path1.moveTo(180, 200);
path1.lineTo(200, 200);
path1.lineTo(210, 210);
path1.lineTo(200, 220);
path1.lineTo(180, 220);
path1.lineTo(170, 210);
path1.close();
canvas.drawPath(path1, p);
p.setStyle(Paint.Style.FILL);
p.setColor(Color.LTGRAY);
p.setAntiAlias(true);
canvas.drawText("画圆角矩形:", 10, 260, p);
RectF oval3 = new RectF(80, 260, 200, 300);
canvas.drawRoundRect(oval3, 20, 15, p);
canvas.drawText("画贝塞尔曲线:", 10, 310, p);
p.reset();
p.setStyle(Paint.Style.STROKE);
p.setColor(Color.GREEN);
Path path2=new Path();
path2.moveTo(100, 320);
path2.quadTo(150, 310, 170, 400);
canvas.drawPath(path2, p);
p.setStyle(Paint.Style.FILL);
canvas.drawText("画点:", 10, 390, p);
canvas.drawPoint(60, 390, p);
canvas.drawPoints(new float[]{60,400,65,400,70,400}, p);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
canvas.drawBitmap(bitmap, 250,360, p);
}
}