gpt4 book ai didi

java - 如何绘制不同颜色的多条线并撤消,重做android中的路径?

转载 作者:太空宇宙 更新时间:2023-11-03 12:21:49 27 4
gpt4 key购买 nike

我想在 View 上用不同的颜色绘制多条线,然后撤消,重做android中的路径。

我使用位图绘制选项,每条路径都有独特的颜色,但撤消、重做不起作用。

这是我的位图绘制代码:

public MyView(Context context, Object object) {
super(context);
setFocusable(true);
setFocusableInTouchMode(true);

mPath = new Path();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(0xFFFFFF00);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);

mBitmap = Bitmap.createBitmap(320, 480, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
}

protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (Path p : paths)
{
canvas.drawPath(p, mPaint);
}
canvas.drawPath(mPath, mPaint);
}

public boolean onTouchEvent(MotionEvent event)
{
float x = event.getX();
float y = event.getY();
int action = event.getAction();
int action1=event.getAction();
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
undonePaths.clear();
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
startPoint = new PointF(event.getX(), event.getY());
endPoint = new PointF();
invalidate();
// isDrawing = true;
break;
case MotionEvent.ACTION_MOVE:
float dx = Math.abs(x - mX);
System.out.println("action move");
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE)
{
// currentDrawingPath.path.quadTo(mX,mY,(x + mX)/2, (y + mY)/2);
}
mX = x;
mY = y;
endPoint.x = event.getX();
endPoint.y = event.getY();
isDrawing = true;
invalidate();
break;
case MotionEvent.ACTION_UP:
mPath.lineTo(mX, mY);
paths.add(mPath);
mPath = new Path();
// mCanvas.drawPath(mPath, ppaint);
endPoint.x = event.getX();
endPoint.y = event.getY();
isDrawing = false;
invalidate();
break;
default:
break;
}
}

output of bitmappaint canvas

不使用位图时,如果我为路径选择蓝色,则我会遇到颜色问题,这意味着所有先前的路径都将更改为蓝色;

这是我的代码

protected void onDraw(Canvas canvas) 
{
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (Path p : paths)
{
canvas.drawPath(p, mPaint);
}
canvas.drawPath(mPath, mPaint);
}

output of without bitmap using canvas drawing

谁能帮我在android中用不同颜色的独特路径绘制多条路径?

最佳答案

当您处理 MotionEvent.ACTION_UP 时:您需要保存用于绘制路径的颜色,如下所示:

case MotionEvent.ACTION_UP:
paths.add(mPath);
colorsMap.put(mPath,selectedColor); // store the color of mPath
...

在绘制路径之前,您需要设置绘制颜色:

protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
for (Path p : paths)
{
mPaint.setColor(colorsMap.get(p));
canvas.drawPath(p, mPaint);
}
mPaint.setColor(selectedColor);
canvas.drawPath(mPath, mPaint);
}

colorsMap 是一个简单的实例变量:

private Map<Path, Integer> colorsMap = new HashMap<Path, Integer>();    

要实现撤消/重做功能,您只需从路径中删除最后一个元素(并将其存储在 undoneList 中,以便在重做时可以恢复它)。见Android Canvas Redo and Undo Operation

关于java - 如何绘制不同颜色的多条线并撤消,重做android中的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13705973/

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