自定义绘图View
package com.example.storeenergy.right5;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.RectF;
import android.util.Log;
import android.view.View;
public class temperatureChartView extends View {
//原点的X坐标
// public float XPoint = 40;
public float XPoint = 50;
// //原点的Y坐标
// public float YPoint = Constant.point.y - 250;
public float YPoint = Constant.point.y + 320;
// public float XScale = 16.0f; //X的刻度长度
public float XScale = 13.0f; //X的刻度长度
public float YScale = 40.0f; //Y的刻度长度
// public float XLength = Constant.point.x - 150; //X轴的长度
// public float YLength = Constant.point.y / 2; //Y轴的长度
public float XLength = 660; //X轴的长度
public float YLength = 260; //Y轴的长度
public String[] XLabel; //X的刻度
public String[] YLabel; //Y的刻度
public float[] Data3; //数据
public String Title; //显示的标题
public float chart1TempValue = 0.0f; //表1临时数据
public temperatureChartView(Context context) {
super(context);
}
public void SetInfo(String[] XLabels, String[] YLabels, float[] AllData, String strTitle, float tempValue) {
XLabel = XLabels;
YLabel = YLabels;
Data3 = AllData;
Title = strTitle;
// XScale = XLength / AllData.length;//实际X的刻度长度
YScale = YLength / YLabels.length;
chart1TempValue = tempValue;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);//重写onDraw方法
//canvas.drawColor(Color.WHITE);//设置背景颜色
/**
* paint
* */
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
//去锯齿
paint.setAntiAlias(true);
//颜色
paint.setColor(Color.BLACK);
paint.setStrokeWidth(3);
//设置轴文字大小
paint.setTextSize(12);
/**
* paint 设置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);
try {
//文字
canvas.drawText(YLabel[i], XPoint - 30, 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);
//设置字体的大小角度等
paint.setTextSize(20);
drawText(canvas, " ", XPoint - 5, YPoint - YLength + YScale - 5, paint, -90);
/**
* paint 设置X轴
* */
paint.setTextSize(12);
//轴线
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 {
// 文字
// drawText(canvas, XLabel[i], XPoint + i * XScale, YPoint + 40, paint, -45);
if (i < 7) {
drawText(canvas, XLabel[i], XPoint + i * 130, YPoint + 20, paint, 0);
}
// 数据值 , 保证有效数据
if (i > 0 && YCoord(Data3[i - 1]) != -999 && YCoord(Data3[i]) != -999) {
for (int j = 0; j < Data3.length; j++) {
paint.setStrokeWidth(12);
//柱状图
canvas.drawLine(XPoint + i * XScale, YPoint, XPoint + i * XScale, YCoord(Data3[i]), paint);
paint.setStrokeWidth(3);
}
//折线图
// canvas.drawLine(XPoint + (i - 1) * XScale, YCoord(Data3[i - 1]), XPoint + i * XScale, YCoord(Data3[i]), paint);
}
//折线图的坐标点画的圆
// canvas.drawCircle(XPoint + i * XScale, YCoord(Data3[i]), 2, paint);
} catch (Exception e) {
Log.d("TAG", "onDraw: 出错了" + i);
e.printStackTrace();
}
}
//箭头
canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6, YPoint - 3, paint);
canvas.drawLine(XPoint + XLength, YPoint, XPoint + XLength - 6, YPoint + 3, paint);
//设置标题位置
paint.setTextSize(20);
String s1 = "时间/200ms";
canvas.drawText(s1, Constant.point.x / 2 - 113, Constant.point.y - 225, paint);
String s2 = "温度值";
canvas.drawText(s2, 15, 25, paint);
}
/**
* 设置文字显示方向
*/
void drawText(Canvas canvas, String text, float x, float y, Paint paintP, float angle) {
if (angle != 0) {
canvas.rotate(angle, x, y);
}
canvas.drawText(text, x, y, paintP);
if (angle != 0) {
canvas.rotate(-angle, x, y);
}
}
private float YCoord(float y0) //计算绘制时的Y坐标,无数据时返回-999
{
float y;
try {
y = y0;
} catch (Exception e) {
return -999; //出错则返回-999
}
try {
return YPoint - y * YScale / Float.parseFloat(YLabel[1]);
} catch (Exception e) {
}
return y;
}
}
调用如下:
package com.example.storeenergy.right5;
import android.graphics.Point;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.storeenergy.R;
public class right5_frag1 extends Fragment {
LinearLayout chartLl1;
//自动变化刻度长度的Y轴
String[] autoYaxis = new String[]{"0", "10", "20", "30", "40", "50"};
//50个温度值
float[] temperature_arr = new float[]{19,2,2,23,3,4,32,6,47,32,6,64,6,7,8,4,3,4,5,7,20,34,34,21,5,5,5,32,23,23,42,32,32,23,14,51,20,31,24,25,24,34,23,14,1,16,16,16,17,30};
temperatureChartView myView;
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.right5_frag1,container,false); //关联布局文件
chartLl1 = view.findViewById(R.id.right5_frag1_chartLl1);
Constant.point = new Point();
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
chartLl1.removeAllViews();
myView = new temperatureChartView(getActivity());
myView.SetInfo(new String[]{"9:11", "9:12", "9:13", "9:14", "9:11",
"9:16", "9:17"}, // X轴刻度
autoYaxis, // Y轴刻度
temperature_arr, // 数据
"频率", 30);
chartLl1.addView(myView);
myView.invalidate();
}
});
return view;
}
}
Constant类如下:
package com.example.storeenergy.right5;
import android.graphics.Point;
public class Constant {
/**
* 获取屏幕的大小
* */
public static Point point;
}