写了一个简单的连接静态方法

This commit is contained in:
muqing 2023-11-27 13:03:50 +08:00
parent 53290aea92
commit be914dc907
10 changed files with 208 additions and 58 deletions

View File

@ -0,0 +1,48 @@
package com.muqingedit;
import java.util.HashSet;
import java.util.Set;
/**
* ConnectionManager 类用于管理视图之间的连接关系
*/
public class ConnectionManager {
private static Set<String> connections = new HashSet<>();
/**
* ConnectionManager 构造函数初始化连接集合
*/
/**
* 添加视图之间的连接
*
* @param view1Id 第一个视图的ID
* @param view2Id 第二个视图的ID
*/
public static void addConnection(String view1Id, String view2Id) {
String connection = getConnectionKey(view1Id, view2Id);
connections.add(connection);
}
/**
* 检查两个视图是否已连接
*
* @param view1Id 第一个视图的ID
* @param view2Id 第二个视图的ID
* @return 如果两个视图已连接则返回 true否则返回 false
*/
public static boolean isConnected(String view1Id, String view2Id) {
String connection = getConnectionKey(view1Id, view2Id);
return connections.contains(connection);
}
/**
* 获取视图连接的唯一标识
*
* @param view1Id 第一个视图的ID
* @param view2Id 第二个视图的ID
* @return 视图连接的唯一标识
*/
private static String getConnectionKey(String view1Id, String view2Id) {
// 这里使用视图 ID 作为连接信息的关键
return view1Id + "-" + view2Id;
}
}

View File

@ -1,34 +1,49 @@
package com.muqingedit.activity; package com.muqingedit.activity;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.ViewTreeObserver;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import com.muqingedit.ConnectionManager;
import com.muqingedit.R; import com.muqingedit.R;
import com.muqingedit.fanjian; import com.muqingedit.fanjian;
import com.muqingedit.xian; import com.muqingedit.gj;
import java.util.ArrayList;
import java.util.List;
public class Edit extends ActivityFragment { public class Edit extends ActivityFragment {
@SuppressLint("StaticFieldLeak") public FrameLayout frameLayout;//后续需要添加fragment的移动和缩放
public FrameLayout frameLayout; public static List<fanjian> list_fanjian = new ArrayList<>();
@SuppressLint("ClickableViewAccessibility")
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.edit); setContentView(R.layout.edit);
frameLayout = findViewById(R.id.view); frameLayout = findViewById(R.id.view);
//绘制一个房间 //绘制一个房间
findViewById(R.id.button).setOnClickListener(view -> { findViewById(R.id.button).setOnClickListener(view -> new fanjian(frameLayout));
fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout) frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
.setLayoutParams(260, 250) @Override
.setXY(200, 100); public void onGlobalLayout() {
frameLayout.addView(button); // 在布局完成之后获取 FrameLayout 的高度
int height = frameLayout.getHeight();
int width = frameLayout.getWidth();
fanjian main = new fanjian(frameLayout, "main");//设置主房间
int viewWidth = main.getMeasuredWidth();
int viewHeight = main.getMeasuredHeight();
float x = (width - viewWidth) / 2.0f;
float y = (height - viewHeight) / 2.0f;
gj.sc(height + ":" + y);
main.setXY((int) x, (int) y);
frameLayout.addView(main);
// 如果只需要获取一次高度可以在获取之后移除监听器
frameLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}); });
} }
} }

View File

@ -5,38 +5,61 @@ import android.content.Context;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.text.TextUtils;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.Gravity; import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.muqingedit.activity.Edit; import com.muqingedit.activity.Edit;
public class fanjian extends FrameLayout { public class fanjian extends FrameLayout {
private String id;
private int X, Y;
public FrameLayout frameLayout;
private FrameLayout paletteView; public fanjian(FrameLayout context, String id) {
super(context.getContext());
/** this.frameLayout = context;
* 为其设置画板视图 init();
* setId(id.hashCode());
* @param paletteView setLayoutParams(300, 300);
*/
public fanjian setPaletteView(FrameLayout paletteView) {
this.paletteView = paletteView;
return this;
} }
public static int id = 0; public fanjian(FrameLayout context) {
super(context.getContext());
private int X, Y; this.frameLayout = context;
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this.getContext());
public fanjian(Context context) { dialogBuilder.setTitle("创建房间");
super(context); View inflate = LayoutInflater.from(getContext()).inflate(R.layout.new_fangjian, null);
setId(id++); EditText edit_id = inflate.findViewById(R.id.edit_id);
Button end = inflate.findViewById(R.id.button_end);
Button start = inflate.findViewById(R.id.button_start);
dialogBuilder.setView(inflate);
AlertDialog show = dialogBuilder.show();
end.setOnClickListener(view -> show.dismiss());
start.setOnClickListener(view -> {
String id = edit_id.getText().toString();
if (TextUtils.isEmpty(id)) {
return;
}
fanjian.this.id = id;
setId(id.hashCode());
Edit.list_fanjian.add(fanjian.this);
setLayoutParams(300, 300);
setXY(0, 0);
frameLayout.addView(fanjian.this);
show.dismiss();
});
init(); init();
} }
@ -78,21 +101,41 @@ public class fanjian extends FrameLayout {
fanjian.this.getLocationOnScreen(location); fanjian.this.getLocationOnScreen(location);
csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2; csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2;
csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2; csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2;
xian = new xian(getContext()).createNewLine(paletteView, csx, csy); xian = new xian(fanjian.this.frameLayout).createNewLine(csx, csy);
return true; break;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
//绘制线 //绘制线
xian.updateCurrentLine(csx + ax, csy + ay); xian.updateCurrentLine(csx + ax, csy + ay);
// xian.updateCurrentLine(event.getRawX()-ax, event.getRawY()-ay); // xian.updateCurrentLine(event.getRawX()-ax, event.getRawY()-ay);
return true; break;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
//删除线 //删除线 判断是否连接房间 如果连接则不删除不连接则删除
xian.clearCurrentLine(); int size = Edit.list_fanjian.size();
paletteView.removeView(xian); //只要大于1个房间才会进行判断操作
xian = null; if (size > 1) {
for (int i = 0; i < size; i++) {
fanjian aa = Edit.list_fanjian.get(i);
if (aa == fanjian.this) {
continue;
}
boolean ab = ConnectionManager.isConnected(fanjian.this.Id(), aa.Id());
if (ab) {
break;
}
boolean a = csx + ax > aa.getX() && csx + ax < aa.getX() + 300;
boolean b = csy + ay > aa.getY() && csy + ay < aa.getY() + 300;
if (a && b) {
ConnectionManager.addConnection(fanjian.this.Id(), aa.Id());
return true; return true;
} }
return super.onTouchEvent(event); }
}
xian.clearCurrentLine();
frameLayout.removeView(xian);
xian = null;
break;
}
return true;
} }
} }
@ -115,12 +158,9 @@ public class fanjian extends FrameLayout {
@Override @Override
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) { public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
if (Long) {
} else {
Y = (int) (e2.getY() - e1.getY()); Y = (int) (e2.getY() - e1.getY());
X = (int) (e2.getX() - e1.getX()); X = (int) (e2.getX() - e1.getX());
animate().xBy(X).yBy(Y).setDuration(0).start(); animate().xBy(X).yBy(Y).setDuration(0).start();
}
return true; return true;
} }
}); });
@ -155,6 +195,9 @@ public class fanjian extends FrameLayout {
return this.Y; return this.Y;
} }
*/ */
public String Id() {
return id;
}
public String getXY() { public String getXY() {
return X + ":" + Y; return X + ":" + Y;

View File

@ -17,9 +17,10 @@ public class xian extends View {
private Paint linePaint; private Paint linePaint;
private float currentX, currentY; private float currentX, currentY;
private float startX, startY; private float startX, startY;
FrameLayout frameLayout;
public xian(Context context) { public xian(FrameLayout context) {
super(context); super(context.getContext());
this.frameLayout = context;
init(); init();
} }
@ -33,7 +34,7 @@ public class xian extends View {
linePaint.setStrokeWidth(5f); linePaint.setStrokeWidth(5f);
} }
public xian createNewLine(FrameLayout frameLayout, float x, float y) { public xian createNewLine(float x, float y) {
startX = x; startX = x;
startY = y; startY = y;
currentX = x; currentX = x;

View File

@ -0,0 +1,43 @@
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/edit_id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:hint="ID" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/button_end"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消"/>
<Button
android:id="@+id/button_start"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="确定" />
</LinearLayout>
</LinearLayout>
</ScrollView>

View File

@ -5,4 +5,4 @@
# For customization when using a Version Control System, please read the # For customization when using a Version Control System, please read the
# header note. # header note.
#Sat Nov 25 19:49:21 CST 2023 #Sat Nov 25 19:49:21 CST 2023
sdk.dir=E\:\\SDK\\Android sdk.dir=D\:\\sdk