实现了拖拽线的功能

连线功能建议薄荷马上更新
This commit is contained in:
muqing 2023-11-25 22:17:06 +08:00
parent 297983ec75
commit cd8d3a30cd
3 changed files with 23 additions and 34 deletions

View File

@ -23,8 +23,8 @@ public class Edit extends ActivityFragment {
//绘制一个房间 //绘制一个房间
findViewById(R.id.button).setOnClickListener(view -> { findViewById(R.id.button).setOnClickListener(view -> {
fanjian button = new fanjian(Edit.this) fanjian button = new fanjian(Edit.this)
.setLayoutParams(200, 200) .setLayoutParams(260, 250)
.setXY(100, 100); .setXY(200, 100);
frameLayout.addView(button); frameLayout.addView(button);
}); });
} }

View File

@ -6,6 +6,7 @@ import android.graphics.Canvas;
import android.graphics.Color; import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.view.GestureDetector; import android.view.GestureDetector;
import android.view.Gravity;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
@ -31,37 +32,42 @@ public class fanjian extends FrameLayout {
xian xian; xian xian;
public dian(Context context) { public dian(Context context) {
super(context); super(context);
/* ViewGroup.LayoutParams layoutParams = fanjian.this.getLayoutParams(); ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams();
dian.this.setLayoutParams( LayoutParams layoutParams = new LayoutParams(aa.width /3,
new ViewGroup.LayoutParams(layoutParams.width / 2, aa.height / 3, Gravity.CENTER);
layoutParams.width / 2));*/ this.setLayoutParams(layoutParams);
// setBackgroundColor(Color.WHITE);
} }
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
Paint paint = new Paint(); @SuppressLint("DrawAllocation") Paint paint = new Paint();
paint.setColor(Color.WHITE); paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL); paint.setStyle(Paint.Style.FILL);
float cx = getWidth() / 2f; // 圆心 x 坐标 paint.setAntiAlias(true);
float cy = getHeight() / 2f; // 圆心 y 坐标 int centerX = getWidth()/2;
float radius = 26F; // 圆的半径 int centerY = getHeight()/2;
canvas.drawCircle(cx, cy, radius, paint); // 绘制圆点 int radius = Math.min(centerX, centerY);
canvas.drawCircle(centerX, centerY, radius, paint);
} }
float ax, ay,csx, csy;
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
ax = event.getX();
ay = event.getY();
switch (event.getAction()) { switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: case MotionEvent.ACTION_DOWN:
//在视图上面添加一条线 //在视图上面添加一条线
int[] location = new int[2]; int[] location = new int[2];
getLocationOnScreen(location); fanjian.this.getLocationOnScreen(location);
int x = location[0] + getWidth() / 2; csx= (int) fanjian.this.getX() + fanjian.this.getWidth() / 2;
int y = location[1] - getHeight() / 2; csy= (int) fanjian.this.getY() + fanjian.this.getHeight() / 2;
xian = new xian(getContext()).createNewLine(x, xian = new xian(getContext()).createNewLine(csx, csy);
y);
return true; return true;
case MotionEvent.ACTION_MOVE: 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; return true;
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
//删除线 //删除线
@ -70,7 +76,6 @@ public class fanjian extends FrameLayout {
xian = null; xian = null;
return true; return true;
} }
return super.onTouchEvent(event); return super.onTouchEvent(event);
} }
@ -108,7 +113,6 @@ public class fanjian extends FrameLayout {
gestureDetector.onTouchEvent(event); gestureDetector.onTouchEvent(event);
return true; return true;
} }
@SuppressLint("DrawAllocation") @SuppressLint("DrawAllocation")
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {

View File

@ -1,15 +0,0 @@
<?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">
<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="match_parent" />
</LinearLayout>