您的位置:首页 > 其它

使用canvas画曲线

2015-07-06 10:36 197 查看
Draw类

package com.example.drawtestone;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class Draw extends View{

	//line paint
	Paint paint = new Paint();
	//x y axis paint
	Paint paintPoint = new Paint();
	//path paint
	Paint paintPath = new Paint();
	//x y axis unit
	private float xUnit,yUnit;
	//x y axis start
	private float xStart,yStart;
	//x y end start
	private float xEnd,yEnd;
	
	public Draw(Context context) {
		super(context);
	}
	public Draw(Context context,AttributeSet attrs) {
		super(context,attrs);
		// TODO Auto-generated constructor stub
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);

		canvas.drawColor(Color.WHITE);
		paint.setColor(Color.BLUE);
		paintPoint.setColor(Color.RED);
		paintPath.setColor(Color.BLACK);
		paintPath.setStyle(Paint.Style.STROKE);
		
		Path path = new Path();
		path.moveTo(100, 100);
		path.quadTo(500, 300, 300, 400);
		path.quadTo(200, 400, 500, 600);
		
		canvas.drawPath(path, paintPath);
		
		
		xUnit = 50;
		yUnit = 50;
		xStart = 40;
		yStart = 0;
		xEnd = 500;
		yEnd = 1000;
		
		//y axis draw
		canvas.drawLine(xStart, yStart, xStart, yEnd, paint);
		//x axis draw
		drawXAxis(canvas,xStart ,xEnd ,yStart ,yEnd ,yUnit);
		
		//draw axis text
		drawYText(canvas,paintPoint);
		drawXText(canvas,paintPoint);
	
		
	}
	
	/**
	 * get before data
	 * @param c Calendar object
	 * @param num want to get the number of days
	 * @return ArrayLsit of days
	 */
	public ArrayList<String> getBeforDay(Calendar c,int num)
	{
		ArrayList<String> dataArr = new ArrayList<String>();
		
		while(num >= 0)
		{
			int month = c.get(Calendar.MONTH)+1;
			int day = c.get(Calendar.DATE);
			String data = month + "/" + day;
			dataArr.add(data);
			//set before day
			c.set(Calendar.DATE, day-1);
			num--;
		}
		
		return dataArr;
	}

	/**
	 * draw x axis text
	 * @param canvas 
	 * @param paintPoint
	 */
	public void drawXText(Canvas canvas,Paint paintPoint)
	{
		Calendar c = Calendar.getInstance();
		ArrayList<String> dataArr = getBeforDay(c, 7);
		int dataIndex = dataArr.size()-1;
		int x = 30;
		while(x <= 450 && dataIndex >= 0)
		{
			canvas.drawText(dataArr.get(dataIndex), x, 1020, paintPoint);
			x += xUnit;
			dataIndex--;
		}
	}
	
	/**
	 * draw y axis text
	 * @param canvas
	 * @param paintPoint
	 */
	public void drawYText(Canvas canvas,Paint paintPoint)
	{

		float yText = 50.0f;
		int y = 0;
		//save one decimal point
		DecimalFormat df = new DecimalFormat("##0.0");
		String text = df.format(yText);
		while(y <= 1000)
		{
			canvas.drawText(text + "", 0, 1000-y, paintPoint);
			y += yUnit;
			yText += 0.1f;
			text = df.format(yText);
		}
	}
	
	/**
	 * draw every x axis
	 * @param canvas
	 * @param xStart
	 * @param xEnd
	 * @param yStart
	 * @param yEnd
	 * @param yUnit
	 */
	public void drawXAxis(Canvas canvas ,float xStart ,float xEnd ,float yStart ,float yEnd ,float yUnit)
	{
		
		do
		{
			canvas.drawLine(xStart, yStart, xEnd, yStart, paint);
			yStart += yUnit;
			
		}while(yStart <= yEnd);
		
	}
	
	public String getTime()
	{
		Date date = new Date();
		SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd");
		String time = dateFormat.format(date);
		return time;
	}

}
layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.drawtestone.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="draw" />

    <com.example.drawtestone.Draw
        android:id="@+id/draw1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Main

package com.example.drawtestone;

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		LinearLayout linearlayout = (LinearLayout)findViewById(R.id.linearlayout);
		Draw draw = new Draw(this);
		draw.setMinimumHeight(10000);
		draw.setMinimumWidth(10000);
//		draw.setScaleX(10.0f);
//		draw.setScaleY(10.0f);
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: