MyView绘图类的对View的重载(android)


        package com.meihao.xingdongrizhi;


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 ChatView extends View {
public int XPoint = 40; // 原点的X坐标
public int YPoint = 460; // 原点的Y坐标
public int XScale = 12; // X的刻度长度
public int YScale = 60; // Y的刻度长度
public int XLength = 380; // X轴的长度
public int YLength = 360; // Y轴的长度
public String[] XLabel; // X的刻度
public String[] YLabel; // Y的刻度
public int[] Data; // 数据
public String Title; // 显示的标题
private float screenW, screenH;
private float lastX;
private float lastY;
private float total_Width = 0;


public ChatView(Context context, AttributeSet attrs) {
super(context, attrs);
screenW = this.getWidth();
screenH = this.getHeight();
// TODO Auto-generated constructor stub
}
public void SetInfo(String[] XLabels, String[] YLabels, int[] AllData,
String strTitle) {
XLabel = XLabels;
YLabel = YLabels;
Data = AllData;
Title = strTitle;
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);// 重写onDraw方法

System.out.println("3");
// canvas.drawColor(Color.WHITE);//设置背景颜色
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setAntiAlias(true);// 去锯齿
paint.setColor(Color.BLACK);// 颜色
Paint paint1 = new Paint();
paint1.setStyle(Paint.Style.STROKE);
paint1.setAntiAlias(true);// 去锯齿
paint1.setColor(Color.DKGRAY);
paint.setTextSize(12); // 设置轴文字大小
// 设置Y轴(对于系统来讲屏幕的原点在左上角)
canvas.drawLine(XPoint, YPoint - YLength, XPoint, YPoint, paint); // 轴线
for (int i = 0; i * YScale < YLength; i++) {
canvas.drawLine(XPoint, YPoint - i * YScale, XPoint + 5, YPoint - i
* YScale, paint); // 刻度 XPoint+5画出了一条短的小横线
try {
canvas.drawText(YLabel[i], XPoint - 22,
YPoint - i * YScale + 5, paint); // 文字
} catch (Exception e) {
}
}
canvas.drawLine(XPoint, YPoint - YLength, XPoint - 3, YPoint - YLength
+ 6, paint); // 箭头
canvas.drawLine(XPoint, YPoint - YLength, XPoint + 3, YPoint - YLength
+ 6, paint);
// 设置X轴
canvas.drawLine(XPoint, YPoint, XPoint + XLength, YPoint, paint); // 轴线
for (int i = 0; i * XScale < XLength; i++) {
canvas.drawLine(XPoint + i * XScale, YPoint, XPoint + i * XScale,
YPoint - 5, paint); // 刻度
try {
canvas.drawText(XLabel[i], XPoint + i * XScale - 10,
YPoint + 20, paint); // 文字
// 数据值
if (i > 0 && YCoord(Data[i - 1]) != -999
&& YCoord(Data[i]) != -999) // 保证有效数据
canvas.drawLine(XPoint + (i - 1) * XScale,
YCoord(Data[i - 1]), XPoint + i * XScale,
YCoord(Data[i]), paint);
canvas.drawCircle(XPoint + i * XScale, YCoord(Data[i]), 2,
paint);
} catch (Exception e) {
}
}
canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6,
YPoint - 3, paint); // 箭头
canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6,
YPoint + 3, paint);
   paint.setTextSize(16);
canvas.drawText(Title, 150, 50, paint);
}


private int YCoord(int y0) // 计算绘制时的Y坐标,无数据时返回-999
{
int y;
try {
y = y0;
} catch (Exception e) {
return -999; // 出错则返回-999
}
try {
return YPoint - y * YScale / Integer.parseInt(YLabel[1]);
} catch (Exception e) {
}
return y;
}


}


注:view类的重载之后,可以做为一个像imageview的应用方式应用


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值