Android 函数绘画思路 Y=sinX 绘制过程;

代码解决一切废话,内部注解详细:

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值