diff --git a/.gradle/8.0/executionHistory/executionHistory.lock b/.gradle/8.0/executionHistory/executionHistory.lock index ea478f9..2e7a9ef 100644 Binary files a/.gradle/8.0/executionHistory/executionHistory.lock and b/.gradle/8.0/executionHistory/executionHistory.lock differ diff --git a/.gradle/8.0/fileHashes/fileHashes.bin b/.gradle/8.0/fileHashes/fileHashes.bin index 0be5814..89108dd 100644 Binary files a/.gradle/8.0/fileHashes/fileHashes.bin and b/.gradle/8.0/fileHashes/fileHashes.bin differ diff --git a/.gradle/8.0/fileHashes/fileHashes.lock b/.gradle/8.0/fileHashes/fileHashes.lock index a0a8052..1f9c009 100644 Binary files a/.gradle/8.0/fileHashes/fileHashes.lock and b/.gradle/8.0/fileHashes/fileHashes.lock differ diff --git a/.gradle/buildOutputCleanup/buildOutputCleanup.lock b/.gradle/buildOutputCleanup/buildOutputCleanup.lock index df5003e..a5fb0f6 100644 Binary files a/.gradle/buildOutputCleanup/buildOutputCleanup.lock and b/.gradle/buildOutputCleanup/buildOutputCleanup.lock differ diff --git a/app/src/main/java/com/muqingedit/activity/Edit.java b/app/src/main/java/com/muqingedit/activity/Edit.java index e622064..b3369be 100644 --- a/app/src/main/java/com/muqingedit/activity/Edit.java +++ b/app/src/main/java/com/muqingedit/activity/Edit.java @@ -14,7 +14,8 @@ import com.muqingedit.xian; public class Edit extends ActivityFragment { @SuppressLint("StaticFieldLeak") - public static FrameLayout frameLayout; + public FrameLayout frameLayout; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -22,19 +23,12 @@ public class Edit extends ActivityFragment { frameLayout = findViewById(R.id.view); //绘制一个房间 findViewById(R.id.button).setOnClickListener(view -> { - fanjian button = new fanjian(Edit.this) + fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout) .setLayoutParams(260, 250) .setXY(200, 100); frameLayout.addView(button); }); } - public static void addView(View view) { - frameLayout.addView(view); - } - - public static Context getContext() { - return frameLayout.getContext(); - } } diff --git a/app/src/main/java/com/muqingedit/fanjian.java b/app/src/main/java/com/muqingedit/fanjian.java index a429b2a..637484a 100644 --- a/app/src/main/java/com/muqingedit/fanjian.java +++ b/app/src/main/java/com/muqingedit/fanjian.java @@ -18,6 +18,18 @@ import com.muqingedit.activity.Edit; public class fanjian extends FrameLayout { + private FrameLayout paletteView; + + /** + * 为其设置画板视图 + * + * @param paletteView + */ + public fanjian setPaletteView(FrameLayout paletteView) { + this.paletteView = paletteView; + return this; + } + public static int id = 0; private int X, Y; @@ -30,14 +42,16 @@ public class fanjian extends FrameLayout { private class dian extends View { xian xian; + public dian(Context context) { super(context); ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams(); - LayoutParams layoutParams = new LayoutParams(aa.width /3, + LayoutParams layoutParams = new LayoutParams(aa.width / 3, aa.height / 3, Gravity.CENTER); this.setLayoutParams(layoutParams); // setBackgroundColor(Color.WHITE); } + @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); @@ -45,12 +59,14 @@ public class fanjian extends FrameLayout { paint.setColor(Color.WHITE); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); - int centerX = getWidth()/2; - int centerY = getHeight()/2; + int centerX = getWidth() / 2; + int centerY = getHeight() / 2; int radius = Math.min(centerX, centerY); canvas.drawCircle(centerX, centerY, radius, paint); } - float ax, ay,csx, csy; + + float ax, ay, csx, csy; + @Override public boolean onTouchEvent(MotionEvent event) { ax = event.getX(); @@ -60,9 +76,9 @@ public class fanjian extends FrameLayout { //在视图上面添加一条线 int[] location = new int[2]; 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(csx, csy); + 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; case MotionEvent.ACTION_MOVE: //绘制线 @@ -72,7 +88,7 @@ public class fanjian extends FrameLayout { case MotionEvent.ACTION_UP: //删除线 xian.clearCurrentLine(); - Edit.frameLayout.removeView(xian); + paletteView.removeView(xian); xian = null; return true; } @@ -83,6 +99,7 @@ public class fanjian extends FrameLayout { private GestureDetector gestureDetector; private boolean Long = false; + private void init() { // 设置背景颜色为红色 setBackgroundColor(Color.RED); @@ -95,6 +112,7 @@ public class fanjian extends FrameLayout { Long = !Long; // 处理长按事件 } + @Override public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) { if (Long) { @@ -113,6 +131,7 @@ public class fanjian extends FrameLayout { gestureDetector.onTouchEvent(event); return true; } + @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { @@ -153,6 +172,7 @@ public class fanjian extends FrameLayout { super.setX(x); X = (int) x; } + @Override public void setY(float y) { super.setY(y); diff --git a/app/src/main/java/com/muqingedit/xian.java b/app/src/main/java/com/muqingedit/xian.java index 198aff2..2b69c11 100644 --- a/app/src/main/java/com/muqingedit/xian.java +++ b/app/src/main/java/com/muqingedit/xian.java @@ -7,6 +7,7 @@ import android.graphics.Color; import android.graphics.Paint; import android.view.MotionEvent; import android.view.View; +import android.widget.FrameLayout; import com.muqingedit.activity.Edit; @@ -32,18 +33,25 @@ public class xian extends View { linePaint.setStrokeWidth(5f); } - public xian createNewLine(float x, float y) { - startX = x;startY = y;currentX = x;currentY = y; - Edit.addView(this); + public xian createNewLine(FrameLayout frameLayout, float x, float y) { + startX = x; + startY = y; + currentX = x; + currentY = y; + frameLayout.addView(this); invalidate();//创建一个新的线 return this; } + public void updateCurrentLine(float x, float y) { - currentX = x;currentY = y; + currentX = x; + currentY = y; invalidate();//绘制中 } + public void clearCurrentLine() { - currentX = startX;currentY = startY; + currentX = startX; + currentY = startY; invalidate();//关闭绘制 }