码迷,mamicode.com
首页 > 其他好文 > 详细

贝塞尔曲线

时间:2015-02-05 07:02:14      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:

学习了很久andriod,今天第一次写自定义控件;

package com.zy.yh.surfacedemo.myview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

public class CurveLineView extends View {
    private Context context;
    // 贝赛尔曲线成员变量(起始点,控制(操作点),终止点,3点坐标)
    // private int startX = 100, startY = 100, controlX = 260, controlY = -30,
    // endX = 420, endY = 100;
    // 为了不影响主画笔,这里绘制贝赛尔曲线单独用一个新画笔
    private Paint mpaintQ;
    // Path
    private Path mpath;

    Object[][] ints = null;

    public CurveLineView(Context context, Object[][] ints) {
        super(context);
        this.context = context;
        mpaintQ = new Paint();
        mpaintQ.setColor(Color.GREEN);// 画笔为绿色
        mpaintQ.setStrokeWidth(7);// 设置画笔粗细
        mpaintQ.setAntiAlias(true);
        mpaintQ.setStyle(Style.STROKE);
        mpath = new Path();
        this.ints = ints;
    }

    public CurveLineView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }

    public CurveLineView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * * 绘制贝赛尔曲线
     * 
     * @param canvas
     *            主画布
     * @param startX
     * 
     * @param startY
     * @param controlX
     * @param controlY
     * @param endX
     * @param endY
     */
    public void drawQpath(Canvas canvas, int startX, int startY, int controlX,
            int controlY, int endX, int endY, String value, String time,
            Object colorValue) {
        mpath.reset();// 重置path
        // 贝赛尔曲线的起始点
        mpath.moveTo(startX, startY);
        // 设置贝赛尔曲线的操作点以及终止点
        mpath.quadTo(controlX, controlY, endX, endY);
        Paint p = new Paint();
        // 绘制贝赛尔曲线(Path)
        p.setColor(Color.BLACK);
        // 设置值
        p.setTextSize(28);
        canvas.drawText(value, startX - 30, startY - 30, p);
        // 画曲线
        canvas.drawPath(mpath, mpaintQ);
        p.setColor(Color.WHITE);
        // 画外面的员
        canvas.drawCircle(startX, startY, 18, p);
        p.setColor(Color.WHITE);
        p.setStrokeWidth(4);
        // 画竖线
        canvas.drawLine(startX, startY, startX, 400, p);
        p.setColor((Integer) colorValue);
        canvas.drawCircle(startX, startY, 14, p);
        // 设置时间
        p.setColor(Color.BLACK);
        p.setTextSize(28);
        canvas.drawText(time, startX - 30, 430, p);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawColor(Color.GRAY);

        int OldX = 0;
        int OldY = 0;
        for (int i = 0; i < ints.length; i++) {
            int startX = (Integer) ints[i][0];
            int startY = (Integer) ints[i][1];
            int endX = (Integer) ints[i][2];
            int endY = (Integer) ints[i][3];
            String value = (String) ints[i][4];
            String time = (String) ints[i][5];

            //建立一个模拟点,作为控制点
            //X    轴
            int simulationX = startX + (endX - startX) / 3;
            int simulationY = startY;

            drawQpath(canvas, startX, startY, simulationX, simulationY, endX,
                    endY, value, time, ints[i][6]);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        Toast.makeText(context, "点击了界面", 0).show();

        return super.onTouchEvent(event);

    }
}

 

贝塞尔曲线

标签:

原文地址:http://www.cnblogs.com/YangKaifeng/p/4273804.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!