连线的问题就交给薄荷了

This commit is contained in:
muqing 2023-11-25 17:56:52 +08:00
parent b9b2d8013b
commit 93221e5e1a
16 changed files with 327 additions and 59 deletions

View File

@ -15,6 +15,6 @@
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity android:name=".activity.Edit" />
</application> </application>
</manifest> </manifest>

View File

@ -1,22 +1,19 @@
package com.muqingedit; package com.muqingedit;
import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.widget.Button;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import com.muqingedit.activity.Edit;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
FrameLayout view;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.huabu); setContentView(R.layout.activity_main);
view = findViewById(R.id.view); Intent intent = new Intent(this, Edit.class);
startActivity(intent);
//绘制一个房间 finish();
fanjian button = new fanjian(this)
.setLayoutParams(200, 200)
.setXY(100,100);
view.addView(button);
} }
} }

View File

@ -0,0 +1,25 @@
package com.muqingedit.activity;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.muqingedit.R;
public class ActivityFragment extends AppCompatActivity {
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(item);
}
public void setToolbar() {
setSupportActionBar(findViewById(R.id.toolbar));
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
public void setContentView(int view) {
super.setContentView(view);
setToolbar();
}
}

View File

@ -0,0 +1,40 @@
package com.muqingedit.activity;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import androidx.annotation.Nullable;
import com.muqingedit.R;
import com.muqingedit.fanjian;
import com.muqingedit.xian;
public class Edit extends ActivityFragment {
@SuppressLint("StaticFieldLeak")
public static FrameLayout frameLayout;
@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)
.setLayoutParams(200, 200)
.setXY(100, 100);
frameLayout.addView(button);
});
}
public static void addView(View view) {
frameLayout.addView(view);
}
public static Context getContext() {
return frameLayout.getContext();
}
}

View File

@ -4,72 +4,155 @@ import android.annotation.SuppressLint;
import android.content.Context; 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.view.GestureDetector;
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.FrameLayout;
import androidx.annotation.NonNull;
import com.muqingedit.activity.Edit;
public class fanjian extends FrameLayout {
public static int id = 0;
private int X, Y;
public class fanjian extends View {
public fanjian(Context context) { public fanjian(Context context) {
super(context); super(context);
setId(id++);
init(); init();
} }
private class dian extends View {
xian xian;
public dian(Context context) {
super(context);
/* ViewGroup.LayoutParams layoutParams = fanjian.this.getLayoutParams();
dian.this.setLayoutParams(
new ViewGroup.LayoutParams(layoutParams.width / 2,
layoutParams.width / 2));*/
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
float cx = getWidth() / 2f; // 圆心 x 坐标
float cy = getHeight() / 2f; // 圆心 y 坐标
float radius = 26F; // 圆的半径
canvas.drawCircle(cx, cy, radius, paint); // 绘制圆点
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//在视图上面添加一条线
int[] location = new int[2];
getLocationOnScreen(location);
int x = location[0] + getWidth() / 2;
int y = location[1] - getHeight() / 2;
xian = new xian(getContext()).createNewLine(x,
y);
return true;
case MotionEvent.ACTION_MOVE:
//绘制线
xian.updateCurrentLine(event.getRawX(), event.getRawY());
return true;
case MotionEvent.ACTION_UP:
//删除线
xian.clearCurrentLine();
Edit.frameLayout.removeView(xian);
xian = null;
return true;
}
return super.onTouchEvent(event);
}
}
private GestureDetector gestureDetector;
private boolean Long = false;
private void init() { private void init() {
// 设置背景颜色为红色 // 设置背景颜色为红色
setBackgroundColor(Color.RED); setBackgroundColor(Color.RED);
// 设置视图的尺寸为100x100像素 // 设置视图的尺寸为100x100像素
setLayoutParams(new ViewGroup.LayoutParams(0, 0)); 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) {
if (Long) {
} else {
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 @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
// 在此处进行自定义的绘制操作例如绘制图形文本等 // 在此处进行自定义的绘制操作例如绘制图形文本等
} }
public fanjian setLayoutParams(int a,int b) { public fanjian setLayoutParams(int a, int b) {
setLayoutParams(new ViewGroup.LayoutParams(a, b)); setLayoutParams(new ViewGroup.LayoutParams(a, b));
return this; return this;
} }
/*
private int X;
private int Y;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
// 在按下事件中记录按下时的位置
X = (int) event.getX();
Y = (int) event.getY();
break;
case MotionEvent.ACTION_MOVE:
// 在移动事件中计算偏移量
int dx = (int) event.getX() - X;
int dy = (int) event.getY() - Y;
// 平滑移动视图
animate().xBy(dx).yBy(dy).setDuration(0).start();
break;
}
return true;
}
@Override @Override
public float getX() { public float getX() {
return X; return this.X;
} }
@Override @Override
public float getY() { public float getY() {
return Y; return this.Y;
}
*/
public String getXY() {
return X + ":" + Y;
} }
public fanjian setXY(int a, int b) { public fanjian setXY(int a, int b) {
setX(a); setX(a);
setY(b); setY(b);
addView(new dian(getContext()));
return this; 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,12 @@
package com.muqingedit;
import android.util.Log;
public class gj {
public static void sc(Object obj) {
if (obj == null) {
obj = "nnull";
}
Log.d("编辑器" , obj.toString());
}
}

View File

@ -0,0 +1,56 @@
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.view.MotionEvent;
import android.view.View;
import com.muqingedit.activity.Edit;
@SuppressLint("ViewConstructor")
public class xian extends View {
private Paint pointPaint;
private Paint linePaint;
private float currentX, currentY;
private float startX, startY;
public xian(Context context) {
super(context);
init();
}
private void init() {
pointPaint = new Paint();
pointPaint.setColor(Color.RED);
pointPaint.setStyle(Paint.Style.FILL);
linePaint = new Paint();
linePaint.setColor(Color.BLACK);
linePaint.setStrokeWidth(5f);
}
public xian createNewLine(float x, float y) {
startX = x;startY = y;currentX = x;currentY = y;
Edit.addView(this);
invalidate();//创建一个新的线
return this;
}
public void updateCurrentLine(float x, float y) {
currentX = x;currentY = y;
invalidate();//绘制中
}
public void clearCurrentLine() {
currentX = startX;currentY = startY;
invalidate();//关闭绘制
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(startX, startY, 10f, pointPaint);//绘制初始的点
canvas.drawLine(startX, startY, currentX, currentY, linePaint);//绘制线
}
}

View File

@ -1,7 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" <androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity"> tools:context=".MainActivity">
</LinearLayout>
</androidx.appcompat.widget.LinearLayoutCompat>

View File

@ -0,0 +1,35 @@
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="@+id/view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="@+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:menu="@menu/bottom_app_bar"/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/ic_launcher_foreground"
app:layout_anchor="@id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</LinearLayout>

View File

@ -1,21 +1,15 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat xmlns:android="http://schemas.android.com/apk/res/android" <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent">
android:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.appcompat.widget.Toolbar
<com.google.android.material.appbar.MaterialToolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content" />
app:title="@string/app_name"/>
<FrameLayout <FrameLayout
android:id="@+id/view" android:id="@+id/view"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" > android:layout_height="match_parent" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/app_name" />
</FrameLayout>
</androidx.appcompat.widget.LinearLayoutCompat>

View File

@ -0,0 +1,24 @@
<?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"/>
</menu>

View File

@ -19,3 +19,4 @@ android.useAndroidX=true
# resources declared in the library itself and none from the library's dependencies, # resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library # thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true android.nonTransitiveRClass=true
android.enableJetifier=true