最近在做自定义控件,温习下draw方法,画个函数轨迹图。代码上来:
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.graphics.Path;
importandroid.support.annotation.Nullable;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.view.WindowManager;
/**
* Created by Administrator on 2017/9/13.
*/
public classDrawViewextendsView {
privatePaintmPaintLine;
privatePaintmCirclePaint;
publicDrawView(Context context) {
super(context);
init();
}
publicDrawView(Context context,@NullableAttributeSet attrs) {
super(context, attrs);
init();
}
publicDrawView(Context context,@NullableAttributeSet attrs,intdefStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private voidinit() {
//添加线画笔
mPaintLine=newPaint();
mPaintLine.setStrokeWidth(5);
mPaintLine.setStyle(Paint.Style.STROKE);
mPaintLine.setColor(Color.BLACK);
mPaintLine.setFlags(Paint.ANTI_ALIAS_FLAG);
//添加圆画笔(画点)
mCirclePaint=newPaint();
mCirclePaint.setColor(Color.RED);
mCirclePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
}
@Override
protected voidonDraw(Canvas canvas) {
super.onDraw(canvas);
//先画横轴
drawXLine(canvas);
//再画纵轴
drawYLine(canvas);
//画横轴箭头
drawXArrow(canvas);
//画纵坐标箭头
drawYArrow(canvas);
//标记原点
drawCenterPoint(canvas);
//画函数轨迹,以y=sinx为例
drawPathRight(canvas);
drawPathLeft(canvas);
}
private voiddrawPathLeft(Canvas canvas) {
Path path=newPath();
path.moveTo(width/2,height/2);
//0,-360
for(inti =0; i <480; i++) {
//为了绘画明显,扩大相应的比例
//假设右边能完整显示2个周期,即至少平分为三等分,width/6=180度长度,1度=width/180/6
floatx =width/6/180* -i +width/2;
//假设Y的上半轴能平均分成4分,即height/2/4=1单位高度;
floaty = (float) (ponitX(-i) *height/8) +height/2;
path.lineTo(x, y);
}
canvas.drawPath(path,mPaintLine);
}
private voiddrawPathRight(Canvas canvas) {
Path path =newPath();
path.moveTo(width/2,height/2);
//这里用角度制
for(inti1 =0; i1 <480; i1++) {
//为了绘画明显,扩大相应的比例
//假设右边能完整显示2个周期,即至少平分为三等分,width/6=180度长度,1度=width/180/6
floatx =width/6/180* i1 +width/2;
//假设Y的上半轴能平均分成4分,即height/2/4=1单位高度;
floaty = (float) (ponitX(i1) *height/8) +height/2;
path.lineTo(x, y);
}
canvas.drawPath(path,mPaintLine);
}
private doubleponitX(inti) {
//把弧度制转化为角度制运算
returnMath.sin(Math.PI/180* i);
}
private voiddrawCenterPoint(Canvas canvas) {
canvas.drawCircle(width/2,height/2,10,mCirclePaint);
}
floatwidth= ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
floatheight= ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
private voiddrawYArrow(Canvas canvas) {
Path path =newPath();
path.moveTo(width/2-20,20);
path.lineTo(width/2,0);
path.lineTo(width/2+20,20);
canvas.drawPath(path,mPaintLine);
}
private voiddrawXArrow(Canvas canvas) {
Path path =newPath();
path.moveTo(width-20,height/2-20);
path.lineTo(width,height/2);
path.lineTo(width-20,height/2+20);
canvas.drawPath(path,mPaintLine);
}
private voiddrawYLine(Canvas canvas) {
floatstartX =width/2;
floatstartY =0;
floatstopX = startX;
floatstopY =height;
canvas.drawLine(startX, startY, stopX, stopY,mPaintLine);
}
private voiddrawXLine(Canvas canvas) {
floatstartX =0;
floatstartY =height/2;
floatstopX =width;
floatstopY = startY;
canvas.drawLine(startX, startY, stopX, stopY,mPaintLine);
}
}