12

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

编码:

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
} 

OnDraw

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

}

触摸事件

@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 在执行时会设置 = bitmap OnTouchEvent touchStarted。我在里面放了一个吐司,当用户按下屏幕并开始绘图时,它成功呈现“备份 123”。

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

4

6 回答 6

7

我认为最简单的方法是拥有 2 个位图(1 个额外的备份位图用于恢复以前的状态)。

在开始新绘图之前,您需要保存位图的先前状态。

以下是我将如何修改您的代码:

  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 Bitmap bitmapBackup; 
  private Canvas bitmapCanvas; // used to draw on bitmap
  private Canvas bitmapBackupCanvas; 


  // remember last bitmap before new drawings...    
     private void touchStarted()   
     {
        bitmapBackupCanvas.drawBitmap(bitmap, 0, 0, null);
     } 
  // 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.drawBitmap(bitmapBackup, 0, 0, null); // restore from backup
        path.reset(); // reset the Path
     } 
于 2013-02-13T07:42:27.870 回答
5

这是一个旧帖子,但我也在寻找该问题的答案。我对该帖子选择的答案不满意,之后我自己找到了一个。我实际上认为将完整的位图作为备份在内存方面并不是很好,它会限制我们可以拥有的撤消步骤的数量。

我相信更好的解决方案是:

在您的班级中有一堆路径

private Stack<Path> m_pathHistory = new Stack<Path>();

在画布和画笔旁边(跳过初始化):

private Canvas m_drawingCanvas;
private Paint m_paint;

然后每次完成笔画(在修饰事件上)时,将路径的“克隆”添加到撤消历史记录:

m_pathHistory.Push(new Path(currentPath));

这是撤消功能:

public void Undo()
{
    if(m_pathHistory.Count > 0)
    {
        m_pathHistory.Pop(); // Remove the last path from the history

        m_drawingCanvas.DrawColor(Color.Transparent, PorterDuff.Mode.Clear); // Clear the canvas with a transparent color

        // Draw the paths which are still in the history
        foreach (Path p in m_pathHistory)
        {
            m_drawingCanvas.DrawPath(p, m_paint);
        }
     }
}

存储在内存中的路径比完整的位图小得多,因此我们可以拥有更大的历史记录。

于 2019-01-19T14:40:33.177 回答
1

乍一看,我看到以下问题:

通过在创建后立即添加空Pathpaths一旦撤消就会遇到问题:您 Path首先弹出空,使第一个撤消似乎不起作用。然后,如果您将其绘制到 中Path,则它不会添加到paths. 解决方案是在创建新路径之前添加已完成Path的路径。touch_up()

也就是去掉

paths.add(mPath);

从构造函数,并在touch_up(),改变

mPath = new Path();
paths.add(mPath);

paths.add(mPath);
mPath = new Path();

您还需要添加

canvas.drawPath(mPath, mPaint);

在你的for循环之后onDraw(),为了绘制 in-progress Path

undonePaths当用户再次开始绘图时,您并没有清空。

于 2013-02-13T07:36:34.620 回答
1
use path.reset()in the MotionEvent.ACTION_DOWN event of OnTouchEvent() method.

@Override
public boolean onTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mPath.reset();
            invalidate();
            break;
    }
    return true;

}
于 2016-09-29T05:18:46.913 回答
0

看到这里可能会使用完整的这个如何绘制和清除油漆

http://polamreddyn.blogspot.in/2012/11/simple-free-hand-paint-with-color.html

还有这个

http://polamreddyn.blogspot.in/2012/10/free-hand-graw.html

于 2013-02-13T07:27:11.883 回答
0

如果您使用PorterDuffXfermode,将视图保存到您的bitmapBackup, 而不是以前的位图

public void undo (){
    bitmap.eraseColor(getDrawingCacheBackgroundColor());
    mCanvas.drawBitmap(bitmapBackup, 0, 0, null);
    invalidate();
    mPath.reset();
    undofresh=true;
}


private void touch_start(float x, float y) {
    View v1 = this;
    v1.setDrawingCacheEnabled(true);
    this.bitmapBackup = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
}
于 2016-09-22T15:08:07.363 回答