android 模型曲线图
package com.sun.demo;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import androidx.annotation.Nullable;
/**
* 模型曲线图 控件(自定义控件-模型曲线图)
* @author sunmingshun
* @date 2022/7/26.
*/
public class MyEspView extends View {
private Paint paint;
private Path path;
int radian = 100;
private boolean isInitialization;
CircleListener circleListener;
Paint p = new Paint();
public interface CircleListener {
// 拿到圆心 半径 中点坐标
void calculateCircle(int x,int y,int r,int wide,int partition);
void wide(int wide);
}
public void setRadian(int radian){
this.radian = radian;
path.reset();
this.invalidate();
}
public void setCircleListener(CircleListener circleListener){
this.circleListener = circleListener;
}
public MyEspView(Context context) {
super(context);
initData();
}
public MyEspView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initData();
}
public MyEspView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initData();
}
public MyEspView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initData();
}
public void initData(){
paint=new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);//设置为不实心
path=new Path();
paint.setStrokeWidth(3);
paint.setColor(Color.BLACK);
paint.setTextSize(52);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int mWidth = this.getWidth();
int mHeight = this.getHeight();
if(!isInitialization){
radian = (mWidth-10) / 4;
isInitialization = true;
if(circleListener != null){
circleListener.wide(mWidth / 2);
}
}
int x1=5,y1=5;
int x2=5,y2= mHeight-5;
int x3= mWidth-5,y3=5;
int x4= mWidth-5,y4= mHeight-5;
int x5=5,y5=mHeight/2;
int x6=mWidth-5,y6=mWidth/2;
int x7=mWidth/2,y7=5;
int x8=mWidth/2,y8=mHeight-5;
int x9=mWidth/2,y9=mHeight/2;
// path.moveTo(x1,y1);
// path.lineTo(x2,y2);
// path.moveTo(x1,y1);
// path.lineTo(x3,y3);
// path.moveTo(x2,y2);
// path.lineTo(x4,y4);
// path.moveTo(x3,y3);
// path.lineTo(x4,y4);
// path.moveTo(x5,y5);
// path.lineTo(x6,y6);
// path.moveTo(x7,y7);
// path.lineTo(x8,y8);
Log.d("dayin mWidth",mWidth + "");
Log.d("dayin mHeight",mHeight + "");
int x10= mWidth - radian,y10=mHeight/2 - radian;
int x11= radian+5,y11=mHeight/2 + radian;
path.moveTo(x9,y9);//起点
path.quadTo(x10,y10,x3,y3);//3点画弧
path.moveTo(x9,y9);//起点
path.quadTo(x11,y11,x2,y2);//3点画弧
int [] ints = calculationCircle(x9,y9,x10,y10,x3,y3);
if(circleListener != null){
circleListener.calculateCircle(ints[0],ints[1],ints[2],mWidth/2,ints[3]);
}
canvas.drawPath(path,paint);
}
/**
* 3点 圆弧 计算圆心 半径
* @param x1
* @param y1
* @param x2
* @param y2
* @param x3
* @param y3
* @return
*/
private int[] calculationCircle(int x1, int y1, int x2, int y2, int x3, int y3){
double a13 = x1 - x3;
double a13_ = x1 + x3;
double b13 = y1 - y3;
double b13_ = y1 + y3;
double a12 = x1 - x2;
double a12_ = x1 + x2;
double b12 = y1 - y2;
double b12_ = y1 + y2;
double a12b12_2 = a12 * a12_ + b12 * b12_;
double a13b13_2 = a13 * a13_ + b13 * b13_;
double a13b12 =2* a13 * b12;
double a12b13 =2* a12 * b13;
int[] ints = new int[4];
if (a12b13 - a13b12 == 0) {
int x = (x2+x1);
int y = (y2+y1);
ints[0] = (int) x;
ints[1] = (int) y;
ints[2] = 150;
ints[3] = 0;
return ints;
}else if (a12b13 - a13b12 > 0) {
double af = a12b13 - a13b12;
double bf = a13b12 - a12b13;
double az = b13 * a12b12_2 - b12 * a13b13_2;
double bz = a13 * a12b12_2 - a12 * a13b13_2;
int a = (int) (az / af);
int b = (int) (bz / bf);
ints[0] = (int) a+x1/2 ;
ints[1] = (int) b+x1/2 ;
double r = Math.sqrt((x1 - ints[0]) * (x1 - ints[0]) + (y1 - ints[1]) * (y1 - ints[1]));
ints[2] = (int) r;
ints[3] = 1;
}else if (a12b13 - a13b12 < 0){
double af = a12b13 - a13b12;
double bf = a13b12 - a12b13;
double az = b13 * a12b12_2 - b12 * a13b13_2;
double bz = a13 * a12b12_2 - a12 * a13b13_2;
int a = (int) (az / af);
int b = (int) (bz / bf);
ints[0] = (int) a-x1/2;
ints[1] = (int) b-x1/2;
double r = Math.sqrt((x1 - ints[0]) * (x1 - ints[0]) + (y1 - ints[1]) * (y1 - ints[1]));
//计算y值
int y = (int) (ints[1] - Math.sqrt((r*r)-(ints[0]-250)*(ints[0]-250)));
y = ints[1] + (ints[1]-y);
ints[2] = (int) r;
ints[3] = -1;
}
return ints;
}
}
用法查看demo
版权声明:本文为qq_37630270原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。