写了一个简单的连接静态方法
This commit is contained in:
parent
53290aea92
commit
be914dc907
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
48
app/src/main/java/com/muqingedit/ConnectionManager.java
Normal file
48
app/src/main/java/com/muqingedit/ConnectionManager.java
Normal 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;
|
||||
}
|
||||
}
|
|
@ -1,34 +1,49 @@
|
|||
package com.muqingedit.activity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.content.Context;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.view.ViewTreeObserver;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.muqingedit.ConnectionManager;
|
||||
import com.muqingedit.R;
|
||||
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 {
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
public FrameLayout frameLayout;
|
||||
|
||||
public FrameLayout frameLayout;//后续需要添加fragment的移动和缩放
|
||||
public static List<fanjian> list_fanjian = new ArrayList<>();
|
||||
@SuppressLint("ClickableViewAccessibility")
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.edit);
|
||||
frameLayout = findViewById(R.id.view);
|
||||
//绘制一个房间
|
||||
findViewById(R.id.button).setOnClickListener(view -> {
|
||||
fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout)
|
||||
.setLayoutParams(260, 250)
|
||||
.setXY(200, 100);
|
||||
frameLayout.addView(button);
|
||||
findViewById(R.id.button).setOnClickListener(view -> new fanjian(frameLayout));
|
||||
frameLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||
@Override
|
||||
public void onGlobalLayout() {
|
||||
// 在布局完成之后获取 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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -5,38 +5,61 @@ import android.content.Context;
|
|||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.text.TextUtils;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.Gravity;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
|
||||
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
|
||||
import com.muqingedit.activity.Edit;
|
||||
|
||||
public class fanjian extends FrameLayout {
|
||||
private String id;
|
||||
private int X, Y;
|
||||
public FrameLayout frameLayout;
|
||||
|
||||
private FrameLayout paletteView;
|
||||
|
||||
/**
|
||||
* 为其设置画板视图
|
||||
*
|
||||
* @param paletteView
|
||||
*/
|
||||
public fanjian setPaletteView(FrameLayout paletteView) {
|
||||
this.paletteView = paletteView;
|
||||
return this;
|
||||
public fanjian(FrameLayout context, String id) {
|
||||
super(context.getContext());
|
||||
this.frameLayout = context;
|
||||
init();
|
||||
setId(id.hashCode());
|
||||
setLayoutParams(300, 300);
|
||||
}
|
||||
|
||||
public static int id = 0;
|
||||
|
||||
private int X, Y;
|
||||
|
||||
public fanjian(Context context) {
|
||||
super(context);
|
||||
setId(id++);
|
||||
public fanjian(FrameLayout context) {
|
||||
super(context.getContext());
|
||||
this.frameLayout = context;
|
||||
MaterialAlertDialogBuilder dialogBuilder = new MaterialAlertDialogBuilder(this.getContext());
|
||||
dialogBuilder.setTitle("创建房间");
|
||||
View inflate = LayoutInflater.from(getContext()).inflate(R.layout.new_fangjian, null);
|
||||
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();
|
||||
}
|
||||
|
||||
|
@ -78,21 +101,41 @@ public class fanjian extends FrameLayout {
|
|||
fanjian.this.getLocationOnScreen(location);
|
||||
csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2;
|
||||
csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2;
|
||||
xian = new xian(getContext()).createNewLine(paletteView, csx, csy);
|
||||
return true;
|
||||
xian = new xian(fanjian.this.frameLayout).createNewLine(csx, csy);
|
||||
break;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
//绘制线
|
||||
xian.updateCurrentLine(csx + ax, csy + ay);
|
||||
// xian.updateCurrentLine(event.getRawX()-ax, event.getRawY()-ay);
|
||||
return true;
|
||||
break;
|
||||
case MotionEvent.ACTION_UP:
|
||||
//删除线
|
||||
//删除线 判断是否连接房间 如果连接则不删除不连接则删除
|
||||
int size = Edit.list_fanjian.size();
|
||||
//只要大于1个房间才会进行判断操作
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
xian.clearCurrentLine();
|
||||
paletteView.removeView(xian);
|
||||
frameLayout.removeView(xian);
|
||||
xian = null;
|
||||
return true;
|
||||
break;
|
||||
}
|
||||
return super.onTouchEvent(event);
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -115,12 +158,9 @@ public class fanjian extends FrameLayout {
|
|||
|
||||
@Override
|
||||
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
|
||||
if (Long) {
|
||||
} else {
|
||||
Y = (int) (e2.getY() - e1.getY());
|
||||
X = (int) (e2.getX() - e1.getX());
|
||||
animate().xBy(X).yBy(Y).setDuration(0).start();
|
||||
}
|
||||
Y = (int) (e2.getY() - e1.getY());
|
||||
X = (int) (e2.getX() - e1.getX());
|
||||
animate().xBy(X).yBy(Y).setDuration(0).start();
|
||||
return true;
|
||||
}
|
||||
});
|
||||
|
@ -144,17 +184,20 @@ public class fanjian extends FrameLayout {
|
|||
return this;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public float getX() {
|
||||
return this.X;
|
||||
}
|
||||
/*
|
||||
@Override
|
||||
public float getX() {
|
||||
return this.X;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getY() {
|
||||
return this.Y;
|
||||
@Override
|
||||
public float getY() {
|
||||
return this.Y;
|
||||
}
|
||||
*/
|
||||
public String Id() {
|
||||
return id;
|
||||
}
|
||||
*/
|
||||
|
||||
public String getXY() {
|
||||
return X + ":" + Y;
|
||||
|
|
|
@ -17,9 +17,10 @@ public class xian extends View {
|
|||
private Paint linePaint;
|
||||
private float currentX, currentY;
|
||||
private float startX, startY;
|
||||
|
||||
public xian(Context context) {
|
||||
super(context);
|
||||
FrameLayout frameLayout;
|
||||
public xian(FrameLayout context) {
|
||||
super(context.getContext());
|
||||
this.frameLayout = context;
|
||||
init();
|
||||
}
|
||||
|
||||
|
@ -33,7 +34,7 @@ public class xian extends View {
|
|||
linePaint.setStrokeWidth(5f);
|
||||
}
|
||||
|
||||
public xian createNewLine(FrameLayout frameLayout, float x, float y) {
|
||||
public xian createNewLine(float x, float y) {
|
||||
startX = x;
|
||||
startY = y;
|
||||
currentX = x;
|
||||
|
|
43
app/src/main/res/layout/new_fangjian.xml
Normal file
43
app/src/main/res/layout/new_fangjian.xml
Normal 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>
|
|
@ -5,4 +5,4 @@
|
|||
# For customization when using a Version Control System, please read the
|
||||
# header note.
|
||||
#Sat Nov 25 19:49:21 CST 2023
|
||||
sdk.dir=E\:\\SDK\\Android
|
||||
sdk.dir=D\:\\sdk
|
Reference in New Issue
Block a user