美文网首页
Android 图像 Y = sinX;

Android 图像 Y = sinX;

作者: 胡先润 | 来源:发表于2017-09-13 16:23 被阅读0次

最近在做自定义控件,温习下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);

}

}

相关文章

  • Android 图像 Y = sinX;

    最近在做自定义控件,温习下draw方法,画个函数轨迹图。代码上来: importandroid.content.C...

  • (3.4)James Stewart Calculus 5th

    Derivatives of Trigonometric Functions 三角函数的导数 对应的图像(sinx...

  • Android 图像绘制之 Drawable

    Android 图像绘制系列文章Android图像绘制之BitmapAndroid 图像绘制之 DrawableA...

  • Android 图像绘制之 Drawable(二)

    Android 图像绘制系列文章Android图像绘制之BitmapAndroid 图像绘制之 DrawableA...

  • Java班海草三角形代码

    核心思想是: 在打印出标准图形的情况下(注释部分),横向添加0->n->0个空格。 考虑到规律,选用y=sinx(...

  • 数字图像处理基础

    书籍:数字图像处理(冈萨雷斯) 一、图像的概念 一幅图像可以被定义为一个二维函数 f (x,y),其中 x,y 是...

  • y=lnx

    y=lnx的图像与性质如下

  • 三角函数公式大全

    整理三角函数常用的公式,方便复习。 三角函数的导数 (sinx)' = cosx (cosx)' = - sinx...

  • OpenCV学习之旅-简介

    1、什么是图像,对图像进行处理是神马操作 一副图像可以定义为二维的函数z = f(x,y),其中x、y是其空间坐标...

  • 数字图像的距离变换算法

    一、图像数字化 通过传感器获得的图像是平面坐标(x,y)的连续函数f(x,y),它的值图像对应位置的亮度。为了能够...

网友评论

      本文标题:Android 图像 Y = sinX;

      本文链接:https://www.haomeiwen.com/subject/avclsxtx.html