自定义控件 数学坐标系图表 支持坐标点交互

QQ图片20210514174438.jpg

public class WorkingDiagramView extends View {
private Context context;
private Canvas canvas; ///< 画布
private Paint paint1; ///< 画笔
private Paint paint2; ///< 画笔
private int Ox = 50, Oy = 50;///O
private boolean isDIAN = false;

private List<WorkingDiagramData> list;



public void setOXY(int Ox, int Oy) {
    this.Ox = Ox;
    this.Oy = Oy;
}

public WorkingDiagramView(Context context) {
    this(context, null);
}

public WorkingDiagramView(Context context, @Nullable AttributeSet attrs) {
    this(context, attrs, 0);
}

public WorkingDiagramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    this.context = context;
    initPaint();

}

public void initPaint() {
    list=new ArrayList<>();
    paint1 = new Paint();
    paint1.setColor(context.getResources().getColor(R.color.black_54));
    paint1.setStrokeWidth(3);                        //粗细
    paint1.setAntiAlias(true);
    paint1.setTextSize(dip2px(context, 12));
    paint1.setTextAlign(Paint.Align.CENTER);
    paint2 = new Paint();
    paint2.setStyle(Paint.Style.STROKE);
    paint2.setStrokeWidth(1);
    paint2.setPathEffect(new DashPathEffect(new float[]{dip2px(context, 18), dip2px(context, 6)}, 0));
    paint2.setColor(context.getResources().getColor(R.color.black_26));
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Draw(canvas);
    if(isDIAN){
        for (WorkingDiagramData data:
             list) {
            float x = (data.getX() *wc*50 / 4096)+Ox;
            float y = (getHeight() - Oy )- (data.getY() * hc*50 / 27);
            data.setPx(x);
            data.setPy(y);

            if(data.isChecked()) {
                int innerCircle = dip2px(context, 1); //内圆半径
                int ringWidth = dip2px(context, 4);   //圆环宽度
                paint2.setColor(context.getResources().getColor(R.color.white));
                canvas.drawCircle(x, y, innerCircle, paint2);
                paint2.setColor(context.getResources().getColor(R.color.deep_orange_a400));
                canvas.drawCircle(x, y, innerCircle + 1 +ringWidth, paint2);
                paint2.setColor(context.getResources().getColor(R.color.white));
                canvas.drawCircle(x, y, innerCircle + ringWidth, paint2);
                canvas.drawText("(" + (int) data.getX() + "," + data.getY() + ")", x, y + 15, paint1);
                canvas.drawText(data.getText(), x, y + 30, paint1);
            }
            canvas.drawPoint(x, y, paint1);
            paint2.setColor(context.getResources().getColor(R.color.black_26));
        }

    }

}

public static int dip2px(Context context, float dipValue) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dipValue * scale + 0.5f);
}

private int wc,hc;
public void val2xy(float x, float y,String text) {
    list.add(new WorkingDiagramData(x,y,false,text));
    isDIAN=true;
    invalidate();

}

public void Draw(Canvas canvas){
    canvas.drawColor(context.getResources().getColor(R.color.white));
    canvas.drawLine(Ox, 0, Ox, getHeight() - Oy, paint1);//4096
    canvas.drawLine(Ox, getHeight() - Oy, getWidth(), getHeight() - Oy, paint1);//27
    hc = (getHeight() - Oy) / 50;
    for (int i = 1; i <= hc; i++) {
        Path path = new Path();
        path.moveTo(Ox, ((getHeight() - Oy) - (i * 50)));
        path.lineTo(getWidth(), ((getHeight() - Oy) - (i * 50)));
        canvas.drawText((27.0f / hc) * i + " (ng/ml)", 36, ((getHeight() - Oy) - (i * 50)), paint1);
        canvas.drawPath(path, paint2);
    }
    wc = (getWidth() - Ox) / 50;
    for (int i = 1; i <= wc; i++) {
        Path path = new Path();
        path.moveTo(Ox + (i * 50), 0);
        path.lineTo(Ox + (i * 50), getHeight() - Oy);
        canvas.drawText((4096 / wc) * i + "", Ox + (i * 50), getHeight() - 25, paint1);
        canvas.drawPath(path, paint2);
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
   if(event.getAction()==MotionEvent.ACTION_UP) {
       for (WorkingDiagramData data :
               list) {
           Log.e("TGA", "-----------" + (data.px - event.getX()) + "-----------" + (data.py - event.getY()) + "--------");
           if ((data.px - event.getX()) * (data.px - event.getX()) + (data.py - event.getY()) * (data.py - event.getY()) <= 2500) {
               boolean a = data.isChecked();
               data.setChecked(!a);
               invalidate();
               Log.e("TGA", data.isChecked() + "--------");
           }
       }
   }


    return true;
}

private class WorkingDiagramData{
    private float x,y;
    private float px,py;
    private boolean checked;
    private String text;

    public float getPx() {
        return px;
    }

    public void setPx(float px) {
        this.px = px;
    }

    public float getPy() {
        return py;
    }

    public void setPy(float py) {
        this.py = py;
    }

    public WorkingDiagramData(float x, float y, boolean checked, String text) {
        this.x = x;
        this.y = y;
        this.checked = checked;
        this.text = text;
    }

    public float getX() {
        return x;
    }

    public float getY() {
        return y;
    }

    public boolean isChecked() {
        return checked;
    }

    public String getText() {
        return text;
    }

    public void setChecked(boolean checked) {
        this.checked = checked;
    }

    public void setText(String text) {
        this.text = text;
    }
}
复制代码

}

© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享