gpt4 book ai didi

android - 如何在 Android 的绘画应用程序中实现撤消功能?

转载 作者:行者123 更新时间:2023-11-29 02:06:16 26 4
gpt4 key购买 nike

我想在我的应用程序中执行撤消功能。为此,我搜索了我的网络,发现采用 x、y 点的数组列表,我在下面的代码中完成了我无法撤消绘图??

public Drawview(Context context, float x, float y, int r) {
super(context);


mBitmap = Bitmap.createBitmap(800, 1200, Bitmap.Config.ARGB_8888);
// mainbitmap.add(mBitmap);
//System.out.println("**mBitmapPaint***"+mBitmapPaint);
mCanvas = new Canvas(mBitmap);
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);



}

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

@Override
protected void onDraw(Canvas canvas) {

System.out.println("***Drawinglines****"+Drawinglines);

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);



}


private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}

private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
mX = x;
mY = y;
}
}

private void touch_up() {


mPath.lineTo(mX, mY);
mCanvas.drawPath(mPath, mPaint);
/// path.
//graphics.add(mPath);
mPath.reset();
}

// events when touching the screen
public boolean onTouchEvent(MotionEvent event) {

event.getAction();
int eventaction = event.getAction();

int X = (int)event.getX();
int Y = (int)event.getY();

switch (eventaction ) {

case MotionEvent.ACTION_DOWN:

{
touch_start(X, Y);
}
invalidate();
break;


case MotionEvent.ACTION_MOVE:

System.out.println("***ACTION_MOVE**************"+X+"************"+Y);
if((Drawinglines.equals("Pen"))||(Drawinglines.equals("Erase")))
{
touch_move(X, Y);
temp_point.add(new Point(X,Y));

}
invalidate();
break;

case MotionEvent.ACTION_UP:

main_Points.add(temp_point);
temp_point = null;

touch_up();

invalidate();
break;
}

return true;

}




public void undopath()
{
main_Points.remove(main_Points.size()-1);
invalidate();
}

}
@Override
public void onClick(View v) {


if(v == Undo)
{
Drawview abc=new Drawview();
abc.undopath();
}

但是撤消撤消功能不起作用。

最佳答案

private Slate mSlate;
private TiledBitmapCanvas mTiledCanvas;

public void clickUndo(View unused) {

mSlate.undo();
}


public void undo() {
if (mTiledCanvas == null) {
Log.v(TAG, "undo before mTiledCanvas inited");
}
mTiledCanvas.step(-1);

invalidate();
}

关于android - 如何在 Android 的绘画应用程序中实现撤消功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9872579/

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