自定义View:
public class TargetView extends View {
private Paint paint;
private int[] radii = {100, 250, 400, 550, 700}; // 五个圆的半径
private int numberOfSegments = 8;
private int[][] regionValues; // 存储每个区域的值
public TargetView(Context context) {
super(context);
init();
}
private void init() {
paint = new Paint();
regionValues = new int[5][numberOfSegments]; // 5个圆,每个圆8个区域
for (int i = 0; i < 5; i++) {
for (int j = 0; j < numberOfSegments; j++) {
regionValues[i][j] = i * numberOfSegments + j + 1; // 示例值
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawCircles(canvas);
drawSegments(canvas);
drawSegmentValues(canvas);
}
private void drawCircles(Canvas canvas) {
for (int radius : radii) {
paint.setColor(Color.LTGRAY);
canvas.drawCircle(getWidth() / 2, getHeight() / 2, radius, paint);
}
}
private void drawSegments(Canvas canvas) {
paint.setColor(Color.BLACK);
for (int i = 0; i < numberOfSegments; i++) {
float angle = (float) (Math.toRadians(360.0 / numberOfSegments * i));
for (int radius : radii) {
float startX = (float) (getWidth() / 2 + radius * Math.cos(angle));
float startY = (float) (getHeight() / 2 + radius * Math.sin(angle));
canvas.drawLine(getWidth() / 2, getHeight() / 2, startX, startY, paint);
}
}
}
private void drawSegmentValues(Canvas canvas) {
paint.setColor(Color.BLACK);
paint.setTextSize(40);
paint.setTextAlign(Paint.Align.CENTER);
for (int i = 0; i < regionValues.length; i++) {
for (int j = 0; j < numberOfSegments; j++) {
float angle = (float) (Math.toRadians(360.0 / numberOfSegments * j + 22.5)); // 中心角
float centerX = (float) (getWidth() / 2 + radii[i] * Math.cos(angle) * 0.5);
float centerY = (float) (getHeight() / 2 + radii[i] * Math.sin(angle) * 0.5);
canvas.drawText(String.valueOf(regionValues[i][j]), centerX, centerY, paint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
float x = event.getX();
float y = event.getY();
int[] result = getRegionIndex(x, y);
if (result != null) {
int circleIndex = result[0];
int segmentIndex = result[1];
String position = getPosition(x, y);
String message = "环: " + (circleIndex + 1) + ", 分割点: " + (segmentIndex + 1) + ", 位置: " + position;
Toast.makeText(getContext(), message, Toast.LENGTH_SHORT).show();
}
}
return true;
}
private int[] getRegionIndex(float x, float y) {
float dx = x - getWidth() / 2;
float dy = y - getHeight() / 2;
double distance = Math.sqrt(dx * dx + dy * dy);
for (int i = 0; i < radii.length; i++) {
if (distance <= radii[i]) {
for (int j = 0; j < numberOfSegments; j++) {
double angle = Math.toDegrees(Math.atan2(dy, dx));
if (angle < 0) angle += 360;
if (angle >= (360.0 / numberOfSegments) * j && angle < (360.0 / numberOfSegments) * (j + 1)) {
return new int[]{i, j}; // 返回圆的索引和分割区域索引
}
}
}
}
return null; // 点击不在任何区域内
}
private String getPosition(float x, float y) {
String horizontal = (x < getWidth() / 2) ? "左" : "右";
String vertical = (y < getHeight() / 2) ? "上" : "下";
return horizontal + " " + vertical;
}
TestActivity :
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
public class TestActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TargetView(this));
TargetView targetView = new TargetView(this);
setContentView(targetView);
}
}
build.gradle的module中dependencies 增加:
implementation 'com.licheedev:android-serialport:2.1.3'
xml自定义布局引用:
<com.所在包名.TargetView
android:layout_width="match_parent"
android:layout_height="match_parent"/>