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