添加了连接逻辑方法

This commit is contained in:
muqing 2023-12-01 18:06:37 +08:00
parent be914dc907
commit afd08c4ab4
12 changed files with 299 additions and 142 deletions

View File

@ -4,6 +4,9 @@ import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.muqingedit.R;
import java.util.Objects;
public class ActivityFragment extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
@ -14,7 +17,7 @@ public class ActivityFragment extends AppCompatActivity {
}
public void setToolbar() {
setSupportActionBar(findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
}
@Override

View File

@ -2,48 +2,71 @@ package com.muqingedit.activity;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.ViewTreeObserver;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.FrameLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import com.muqingedit.ConnectionManager;
import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.muqingedit.R;
import com.muqingedit.fanjian;
import com.muqingedit.gj;
import java.util.ArrayList;
import java.util.List;
public class Edit extends ActivityFragment {
public FrameLayout frameLayout;//后续需要添加fragment的移动和缩放
public static List<fanjian> list_fanjian = new ArrayList<>();
//收集所有的房间
public static List<fanjian> list_fanjian;
public static BottomAppBar bottomAppBar;
@SuppressLint("StaticFieldLeak")
public static fanjian fanjian;//选中的房间
@SuppressLint("ClickableViewAccessibility")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit);
list_fanjian = new ArrayList<>();
frameLayout = findViewById(R.id.view);
//绘制一个房间
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();
Connection();
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);
}
private void Connection() {
bottomAppBar = findViewById(R.id.bottomAppBar);
bottomAppBar.setOnMenuItemClickListener(item -> {
int id = item.getItemId();
if (id == R.id.Connection) {
if (fanjian != null) {
new com.muqingedit.list_AlertDialog(this);
}
}
return false;
});
bottomAppBar.performHide(false);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.edit_toolbar,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
int itemId = item.getItemId();
if (itemId == R.id.Save) {
//保存
}
return super.onOptionsItemSelected(item);
}
}

View File

@ -1,13 +1,10 @@
package com.muqingedit;
import android.annotation.SuppressLint;
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;
@ -15,6 +12,8 @@ import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
@ -30,9 +29,7 @@ public class fanjian extends FrameLayout {
public fanjian(FrameLayout context, String id) {
super(context.getContext());
this.frameLayout = context;
init();
setId(id.hashCode());
setLayoutParams(300, 300);
init(id);
}
public fanjian(FrameLayout context) {
@ -49,20 +46,131 @@ public class fanjian extends FrameLayout {
end.setOnClickListener(view -> show.dismiss());
start.setOnClickListener(view -> {
String id = edit_id.getText().toString();
for (int i = 0; i < Edit.list_fanjian.size(); i++) {
if (Edit.list_fanjian.get(i).Id().equals(id)) {
Toast.makeText(this.getContext(), "已经存在该房间", Toast.LENGTH_SHORT).show();
return;
}
}
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);
init(id);
show.dismiss();
});
init();
}
private GestureDetector gestureDetector;
private boolean Long = false;
public TextView textView;
private void init(String id) {
// 设置背景颜色为红色
setBackgroundColor(Color.RED);
// 设置视图的尺寸为100x100像素
setLayoutParams(300, 300);
Id(id);
frameLayout.addView(this);
Edit.list_fanjian.add(this);
// 进行初始化操作例如设置背景颜色文字等
gestureDetector = new GestureDetector(getContext(),
new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onDoubleTap(@NonNull MotionEvent e) {
if (Edit.fanjian != null) {
Edit.fanjian.setBackgroundColor(Color.RED);
}
if (Edit.fanjian == fanjian.this) {
Edit.fanjian = null;
Edit.bottomAppBar.performHide();
return super.onDoubleTap(e);
}
Edit.bottomAppBar.performShow();
Edit.fanjian = fanjian.this;
fanjian.this.setBackgroundColor(Color.MAGENTA);
return super.onDoubleTap(e);
}
@Override
public void onLongPress(@NonNull MotionEvent e) {
Long = !Long;
// 处理长按事件
}
@Override
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
Y = (int) (e2.getY() - e1.getY());
X = (int) (e2.getX() - e1.getX());
animate().xBy(X).yBy(Y).setDuration(0).start();
return true;
}
});
textView = new TextView(this.getContext());
textView.setText(id);
addView(textView);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
public fanjian setLayoutParams(int a, int b) {
setLayoutParams(new ViewGroup.LayoutParams(a, b));
return this;
}
/*
@Override
public float getX() {
return this.X;
}
@Override
public float getY() {
return this.Y;
}
*/
public String Id() {
return id;
}
public String getXY() {
return X + ":" + Y;
}
public fanjian setXY(int a, int b) {
setX(a);
setY(b);
return this;
}
@Override
public void setX(float x) {
super.setX(x);
X = (int) x;
}
@Override
public void setY(float y) {
super.setY(y);
Y = (int) y;
}
public void Id(String id) {
this.id = id;
setId(id.hashCode());
}
/*暂时弃用的一个自定义控件
private class dian extends View {
xian xian;
@ -139,87 +247,5 @@ public class fanjian extends FrameLayout {
}
}
private GestureDetector gestureDetector;
private boolean Long = false;
private void init() {
// 设置背景颜色为红色
setBackgroundColor(Color.RED);
// 设置视图的尺寸为100x100像素
setLayoutParams(new ViewGroup.LayoutParams(0, 0));
// 进行初始化操作例如设置背景颜色文字等
gestureDetector = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {
@Override
public void onLongPress(@NonNull MotionEvent e) {
Long = !Long;
// 处理长按事件
}
@Override
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
Y = (int) (e2.getY() - e1.getY());
X = (int) (e2.getX() - e1.getX());
animate().xBy(X).yBy(Y).setDuration(0).start();
return true;
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
gestureDetector.onTouchEvent(event);
return true;
}
@SuppressLint("DrawAllocation")
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 在此处进行自定义的绘制操作例如绘制图形文本等
}
public fanjian setLayoutParams(int a, int b) {
setLayoutParams(new ViewGroup.LayoutParams(a, b));
return this;
}
/*
@Override
public float getX() {
return this.X;
}
@Override
public float getY() {
return this.Y;
}
*/
public String Id() {
return id;
}
public String getXY() {
return X + ":" + Y;
}
public fanjian setXY(int a, int b) {
setX(a);
setY(b);
addView(new dian(getContext()));
return this;
}
@Override
public void setX(float x) {
super.setX(x);
X = (int) x;
}
@Override
public void setY(float y) {
super.setY(y);
Y = (int) y;
}
*/
}

View File

@ -0,0 +1,88 @@
package com.muqingedit;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.muqingedit.activity.Edit;
import java.util.ArrayList;
import java.util.List;
public class list_AlertDialog extends MaterialAlertDialogBuilder {
RecyclerView list;
AlertDialog alertDialog;
public list_AlertDialog(@NonNull Context context) {
super(context);
list = new RecyclerView(context);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(context);
list.setLayoutManager(linearLayoutManager);
list.setAdapter(new adapter(Edit.list_fanjian));
setView(list);
show();
}
@Override
public AlertDialog show() {
this.alertDialog = super.show();
return alertDialog;
}
public void dismiss() {
alertDialog.dismiss();
}
class adapter extends RecyclerView.Adapter<Vh> {
private final List<fanjian> list=new ArrayList<>();
public adapter(List<fanjian> list) {
this.list.addAll(list);
this.list.remove(Edit.fanjian);
}
@NonNull
@Override
public Vh onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View inflate = View.inflate(list_AlertDialog.this.getContext(),
R.layout.list_fangjian, null);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
inflate.setLayoutParams(lp);
return new Vh(inflate);
}
@Override
public void onBindViewHolder(@NonNull Vh holder, int position) {
fanjian fanjian = list.get(position);
holder.textView.setText(fanjian.Id());
holder.itemView.setOnClickListener(view -> {
fanjian.textView.setText(fanjian.Id() + "已经连接了" + Edit.fanjian.Id());
Edit.fanjian.textView.setText(Edit.fanjian.Id()+"已经连接了"+
fanjian.Id());
/* xian xian = new xian(fanjian.frameLayout);
xian.createNewLine(Edit.fanjian.getX(), Edit.fanjian.getY());
xian.updateCurrentLine(fanjian.getX(), fanjian.getY());*/
list_AlertDialog.this.dismiss();
});
}
@Override
public int getItemCount() {
return list.size();
}
}
class Vh extends RecyclerView.ViewHolder {
public TextView textView;
public Vh(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.text);
}
}
}

View File

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
@ -25,13 +26,18 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/bottom_app_bar" />
app:menu="@menu/bottom_app_bar"
app:navigationContentDescription="@string/app_name"
app:fabAlignmentMode="center"
tools:ignore="VisualLintBottomAppBar" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="@id/bottomAppBar"
app:srcCompat="@drawable/baseline_add_24" />
app:srcCompat="@drawable/baseline_add_24"
tools:ignore="ImageContrastCheck"
android:contentDescription="@string/app_name" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="56dp"
android:layout_height="56dp"
android:src="@drawable/ic_launcher_foreground"
android:contentDescription="icon" />
<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="id" />
</LinearLayout>

View File

@ -1,24 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- <item-->
<!-- android:id="@+id/accelerator"-->
<!-- android:icon="@drawable/ic_launcher_foreground"-->
<!-- android:title="@string/app_name"-->
<!-- android:contentDescription="@string/app_name"-->
<!-- app:showAsAction="ifRoom"/>-->
<!-- <item-->
<!-- android:id="@+id/rotation"-->
<!-- android:icon="@drawable/ic_launcher_foreground"-->
<!-- android:title="@string/app_name"-->
<!-- android:contentDescription="@string/app_name"-->
<!-- app:showAsAction="ifRoom"/>-->
<!-- <item-->
<!-- android:id="@+id/dashboard"-->
<!-- android:icon="@drawable/ic_launcher_foreground"-->
<!-- android:title="@string/app_name"-->
<!-- android:contentDescription="@string/app_name"-->
<!-- app:showAsAction="ifRoom"/>-->
<item
android:id="@+id/Connection"
android:icon="@drawable/baseline_add_24"
android:title="连线"
android:contentDescription="连线"
app:showAsAction="ifRoom"/>
</menu>

View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/Save"
android:icon="@drawable/baseline_add_24"
android:title="保存"
app:showAsAction="ifRoom"/>
</menu>