gpt4 book ai didi

android canvas 删除之前绘制的路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:49 29 4
gpt4 key购买 nike

<分区>

我正在制作一个绘图应用程序,并想实现一个撤消功能以删除紧邻的先前绘制的路径。

编码:

private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points
private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintScreen; // use to draw bitmap onto screen
private Paint paintLine; // used to draw lines onto bitmap

public DrawView(Context context, AttributeSet attrs)
{
super(context, attrs); // pass context to View's constructor
this.context_new=context;

paintScreen = new Paint(); // used to display bitmap onto screen

// set the initial display settings for the painted line
paintLine = new Paint();
paintLine.setAntiAlias(true); // smooth edges of drawn line
paintLine.setColor(Color.BLACK); // default color is black
paintLine.setStyle(Paint.Style.STROKE); // solid line
paintLine.setStrokeWidth(5); // set the default line width
paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends
pathMap = new HashMap<Integer, Path>();
previousPointMap = new HashMap<Integer, Point>();
} // end DrawView constructor

@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen);
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key), paintLine);
}

// called when the user finishes a touch
private void touchEnded(int lineID)
{
Path path = pathMap.get(lineID); // get the corresponding Path
bitmapCanvas.drawPath(path, paintLine); // draw to bitmapCanvas
path.reset(); // reset the Path
rememberLineId = lineID;
} // end method touch_ended

//undo
private void undo()
{
Path path = pathMap.get(rememberLineId); // get the corresponding Path
pathMap.remove(rememberLineId);
bitmapCanvas.clearPath(path, paintLine);
path.reset(); // reset the Path
}

问题:

不过,好像没有bitmapCanvas.clearPath这个方法?如果可以,怎么修改?

代码修改:

声明:

private Bitmap bitmap; // drawing area for display or saving
private Canvas bitmapCanvas; // used to draw on bitmap
private Paint paintScreen; // use to draw bitmap onto screen
private Paint paintLine; // used to draw lines onto bitmap
private HashMap<Integer, Path> pathMap; // current Paths being drawn
private HashMap<Integer, Point> previousPointMap; // current Points

private Bitmap bitmapBackup;

OnSizeChanged

@Override
public void onSizeChanged(int w, int h, int oldW, int oldH)
{
super.onSizeChanged(w, h, oldW, oldH);
DoodlzViewWidth = w;
DoodlzViewHeight = h;

bitmapBackup = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);
bitmap = Bitmap.createBitmap(getWidth(), DoodlzViewHeight, Bitmap.Config.ARGB_8888);

bitmapCanvas = new Canvas(bitmap);
bitmap .eraseColor(Color.WHITE); // erase the BitMap with white
bitmapBackup.eraseColor(Color.WHITE);
}

FirsttoBackup 方法,将在下面的 TouchedStart 执行时调用

public void firsttobackup()
{
bitmapBackup=bitmap;
Toast message = Toast.makeText(getContext(), "backuped 123", Toast.LENGTH_SHORT);
message.show(); //THIS TOAST CAN BE SUCESSFULLY PRESENTED when touching screen starting to draw
}

画图

@Override
protected void onDraw(Canvas canvas)
{
canvas.drawBitmap(bitmap, 0, 0, paintScreen);
for (Integer key : pathMap.keySet())
canvas.drawPath(pathMap.get(key), paintLine);

}

OnTouchEvent

@Override
public boolean onTouchEvent(MotionEvent event)
{
int action = event.getActionMasked(); // event type
int actionIndex = event.getActionIndex(); // pointer (i.e., finger)

if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_POINTER_DOWN)
{
firsttobackup(); //TOAST CAN SHOW "BACKUP 123"

touchStarted(event.getX(actionIndex), event.getY(actionIndex),
event.getPointerId(actionIndex));
}

撤消:用户按下撤消按钮将调用此操作

public void undo()
{
bitmap = bitmapBackup.copy(Bitmap.Config.ARGB_8888, true);
bitmapCanvas = new Canvas(bitmap);
}

修改问题:

现在使用方法 firsttobackup(),这样 bitmapBackup 在执行 OnTouchEvent touchStarted 时会设置 = bitmap。我已经在其中 toast ,当用户按下屏幕并开始绘制时,它成功呈现“备份 123”。

当用户点击撤消按钮时,它会调用撤消方法,但是现在按下撤消按钮,看不到任何 Action ...为什么?

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