本文介绍了Android 曲线图的绘制示例代码,分享给大家,具体如下:
效果展示
效果展示.gif
使用方式
// 初始化数据表格相关 with(mTableView) { // 配置坐标系 setupCoordinator("日", "人", /*这里是横坐标的值*/0f, 5f, 10f, 15f, 20f, 25f, 30f) // 添加曲线, 确保纵坐标的数值位数相等 addWave(ContextCompat.getColor(this@MainActivity, R.color.colorYellow), false, 0f, 10f, 30f, 54f, 30f, 100f, 10f) addWave(ContextCompat.getColor(this@MainActivity, R.color.colorGreen), false, 0f, 30f, 20f, 20f, 46f, 25f, 5f) addWave(ContextCompat.getColor(this@MainActivity, R.color.colorPink), false, 0f, 30f, 20f, 50f, 46f, 30f, 30f) addWave(Color.parseColor("#8596dee9"), true, 0f, 15f, 10f, 10f, 40f, 20f, 5f) }
实现思路
代码实现
/** * Created by FrankChoo on 2017/12/29. * Email: frankchoochina@gmail.com * Version: 1.0 * Description: 表格自定义View */ public class TableView extends View { private ListmWaves;// 数值集合 // 坐标轴的数值 private int mCoordinateYCount = 8; private float[] mCoordinateXValues;// 外界传入 private float[] mCoordinateYValues;// 动态计算 // 坐标的单位 private String mXUnit; private String mYUnit; // 所有曲线中所有数据中的最大值 private float mGlobalMaxValue;// 用于确认是否需要调整坐标系 private Paint mCoordinatorPaint; private Paint mTextPaint; private Paint mWrapPaint; // 坐标轴上描述性文字的空间大小 private int mTopUnitHeight;// 顶部Y轴单位高度 private int mBottomTextHeight; private int mLeftTextWidth; // 网格尺寸 private int mGridWidth, mGridHeight; private float mAnimProgress; public TableView(Context context) { this(context, null); } public TableView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public TableView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); post(new Runnable() { @Override public void run() { showAnimator(); } }); } private void init() { // 初始化数据集合的容器 mWaves = new ArrayList<>(); // 坐标系的单位 mBottomTextHeight = dp2px(40);// X轴底部字体的高度 mLeftTextWidth = mBottomTextHeight;// Y轴左边字体的宽度 mTopUnitHeight = dp2px(30);// 顶部Y轴的单位 // 初始化坐标轴Paint mCoordinatorPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); mCoordinatorPaint.setColor(Color.LTGRAY); // 初始化文本Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); mTextPaint.setColor(Color.GRAY); mTextPaint.setTextSize(sp2px(12)); // 初始化曲线Paint mWrapPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DITHER_FLAG); mWrapPaint.setPathEffect(new CornerPathEffect(200f)); } /** * 配置坐标轴信息 * * @param xUnit X 轴的单位 * @param yUnit Y 轴的单位 * @param coordinateXValues X 坐标轴上的数值 */ public void setupCoordinator(String xUnit, String yUnit, float... coordinateXValues) { mXUnit = xUnit; mYUnit = yUnit; mCoordinateXValues = coordinateXValues; } /** * 添加一条曲线, 确保与横坐标的数值对应 * * @param color * @param isCoverRegion * @param values */ public void addWave(int color, boolean isCoverRegion, float... values) { mWaves.add(new WaveConfigData(color, isCoverRegion, values)); // 根据value的值去计算纵坐标的数值 float maxValue = 0; for (float value : values) { maxValue = Math.max(maxValue, value); } if (maxValue
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。