android绘制折线图

最近公司做的项目中要用到折线图,网上看了下都不是很能满足自己的需要,因此就自己写了个,废话不说了,直接上代码

package com.example.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.Scroller;

import com.example.aa.R;

/**
* LastTNT
* @author 翟昆
* @date 2014-5-6 上午10:28:53
* @version 1.0
*/
public class LineChartView extends View {

	private int mov_x; // 移动X坐标
	private int mov_y; // 移动Y坐标
	private int diff_Y; // Y轴高度
	private Paint paint;// 声明画笔
	private int color_xy = Color.rgb(77, 189, 235);//设置标线颜色
	private int line_color_y = Color.rgb(18, 197, 255);//标注线的颜色
	private int zhexian_color = Color.rgb(21, 139, 229);//折线的颜色
	private int Guides_line_color = Color.rgb(229, 21, 60);//参考线的颜色
	private int text_color = Color.rgb(254, 102, 1);//折线字体颜色
	private int width_xy = 4;//设置标线宽度
	private int line_width_y = 2;//标注线的宽度
	private int zhexian_width = 4;//折线的宽度
	int init_x = 0;//初始按下的X坐标
	int init_left = 0;//左边距
	private int MAX_X = 2000;
	private int MAX_X_Extend = 100;//向右侧继续延伸100
	private int MAX_Y = 100;//Y轴最大值
	private int lineNum = 24; //标线份数
	private int lineInterval_x =0;//标线间隔
	private int lineInterval_y = 0;//标注线间隔
	private int width = 0;//控件宽度
	private int height = 0;//控件高度
	private Bitmap bmp;//标记点的图
	private int Guides_line = 80;//参考线
	private float textSize = 22;//折线图字体大小
	private float CalloutSize = 18;//标注字体大小
	private PaintFlagsDrawFilter pfd;
	private Matrix matrix = new Matrix();
	private int paddingBottom = 50; 
	private int paddingTop = 10;
	private int[] nums = new int[24];
	
	//实现惯性滑动效果
	private Scroller mScroller;
	private int mTouchSlop;
	private int mMinimumVelocity;
	private int mMaximumVelocity;
	
	public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
	}

	public LineChartView(Context context) {
		super(context);
	}

	public LineChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		pfd = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG
				| Paint.FILTER_BITMAP_FLAG);
		paint.setAntiAlias(true);
		matrix.postScale(1f, 1f);
		lineInterval_x = MAX_X/lineNum;
		bmp = BitmapFactory.decodeResource(getResources(), R.drawable.point); 
		for (int i = 0; i < nums.length; i++) {
			nums[i] = (int) (Math.random()*MAX_Y);
		}
	}
	
//	void init(Context context) {
//		mScroller = new Scroller(getContext());
//		setFocusable(true);
//		setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
//		setWillNotDraw(false);
//		final ViewConfiguration configuration = ViewConfiguration.get(context);
//		mTouchSlop = configuration.getScaledTouchSlop();
//		mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
//		mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
//
//	}

	@Override
	protected void onDraw(Canvas canvas) {
		if(init_left==0){
			init_left = this.getLeft();
		}
		if(width==0){
			width = this.getRight()-this.getLeft();
			height = this.getBottom() - this.getTop();
			lineInterval_y = (height*2/3)/MAX_Y;
		}
		diff_Y = this.getBottom() - this.getTop();
		
		// 设置画布颜色 也就是背景颜色
		canvas.drawColor(Color.TRANSPARENT);
		paint.setColor(Color.BLACK);
		canvas.drawText("绘制无规则几何图形喔!!!", 150, 30, paint);
		
		//绘制X坐标轴
		paint.setColor(color_xy);
		paint.setStrokeWidth(width_xy);
		canvas.drawLine(paddingBottom, diff_Y-paddingBottom, MAX_X+paddingBottom+MAX_X_Extend, diff_Y-paddingBottom, paint);

		//绘制Y坐标轴
		paint.setColor(color_xy);
		paint.setStrokeWidth(width_xy);
		canvas.drawLine(paddingBottom, diff_Y-paddingBottom, paddingBottom, this.getTop()+paddingTop, paint);
		
		//绘制标注线
		for (int i = 1; i <=lineNum; i++) {
			paint.setColor(line_color_y);
			paint.setStrokeWidth(line_width_y);
			canvas.drawLine(paddingBottom+lineInterval_x*i, diff_Y-paddingBottom, paddingBottom+lineInterval_x*i, this.getTop()+paddingTop, paint);
			//绘制标注线下的字体
			paint.setColor(Color.BLACK);
			paint.setTextSize(CalloutSize);
			paint.setTextScaleX(1.0f);//设置文本缩放
			paint.setTypeface(Typeface.SERIF);
			canvas.drawText("6日"+i+"时", paddingBottom+lineInterval_x*i-20, diff_Y-paddingBottom+20, paint);
		}
		
		//绘制参考线
		paint.setColor(Guides_line_color);
		paint.setStrokeWidth(width_xy);
		canvas.drawLine(paddingBottom, diff_Y-paddingBottom-lineInterval_y*Guides_line, MAX_X+paddingBottom+MAX_X_Extend, diff_Y-paddingBottom-lineInterval_y*Guides_line, paint);
		//绘制参考线标注
		paint.setColor(text_color);
		paint.setTextSize(textSize);
		paint.setTextScaleX(1.0f);//设置文本缩放
		paint.setTypeface(Typeface.SERIF);
		canvas.drawText(Guides_line+"", paddingBottom-30, diff_Y-paddingBottom-lineInterval_y*Guides_line+5, paint);
		
		//绘制折线
		for (int i = 1; i < nums.length; i++) {
			int temp = nums[i-1];
			if(temp==0){
				temp = 10;
			}
			int temp1 = nums[i];
			if(temp1==0){
				temp1 = 10;
			}
			paint.setColor(zhexian_color);
			paint.setStrokeWidth(zhexian_width);
			canvas.drawLine(paddingBottom + lineInterval_x * i,
					(diff_Y - paddingBottom) - (lineInterval_y * temp), paddingBottom+lineInterval_x*(i+1), (diff_Y-paddingBottom)-(lineInterval_y*temp1), paint);
		}
		//绘制折线点
		for (int i = 1; i <= nums.length; i++) {
			int temp = nums[i-1];
			if(temp==0){
				temp = 10;
			}
			// 绘制一个圆形
			canvas.drawBitmap(bmp, paddingBottom+lineInterval_x*i-8, (diff_Y-paddingBottom)-(lineInterval_y*temp)-5, paint);
			
			paint.setColor(text_color);
			paint.setTextSize(textSize);
			paint.setTextScaleX(1.0f);//设置文本缩放
			//设置字体样式 Typeface.DEFAULT:默认字体;Typeface.DEFAULT_BOLD:加粗字体;Typeface.MONOSPACE:monospace;Typeface.SANS_SERIF:sans;Typeface.SERIF:serif
			paint.setTypeface(Typeface.SERIF);
			canvas.drawText(temp+"", paddingBottom+lineInterval_x*i-8, (diff_Y-paddingBottom)-(lineInterval_y*temp)-10, paint);
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		mov_x = (int) event.getX();
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			break;
		case MotionEvent.ACTION_DOWN:
			init_x = (int) event.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			int diff_left = this.getLeft() + mov_x - init_x;
			int diff_right = MAX_X+paddingBottom+MAX_X_Extend-width;//向左滑动的最大距离
			if (diff_left < init_left) {
				if(diff_right>0&&diff_left>-diff_right){
					this.setLeft(diff_left);
				}else{
					this.setLeft(-diff_right);
				}
			}else{
				this.setLeft(init_left);
			}
			break;
		}
		return true;
	}
}


你可能感兴趣的:(android,折线图)