From cd8d3a30cde8907d634d492abb9b4b98edc116c5 Mon Sep 17 00:00:00 2001 From: MUQING <1966944300@qq.com> Date: Sat, 25 Nov 2023 22:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E4=BA=86=E6=8B=96=E6=8B=BD?= =?UTF-8?q?=E7=BA=BF=E7=9A=84=E5=8A=9F=E8=83=BD=20=E8=BF=9E=E7=BA=BF?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E5=BB=BA=E8=AE=AE=E8=96=84=E8=8D=B7=E9=A9=AC?= =?UTF-8?q?=E4=B8=8A=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/muqingedit/activity/Edit.java | 4 +- app/src/main/java/com/muqingedit/fanjian.java | 38 ++++++++++--------- app/src/main/res/layout/huabu.xml | 15 -------- 3 files changed, 23 insertions(+), 34 deletions(-) delete mode 100644 app/src/main/res/layout/huabu.xml diff --git a/app/src/main/java/com/muqingedit/activity/Edit.java b/app/src/main/java/com/muqingedit/activity/Edit.java index 952b77e..e622064 100644 --- a/app/src/main/java/com/muqingedit/activity/Edit.java +++ b/app/src/main/java/com/muqingedit/activity/Edit.java @@ -23,8 +23,8 @@ public class Edit extends ActivityFragment { //绘制一个房间 findViewById(R.id.button).setOnClickListener(view -> { fanjian button = new fanjian(Edit.this) - .setLayoutParams(200, 200) - .setXY(100, 100); + .setLayoutParams(260, 250) + .setXY(200, 100); frameLayout.addView(button); }); } diff --git a/app/src/main/java/com/muqingedit/fanjian.java b/app/src/main/java/com/muqingedit/fanjian.java index 028cb16..a429b2a 100644 --- a/app/src/main/java/com/muqingedit/fanjian.java +++ b/app/src/main/java/com/muqingedit/fanjian.java @@ -6,6 +6,7 @@ import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.GestureDetector; +import android.view.Gravity; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; @@ -31,37 +32,42 @@ public class fanjian extends FrameLayout { 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));*/ + ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams(); + 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); - Paint paint = new Paint(); + @SuppressLint("DrawAllocation") 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); // 绘制圆点 + paint.setAntiAlias(true); + 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; @Override public boolean onTouchEvent(MotionEvent event) { + ax = event.getX(); + ay = event.getY(); 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); + 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); return true; case MotionEvent.ACTION_MOVE: //绘制线 - xian.updateCurrentLine(event.getRawX(), event.getRawY()); + xian.updateCurrentLine(csx + ax, csy + ay); +// xian.updateCurrentLine(event.getRawX()-ax, event.getRawY()-ay); return true; case MotionEvent.ACTION_UP: //删除线 @@ -70,7 +76,6 @@ public class fanjian extends FrameLayout { xian = null; return true; } - return super.onTouchEvent(event); } @@ -108,7 +113,6 @@ public class fanjian extends FrameLayout { gestureDetector.onTouchEvent(event); return true; } - @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { diff --git a/app/src/main/res/layout/huabu.xml b/app/src/main/res/layout/huabu.xml deleted file mode 100644 index 3499493..0000000 --- a/app/src/main/res/layout/huabu.xml +++ /dev/null @@ -1,15 +0,0 @@ - - - - - - - \ No newline at end of file