欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android自定义View-简单折线图

程序员文章站 2022-05-30 22:50:28
...

先看效果图:

Android自定义View-简单折线图

折线图其实就是对坐标进行计算,实现很简单,下面直接上代码:

package com.example.customeview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import java.util.ArrayList;

/**
 * 自定义折线图
 */
public class MyView extends View {

    private Paint paint;
//    private int color;
    private ArrayList<Integer> mPoint;
    private ArrayList<String> xText = new ArrayList<>();

    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
//        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.myView);
//        color = typedArray.getColor(R.styleable.myView_TextColor, Color.BLUE);
        paint = new Paint();
        paint.setStrokeWidth(2);
//        typedArray.recycle();
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        xText.add("一");
        xText.add("二");
        xText.add("三");
        xText.add("四");
        xText.add("五");
        xText.add("六");
        xText.add("七");
        xText.add("八");

        paint.setColor(Color.BLACK);
        paint.setAntiAlias(true);
        paint.setAntiAlias(true);
        paint.setDither(true);
        canvas.drawLine(50,500,850,500,paint);
        canvas.drawLine(850, 500, 840, 490, paint);
        canvas.drawLine(850, 500, 840, 510, paint);

        canvas.drawLine(50,500,50,0,paint);
        canvas.drawLine(50, 0, 40, 10, paint);
        canvas.drawLine(50, 0, 60, 10, paint);

        for (int i = 0; i < 8; i++) {
            canvas.drawLine(50 + 100*i, 500, 50 + 100*i, 490, paint);
        }

        for (int i = 0; i < 10; i++) {
            canvas.drawLine(50, 50*i, 50 + 10, 50*i, paint);
        }

        paint.setColor(Color.RED);

        if (mPoint.size() != 0){
            for (int i = 0; i < mPoint.size(); i++) {
                if (i == 0){
                    canvas.drawLine(50 + i*100, 500, 50 + i*100 + 100, 500 - mPoint.get(i)*5, paint);
                    canvas.drawCircle(50 + i*100 + 100, 500 - mPoint.get(i)*5,5, paint);
                }
                else{
                    canvas.drawLine(50 + i*100, 500 - mPoint.get(i - 1)*5, 50 + i*100 + 100, 500 - mPoint.get(i)*5, paint);
                    canvas.drawCircle(50 + i*100 + 100, 500 - mPoint.get(i)*5,5, paint);
                }
            }
        }

        paint.setTextSize(25);
        paint.setColor(Color.BLACK);
        for (int i = 0; i < 8; i++) {
            canvas.drawText(xText.get(i), 40+ 100*i, 540 , paint);
        }

        for (int i = 0; i < 10; i++) {
            canvas.drawText(10*i + "", 10, 510 - 50*i , paint);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    }

    public void setmPoint(ArrayList<Integer> point){
        mPoint = point;
        postInvalidate();
    }
}

布局中代码:

<com.example.customeview.MyView
        android:id="@+id/custom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        />

在Activity中设置值:

custom = findViewById(R.id.custom);

        mPoint.add(10);
        mPoint.add(50);
        mPoint.add(70);
        mPoint.add(90);
        mPoint.add(60);
        mPoint.add(0);
        custom.setmPoint(mPoint);