refactor: 使房间组件持有画板。
This commit is contained in:
parent
cd8d3a30cd
commit
53290aea92
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -14,7 +14,8 @@ import com.muqingedit.xian;
|
|||
|
||||
public class Edit extends ActivityFragment {
|
||||
@SuppressLint("StaticFieldLeak")
|
||||
public static FrameLayout frameLayout;
|
||||
public FrameLayout frameLayout;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
|
@ -22,19 +23,12 @@ public class Edit extends ActivityFragment {
|
|||
frameLayout = findViewById(R.id.view);
|
||||
//绘制一个房间
|
||||
findViewById(R.id.button).setOnClickListener(view -> {
|
||||
fanjian button = new fanjian(Edit.this)
|
||||
fanjian button = new fanjian(Edit.this).setPaletteView(frameLayout)
|
||||
.setLayoutParams(260, 250)
|
||||
.setXY(200, 100);
|
||||
frameLayout.addView(button);
|
||||
});
|
||||
}
|
||||
|
||||
public static void addView(View view) {
|
||||
frameLayout.addView(view);
|
||||
}
|
||||
|
||||
public static Context getContext() {
|
||||
return frameLayout.getContext();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -18,6 +18,18 @@ import com.muqingedit.activity.Edit;
|
|||
|
||||
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;
|
||||
|
||||
private int X, Y;
|
||||
|
@ -30,6 +42,7 @@ public class fanjian extends FrameLayout {
|
|||
|
||||
private class dian extends View {
|
||||
xian xian;
|
||||
|
||||
public dian(Context context) {
|
||||
super(context);
|
||||
ViewGroup.LayoutParams aa = fanjian.this.getLayoutParams();
|
||||
|
@ -38,6 +51,7 @@ public class fanjian extends FrameLayout {
|
|||
this.setLayoutParams(layoutParams);
|
||||
// setBackgroundColor(Color.WHITE);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
super.onDraw(canvas);
|
||||
|
@ -50,7 +64,9 @@ public class fanjian extends FrameLayout {
|
|||
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();
|
||||
|
@ -62,7 +78,7 @@ public class fanjian extends FrameLayout {
|
|||
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);
|
||||
xian = new xian(getContext()).createNewLine(paletteView, csx, csy);
|
||||
return true;
|
||||
case MotionEvent.ACTION_MOVE:
|
||||
//绘制线
|
||||
|
@ -72,7 +88,7 @@ public class fanjian extends FrameLayout {
|
|||
case MotionEvent.ACTION_UP:
|
||||
//删除线
|
||||
xian.clearCurrentLine();
|
||||
Edit.frameLayout.removeView(xian);
|
||||
paletteView.removeView(xian);
|
||||
xian = null;
|
||||
return true;
|
||||
}
|
||||
|
@ -83,6 +99,7 @@ public class fanjian extends FrameLayout {
|
|||
|
||||
private GestureDetector gestureDetector;
|
||||
private boolean Long = false;
|
||||
|
||||
private void init() {
|
||||
// 设置背景颜色为红色
|
||||
setBackgroundColor(Color.RED);
|
||||
|
@ -95,6 +112,7 @@ public class fanjian extends FrameLayout {
|
|||
Long = !Long;
|
||||
// 处理长按事件
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onScroll(@NonNull MotionEvent e1, @NonNull MotionEvent e2, float distanceX, float distanceY) {
|
||||
if (Long) {
|
||||
|
@ -113,6 +131,7 @@ public class fanjian extends FrameLayout {
|
|||
gestureDetector.onTouchEvent(event);
|
||||
return true;
|
||||
}
|
||||
|
||||
@SuppressLint("DrawAllocation")
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
|
@ -153,6 +172,7 @@ public class fanjian extends FrameLayout {
|
|||
super.setX(x);
|
||||
X = (int) x;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setY(float y) {
|
||||
super.setY(y);
|
||||
|
|
|
@ -7,6 +7,7 @@ import android.graphics.Color;
|
|||
import android.graphics.Paint;
|
||||
import android.view.MotionEvent;
|
||||
import android.view.View;
|
||||
import android.widget.FrameLayout;
|
||||
|
||||
import com.muqingedit.activity.Edit;
|
||||
|
||||
|
@ -32,18 +33,25 @@ public class xian extends View {
|
|||
linePaint.setStrokeWidth(5f);
|
||||
}
|
||||
|
||||
public xian createNewLine(float x, float y) {
|
||||
startX = x;startY = y;currentX = x;currentY = y;
|
||||
Edit.addView(this);
|
||||
public xian createNewLine(FrameLayout frameLayout, float x, float y) {
|
||||
startX = x;
|
||||
startY = y;
|
||||
currentX = x;
|
||||
currentY = y;
|
||||
frameLayout.addView(this);
|
||||
invalidate();//创建一个新的线
|
||||
return this;
|
||||
}
|
||||
|
||||
public void updateCurrentLine(float x, float y) {
|
||||
currentX = x;currentY = y;
|
||||
currentX = x;
|
||||
currentY = y;
|
||||
invalidate();//绘制中
|
||||
}
|
||||
|
||||
public void clearCurrentLine() {
|
||||
currentX = startX;currentY = startY;
|
||||
currentX = startX;
|
||||
currentY = startY;
|
||||
invalidate();//关闭绘制
|
||||
}
|
||||
|
||||
|
|
Reference in New Issue
Block a user