refactor: 使房间组件持有画板。

This commit is contained in:
Cold-Mint 2023-11-26 00:33:39 +08:00
parent cd8d3a30cd
commit 53290aea92
7 changed files with 44 additions and 22 deletions

View File

@ -14,7 +14,8 @@ import com.muqingedit.xian;
public class Edit extends ActivityFragment { public class Edit extends ActivityFragment {
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
public static FrameLayout frameLayout; public FrameLayout frameLayout;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -22,19 +23,12 @@ public class Edit extends ActivityFragment {
frameLayout = findViewById(R.id.view); frameLayout = findViewById(R.id.view);
//绘制一个房间 //绘制一个房间
findViewById(R.id.button).setOnClickListener(view -> { findViewById(R.id.button).setOnClickListener(view -> {
fanjian button = new fanjian(Edit.this) fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout)
.setLayoutParams(260, 250) .setLayoutParams(260, 250)
.setXY(200, 100); .setXY(200, 100);
frameLayout.addView(button); frameLayout.addView(button);
}); });
} }
public static void addView(View view) {
frameLayout.addView(view);
}
public static Context getContext() {
return frameLayout.getContext();
}
} }

View File

@ -18,6 +18,18 @@ import com.muqingedit.activity.Edit;
public class fanjian extends FrameLayout { 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; public static int id = 0;
private int X, Y; private int X, Y;
@ -30,6 +42,7 @@ public class fanjian extends FrameLayout {
private class dian extends View { private class dian extends View {
xian xian; xian xian;
public dian(Context context) { public dian(Context context) {
super(context); super(context);
ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams(); ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams();
@ -38,6 +51,7 @@ public class fanjian extends FrameLayout {
this.setLayoutParams(layoutParams); this.setLayoutParams(layoutParams);
// setBackgroundColor(Color.WHITE); // setBackgroundColor(Color.WHITE);
} }
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
super.onDraw(canvas); super.onDraw(canvas);
@ -50,7 +64,9 @@ public class fanjian extends FrameLayout {
int radius = Math.min(centerX, centerY); int radius = Math.min(centerX, centerY);
canvas.drawCircle(centerX, centerY, radius, paint); canvas.drawCircle(centerX, centerY, radius, paint);
} }
float ax, ay, csx, csy; float ax, ay, csx, csy;
@Override @Override
public boolean onTouchEvent(MotionEvent event) { public boolean onTouchEvent(MotionEvent event) {
ax = event.getX(); ax = event.getX();
@ -62,7 +78,7 @@ public class fanjian extends FrameLayout {
fanjian.this.getLocationOnScreen(location); fanjian.this.getLocationOnScreen(location);
csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2; csx = (int) fanjian.this.getX() + fanjian.this.getWidth() / 2;
csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2; csy = (int) fanjian.this.getY() + fanjian.this.getHeight() / 2;
xian = new xian(getContext()).createNewLine(csx, csy); xian = new xian(getContext()).createNewLine(paletteView, csx, csy);
return true; return true;
case MotionEvent.ACTION_MOVE: case MotionEvent.ACTION_MOVE:
//绘制线 //绘制线
@ -72,7 +88,7 @@ public class fanjian extends FrameLayout {
case MotionEvent.ACTION_UP: case MotionEvent.ACTION_UP:
//删除线 //删除线
xian.clearCurrentLine(); xian.clearCurrentLine();
Edit.frameLayout.removeView(xian); paletteView.removeView(xian);
xian = null; xian = null;
return true; return true;
} }
@ -83,6 +99,7 @@ public class fanjian extends FrameLayout {
private GestureDetector gestureDetector; private GestureDetector gestureDetector;
private boolean Long = false; private boolean Long = false;
private void init() { private void init() {
// 设置背景颜色为红色 // 设置背景颜色为红色
setBackgroundColor(Color.RED); setBackgroundColor(Color.RED);
@ -95,6 +112,7 @@ public class fanjian extends FrameLayout {
Long = !Long; Long = !Long;
// 处理长按事件 // 处理长按事件
} }
@Override @Override
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) { public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
if (Long) { if (Long) {
@ -113,6 +131,7 @@ 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) {
@ -153,6 +172,7 @@ public class fanjian extends FrameLayout {
super.setX(x); super.setX(x);
X = (int) x; X = (int) x;
} }
@Override @Override
public void setY(float y) { public void setY(float y) {
super.setY(y); super.setY(y);

View File

@ -7,6 +7,7 @@ import android.graphics.Color;
import android.graphics.Paint; import android.graphics.Paint;
import android.view.MotionEvent; import android.view.MotionEvent;
import android.view.View; import android.view.View;
import android.widget.FrameLayout;
import com.muqingedit.activity.Edit; import com.muqingedit.activity.Edit;
@ -32,18 +33,25 @@ public class xian extends View {
linePaint.setStrokeWidth(5f); linePaint.setStrokeWidth(5f);
} }
public xian createNewLine(float x, float y) { public xian createNewLine(FrameLayout frameLayout, float x, float y) {
startX = x;startY = y;currentX = x;currentY = y; startX = x;
Edit.addView(this); startY = y;
currentX = x;
currentY = y;
frameLayout.addView(this);
invalidate();//创建一个新的线 invalidate();//创建一个新的线
return this; return this;
} }
public void updateCurrentLine(float x, float y) { public void updateCurrentLine(float x, float y) {
currentX = x;currentY = y; currentX = x;
currentY = y;
invalidate();//绘制中 invalidate();//绘制中
} }
public void clearCurrentLine() { public void clearCurrentLine() {
currentX = startX;currentY = startY; currentX = startX;
currentY = startY;
invalidate();//关闭绘制 invalidate();//关闭绘制
} }