自定义view统计趋势图效果
公司需求要做一个记步的统计图表,在网上找了很多例子没有发现这样的效果,用了MPchart 功能强大,可我不是很会用效果不理想,所有就有了这个博客,
这是最终实现效果,
废话不多说直接上代码,,这是自定义view
package com.ormdb.ondraw;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class myView extends View {
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private int y = 720;
private int distance = 140;
private int heigth = 1000;
private int[] day = {8, 7, 6, 2, 5, 6, 2};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path = new Path();
path.lineTo(50, 1000);
path.lineTo(50, day[0]*75+100);
path.lineTo(120, day[0]*75+100);
int weigth = 120;
for (int i = 1; i < day.length; i++) {
weigth = weigth + 140;
path.lineTo(weigth, day[i] * 75 + 100);
}
path.lineTo(1030, day[6]*75+100);
path.lineTo(1030, 1000);
path.lineTo(50, 1000);
path.close();
Paint mField = new Paint();
mField.setAntiAlias(true);
mField.setARGB(200, 255, 215, 5);//设置封闭路径的填充色为金***
canvas.drawPath(path, mField);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.parseColor("#ffffff"));
paint.setStrokeWidth(4);
int temp =140;
canvas.drawLine(138+50,heigth,138+50,200,paint);
for (int i = 2; i < 7; i++) {
canvas.drawLine(temp*i+50,heigth,temp*i+50,200,paint);
}
Path path1 = new Path();
path1.lineTo(614,1000);
path1.lineTo(614,100+(day[3]+day[4])/2*75+36);
path1.lineTo(676,100+day[4]*75-2);
path1.lineTo(746,100+(day[4]+day[5])/2*75+36);
path1.lineTo(746,1000);
path1.lineTo(614,1000);
path1.close();
Paint mField1 = new Paint();
mField1.setAntiAlias(true);
mField1.setColor(Color.parseColor("#dc4935"));
canvas.drawPath(path1, mField1);
}
}
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.View;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class myView extends View {
public myView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private int y = 720;
private int distance = 140;
private int heigth = 1000;
private int[] day = {8, 7, 6, 2, 5, 6, 2};
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Path path = new Path();
path.lineTo(50, 1000);
path.lineTo(50, day[0]*75+100);
path.lineTo(120, day[0]*75+100);
int weigth = 120;
for (int i = 1; i < day.length; i++) {
weigth = weigth + 140;
path.lineTo(weigth, day[i] * 75 + 100);
}
path.lineTo(1030, day[6]*75+100);
path.lineTo(1030, 1000);
path.lineTo(50, 1000);
path.close();
Paint mField = new Paint();
mField.setAntiAlias(true);
mField.setARGB(200, 255, 215, 5);//设置封闭路径的填充色为金***
canvas.drawPath(path, mField);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.parseColor("#ffffff"));
paint.setStrokeWidth(4);
int temp =140;
canvas.drawLine(138+50,heigth,138+50,200,paint);
for (int i = 2; i < 7; i++) {
canvas.drawLine(temp*i+50,heigth,temp*i+50,200,paint);
}
Path path1 = new Path();
path1.lineTo(614,1000);
path1.lineTo(614,100+(day[3]+day[4])/2*75+36);
path1.lineTo(676,100+day[4]*75-2);
path1.lineTo(746,100+(day[4]+day[5])/2*75+36);
path1.lineTo(746,1000);
path1.lineTo(614,1000);
path1.close();
Paint mField1 = new Paint();
mField1.setAntiAlias(true);
mField1.setColor(Color.parseColor("#dc4935"));
canvas.drawPath(path1, mField1);
}
}
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<com.ormdb.ondraw.myView
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
代码比较简单,还请大神多多指导,本人第一次写博客,轻喷,