折线图
作者:互联网
public class MyView extends View {
Paint paint,paint1,paint2,paint3;
public MyView(Context context) {
super(context);
}
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
//X轴
paint=new Paint();
paint.setColor(Color.WHITE);
paint.setStrokeWidth(5f);
paint.setStyle(Paint.Style.FILL);
//Y轴
paint1=new Paint();
paint1.setColor(Color.GREEN);
paint1.setStrokeWidth(5f);
paint1.setStyle(Paint.Style.FILL);
//线段
paint2=new Paint();
paint2.setColor(Color.WHITE);
paint2.setStrokeWidth(5f);
paint2.setStyle(Paint.Style.FILL);
//文字
paint3=new Paint();
paint3.setColor(Color.WHITE);
paint3.setStrokeWidth(5f);
paint3.setStyle(Paint.Style.FILL);
}
public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//X
canvas.drawLine(40,700,500,700,paint);
//Y
canvas.drawLine(40,100,40,700,paint1);
//折线
canvas.drawLine(40,700,60,600,paint2);
canvas.drawLine(60,600,100,650,paint2);
canvas.drawLine(100,650,250,350,paint2);
canvas.drawLine(250,350,280,500,paint2);
canvas.drawLine(280,500,380,300,paint2);
canvas.drawLine(380,300,420,350,paint2);
//文字
canvas.drawText("优",20,650,paint3);
canvas.drawText("100",20,600,paint3);
canvas.drawText("200",20,550,paint3);
canvas.drawText("300",20,500,paint3);
canvas.drawText("400",20,450,paint3);
canvas.drawText("500",20,400,paint3);
canvas.drawText("中",20,350,paint3);
canvas.drawText("600",20,300,paint3);
canvas.drawText("700",20,250,paint3);
canvas.drawText("800",20,200,paint3);
canvas.drawText("500",80,720,paint3);
canvas.drawText("1000",180,720,paint3);
canvas.drawText("1500",280,720,paint3);
canvas.drawText("2000",380,720,paint3);
canvas.drawText("2500",480,720,paint3);
}
}
布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/black"
>
<com.bawei.qinchaomeng20190427.view.MyTitle
android:layout_width="match_parent"
android:layout_height="30dp" />
</LinearLayout>
标签:canvas,20,paint2,paint3,drawText,Paint,折线图 来源: https://blog.csdn.net/weixin_43584998/article/details/89813644