连线的问题就交给薄荷了
This commit is contained in:
parent
b9b2d8013b
commit
93221e5e1a
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -15,6 +15,6 @@
|
|||
<category android:name="android.intent.category.LAUNCHER" />
|
||||
</intent-filter>
|
||||
</activity>
|
||||
<activity android:name=".activity.Edit" />
|
||||
</application>
|
||||
|
||||
</manifest>
|
|
@ -1,22 +1,19 @@
|
|||
package com.muqingedit;
|
||||
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.widget.Button;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import com.muqingedit.activity.Edit;
|
||||
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
FrameLayout view;
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.huabu);
|
||||
view = findViewById(R.id.view);
|
||||
|
||||
//绘制一个房间
|
||||
fanjian button = new fanjian(this)
|
||||
.setLayoutParams(200, 200)
|
||||
.setXY(100,100);
|
||||
view.addView(button);
|
||||
setContentView(R.layout.activity_main);
|
||||
Intent intent = new Intent(this, Edit.class);
|
||||
startActivity(intent);
|
||||
finish();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
40
app/src/main/java/com/muqingedit/activity/Edit.java
Normal file
40
app/src/main/java/com/muqingedit/activity/Edit.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
|
@ -4,72 +4,155 @@ import android.annotation.SuppressLint;
|
|||
import android.content.Context;
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Color;
|
||||
import android.graphics.Paint;
|
||||
import android.view.GestureDetector;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
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) {
|
||||
super(context);
|
||||
setId(id++);
|
||||
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() {
|
||||
// 设置背景颜色为红色
|
||||
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) {
|
||||
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
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
// 在此处进行自定义的绘制操作,例如绘制图形、文本等
|
||||
}
|
||||
|
||||
public fanjian setLayoutParams(int a,int b) {
|
||||
public fanjian setLayoutParams(int a, int b) {
|
||||
setLayoutParams(new ViewGroup.LayoutParams(a, b));
|
||||
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
|
||||
public float getX() {
|
||||
return X;
|
||||
return this.X;
|
||||
}
|
||||
|
||||
@Override
|
||||
public float getY() {
|
||||
return Y;
|
||||
return this.Y;
|
||||
}
|
||||
*/
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
12
app/src/main/java/com/muqingedit/gj.java
Normal file
12
app/src/main/java/com/muqingedit/gj.java
Normal 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());
|
||||
}
|
||||
}
|
56
app/src/main/java/com/muqingedit/xian.java
Normal file
56
app/src/main/java/com/muqingedit/xian.java
Normal 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);//绘制线
|
||||
}
|
||||
}
|
|
@ -1,7 +1,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"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
</LinearLayout>
|
||||
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
|
|
35
app/src/main/res/layout/edit.xml
Normal file
35
app/src/main/res/layout/edit.xml
Normal 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>
|
|
@ -1,21 +1,15 @@
|
|||
<?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_height="match_parent"
|
||||
android:orientation="vertical"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
<com.google.android.material.appbar.MaterialToolbar
|
||||
android:layout_height="match_parent">
|
||||
|
||||
<androidx.appcompat.widget.Toolbar
|
||||
android:id="@+id/toolbar"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
app:title="@string/app_name"/>
|
||||
android:layout_height="wrap_content" />
|
||||
|
||||
<FrameLayout
|
||||
android:id="@+id/view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent" >
|
||||
|
||||
<TextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:text="@string/app_name" />
|
||||
</FrameLayout>
|
||||
</androidx.appcompat.widget.LinearLayoutCompat>
|
||||
android:layout_height="match_parent" />
|
||||
</LinearLayout>
|
24
app/src/main/res/menu/bottom_app_bar.xml
Normal file
24
app/src/main/res/menu/bottom_app_bar.xml
Normal 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>
|
|
@ -19,3 +19,4 @@ android.useAndroidX=true
|
|||
# resources declared in the library itself and none from the library's dependencies,
|
||||
# thereby reducing the size of the R class for that library
|
||||
android.nonTransitiveRClass=true
|
||||
android.enableJetifier=true
|
||||
|
|
Reference in New Issue
Block a user