代码解决一切废话,内部注解详细:
package com.example.administrator.demo.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import android.view.WindowManager;
/**
* Created by Administrator on 2017/9/13.
*/
public class DrawView extends View {
private Paint mPaintLine;
private Paint mCirclePaint;
public DrawView(Context context) {
super(context);
init();
}
public DrawView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public DrawView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
//添加线画笔
mPaintLine = new Paint();
mPaintLine.setStrokeWidth(5);
mPaintLine.setStyle(Paint.Style.STROKE);
mPaintLine.setColor(Color.BLACK);
mPaintLine.setFlags(Paint.ANTI_ALIAS_FLAG);
//添加圆画笔(画点)
mCirclePaint = new Paint();
mCirclePaint.setColor(Color.RED);
mCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//先画横轴
drawXLine(canvas);
//再画纵轴
drawYLine(canvas);
//画横轴箭头
drawXArrow(canvas);
//画纵坐标箭头
drawYArrow(canvas);
//标记原点
drawCenterPoint(canvas);
//画函数轨迹,以y=sinx为例
drawPathRight(canvas);
drawPathLeft(canvas);
}
private void drawPathLeft(Canvas canvas) {
Path path=new Path();
path.moveTo(width/2,height/2);
//0,-360
for (int i = 0; i < 480; i++) {
//为了绘画明显,扩大相应的比例
//假设右边能完整显示2个周期,即至少平分为三等分,width/6=180度长度,1度=width/180/6
float x = width / 6 / 180 * -i + width / 2;
//假设Y的上半轴能平均分成4分,即height/2/4=1单位高度;
float y = (float) (ponitX(-i) * height / 8) + height / 2;
path.lineTo(x, y);
}
canvas.drawPath(path, mPaintLine);
}
private void drawPathRight(Canvas canvas) {
Path path = new Path();
path.moveTo(width / 2, height / 2);
//这里用角度制
for (int i1 = 0; i1 < 480; i1++) {
//为了绘画明显,扩大相应的比例
//假设右边能完整显示2个周期,即至少平分为三等分,width/6=180度长度,1度=width/180/6
float x = width / 6 / 180 * i1 + width / 2;
//假设Y的上半轴能平均分成4分,即height/2/4=1单位高度;
float y = (float) (ponitX(i1) * height / 8) + height / 2;
path.lineTo(x, y);
}
canvas.drawPath(path, mPaintLine);
}
private double ponitX(int i) {
//把弧度制转化为角度制运算
return Math.sin(Math.PI / 180 * i);
}
private void drawCenterPoint(Canvas canvas) {
canvas.drawCircle(width / 2, height / 2, 10, mCirclePaint);
}
float width = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
float height = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
private void drawYArrow(Canvas canvas) {
Path path = new Path();
path.moveTo(width / 2 - 20, 20);
path.lineTo(width / 2, 0);
path.lineTo(width / 2 + 20, 20);
canvas.drawPath(path, mPaintLine);
}
private void drawXArrow(Canvas canvas) {
Path path = new Path();
path.moveTo(width - 20, height / 2 - 20);
path.lineTo(width, height / 2);
path.lineTo(width - 20, height / 2 + 20);
canvas.drawPath(path, mPaintLine);
}
private void drawYLine(Canvas canvas) {
float startX = width / 2;
float startY = 0;
float stopX = startX;
float stopY = height;
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
}
private void drawXLine(Canvas canvas) {
float startX = 0;
float startY = height / 2;
float stopX = width;
float stopY = startY;
canvas.drawLine(startX, startY, stopX, stopY, mPaintLine);
}
}