天气折线图

天气预报的App中都会存在气温变化的折线图,自定义了一个天气的折线图只要传入气温数组就能显示


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

public class ChartView extends View {
	public int XPoint; // 原点的X坐标
	public int XScale; // X的刻度长度
	public int YScale; // Y的刻度长度
	public int Trange;
	public int Ysw;
	public int ToLength;
	public int maxLength;
	public int minLength;
	public int radiu;
	public int size;
	private int[] max;// 折线的转折点
	private int[] min;// 折线的转折点
	public Paint paint;
	public Paint paintText;
	public Paint paint1;
	private int Ysw1;
	private Paint paintFirst;
	private Paint paintFirst1;
	private int radiu1;

	public ChartView(Context context) {
		super(context);
		init(context);
	}

	public ChartView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context);
	}

	public void setData(int[] max, int[] min) {
		if (max != null && max.length == 5) {
			this.max = max;
		}
		if (min != null && min.length == 5) {
			this.min = min;
		}
		postInvalidate();
	}

	private void init(Context context) {
		//ScreenUtil是dip转化px的工具类
		size = ScreenUtil.sp2px(context, 12);
		radiu = ScreenUtil.dip2px(context, 3);
		radiu1 = ScreenUtil.dip2px(context, 4);
		Trange = ScreenUtil.dip2px(context, 22);
		int strokeWidth = ScreenUtil.dip2px(context, 2);
		//第一条折线的实心园的画笔
		paint = new Paint();
		paint.setStrokeWidth(strokeWidth);
		paint.setStyle(Paint.Style.FILL);
		paint.setAntiAlias(true);// 去锯齿
		paint.setColor(getResources().getColor(R.color.broken_line_orange));// 颜色
		//第一条折线的空心园的画笔
		paintFirst = new Paint();
		paintFirst.setStrokeWidth(strokeWidth);
		paintFirst.setStyle(Paint.Style.STROKE);
		paintFirst.setAntiAlias(true);// 去锯齿
		paintFirst.setColor(getResources().getColor(R.color.broken_line_orange));// 颜色
		//文字的画笔
		paintText = new Paint();
		paintText.setStyle(Paint.Style.STROKE);
		paintText.setAntiAlias(true);// 去锯齿
		paintText.setColor(getResources().getColor(R.color.white));// 颜色
		paintText.setTextSize(size); // 设置轴文字大小
		//第二条折线的实心园的画笔
		paint1 = new Paint();
		paint1.setStrokeWidth(strokeWidth);
		paint1.setStyle(Paint.Style.FILL);
		paint1.setAntiAlias(true);// 去锯齿
		paint1.setColor(getResources().getColor(R.color.broken_line_azure));// 颜色
		//第二条折线的空心园的画笔
		paintFirst1 = new Paint();
		paintFirst1.setStrokeWidth(strokeWidth);
		paintFirst1.setStyle(Paint.Style.STROKE);
		paintFirst1.setAntiAlias(true);// 去锯齿
		paintFirst1.setColor(getResources().getColor(R.color.broken_line_azure));// 颜色
		
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);// 重写onDraw方法
		if (max == null || min == null) {
			return;
		}
		XScale = getWidth() / 5;
		YScale = getHeight();
		XPoint = XScale / 2;
		ToLength = (YScale - Trange * 2) / 8;
		maxLength = Trange + ToLength;
		if (getMax(max) - getMin(max) == 0 || getMax(min) - getMin(min) == 0) {
			Ysw = 0;
			Ysw1 = 0;
		} else {
			Ysw = (YScale - Trange * 2) / (4 * (getMax(max) - getMin(max)));
			Ysw1 = (YScale - Trange * 2) / (4 * (getMax(min) - getMin(min)));
		}
		minLength = YScale / 2 + ToLength;
		for (int i = 0; i < max.length; i++) {
			if (i == 0) {
				canvas.drawCircle(XPoint + XScale * i, maxLength + Ysw
						* (getMax(max) - max[i]), radiu, paintFirst);
			}else{
				canvas.drawCircle(XPoint + XScale * i, maxLength + Ysw
						* (getMax(max) - max[i]), radiu1, paint);
			}
			canvas.drawText(String.valueOf(max[i]) + "°", XPoint + XScale * i
					- size / 2, maxLength + Ysw * (getMax(max) - max[i]) - ScreenUtil.dip2px(getContext(), 10),
					paintText);
			if (i > 0) {
				canvas.drawLine(XPoint + XScale * (i - 1) + radiu, maxLength
						+ Ysw * (getMax(max) - max[i - 1]), XPoint + XScale * i
						- radiu, maxLength + Ysw * (getMax(max) - max[i]),
						paint);
			}
		}
		for (int i = 0; i < min.length; i++) {
			if (i == 0) {
				canvas.drawCircle(XPoint + XScale * i, minLength + Ysw1
						* (getMax(min) - min[i]), radiu, paintFirst1);
			}else{
				canvas.drawCircle(XPoint + XScale * i, minLength + Ysw1
						* (getMax(min) - min[i]), radiu1, paint1);
			}
			canvas.drawText(String.valueOf(min[i]) + "°", XPoint + XScale * i
					- size / 2, minLength + Ysw1 * (getMax(min) - min[i])
					+ ScreenUtil.dip2px(getContext(), 20), paintText);
			if (i > 0) {
				canvas.drawLine(XPoint + XScale * (i - 1) + radiu, minLength
						+ Ysw1 * (getMax(min) - min[i - 1]), XPoint + XScale
						* i - radiu, minLength + Ysw1 * (getMax(min) - min[i]),
						paint1);
			}
		}
	}

	public int getMin(int[] arr) {
		int min = arr[0];

		for (int i = 1; i < arr.length; i++) {
			if (arr[i] < min)
				min = arr[i];
		}

		return min;
	}

	public int getMax(int[] arr) {
		int max = arr[0];

		for (int i = 1; i < arr.length; i++) {
			if (arr[i] > max) {
				max = arr[i];
			}
		}

		return max;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值