gpt4 book ai didi

java - 用户移动手指时如何在android中绘制矩形?

转载 作者:行者123 更新时间:2023-11-29 06:33:21 27 4
gpt4 key购买 nike

我想画一个矩形。第一个角应该是用户第一次触摸屏幕的地方。当用户移动他的手指时,它应该绘制矩形。这是 a link显示我想做什么的视频。但我不明白,也许你可以帮助我。我只想在白色背景上而不是图像上绘制该矩形。

我的代码:

package com.example.androiddrawing;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CanvasView extends View {

private Canvas canvas;
private Paint paint = new Paint();
private Paint paint2 = new Paint();
private Paint paint3 = new Paint();
private Path path = new Path();
private Point point = new Point();
private static List<Path> lines = new ArrayList<Path>();
private static List<Point> points = new ArrayList<Point>();
private float x, x2, xc, xd, x3, x4;
private float y, y2, yc, yd, y3, y4;
private boolean touchStarted = false;

public enum DrawMode {
FreeDrawMode, RectDrawMode
};

public static DrawMode currentDrawMode;

public void setDrawMode(DrawMode newDrawMode) {
this.currentDrawMode = newDrawMode;
}

public CanvasView(Context context, AttributeSet attrs) {
super(context, attrs);

paint.setAntiAlias(true);
paint.setStrokeWidth(5);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);

paint2.setAntiAlias(true);
paint2.setStrokeWidth(5);
paint2.setColor(Color.RED);
paint2.setStyle(Paint.Style.STROKE);
paint2.setStrokeJoin(Paint.Join.ROUND);

paint3.setAntiAlias(true);
paint3.setColor(Color.BLACK);
paint3.setStrokeWidth(10);
paint3.setStyle(Paint.Style.STROKE);
}

@Override
protected void onDraw(Canvas canvas) {

for (Path p : lines)
canvas.drawPath(p, paint);
canvas.drawPath(path, paint2);

for (Point point : points)
canvas.drawCircle(point.x, point.y, 0, paint);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
x = event.getX();
y = event.getY();

System.out.println(currentDrawMode);
if (currentDrawMode == DrawMode.FreeDrawMode) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Set a new starting point
paint2.setColor(Color.RED);
path = new Path();
path.moveTo(x, y);
touchStarted = true;


break;
// return true;
case MotionEvent.ACTION_MOVE:
// Connect the points
touchStarted = false;
path.lineTo(x, y);

break;
case MotionEvent.ACTION_UP:
if (touchStarted) {
point = new Point();
point.x = (int) x;
point.y = (int) y;
paint2.setColor(Color.BLACK);
points.add(point);
touchStarted = false;
System.out.println("siin");
} else {
System.out.println("seal");
paint2.setColor(Color.BLACK);
lines.add(path);
}
break;
default:
return false;
}
} else if (currentDrawMode == DrawMode.RectDrawMode) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// Set a new starting point
paint3.setColor(Color.RED);
//CODE HERE

break;
// return true;
case MotionEvent.ACTION_MOVE:

//CODE HERE


break;
case MotionEvent.ACTION_UP:

//CODE HERE


break;
default:
return false;
}
}
// Makes our view repaint and call onDraw
invalidate();
return true;
}

}

我应该在注释//CODE HERE 的地方写代码,但我真的不明白,我必须如何绘制一个矩形。

最佳答案

您可以使用以下代码。希望对您有所帮助。

public class DrawSample extends View {

int mStartX;
int mStartY;
int mEndX;
int mEndY;

Paint mPaint = new Paint();

int mSelectedColor = Color.BLACK;

public DrawSample(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

mPaint.setColor(mSelectedColor);
mPaint.setStrokeWidth(5);
mPaint.setStyle(Paint.Style.STROKE);

setFocusable(true);
}

public DrawSample(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}

@Override
public boolean onTouchEvent(MotionEvent event) {

switch (event.getActionMasked()) {

case MotionEvent.ACTION_DOWN:

mStartX = (int) event.getX();
mStartY = (int) event.getY();

break;

case MotionEvent.ACTION_MOVE:

mEndX = (int) event.getX();
mEndY = (int) event.getY();

invalidate();

break;

case MotionEvent.ACTION_UP:


mEndX = (int) event.getX();
mEndY = (int) event.getY();

invalidate();

break;

default:

super.onTouchEvent(event);

break;
}

return true;
}

@Override
protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

canvas.drawRect(mStartX, mStartY, mEndX, mEndY, mPaint);
}
}

关于java - 用户移动手指时如何在android中绘制矩形?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26869822/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com