实现了拖拽线的功能

连线功能建议薄荷马上更新
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 -> {
fanjian button = new fanjian(Edit.this)
.setLayoutParams(200, 200)
.setXY(100, 100);
.setLayoutParams(260, 250)
.setXY(200, 100);
frameLayout.addView(button);
});
}

View File

@ -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) {

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>