1

我正在制作一个绘图应用程序,以下代码可以正常工作,除了当我想更改油漆线的颜色时,之前绘制的所有线条都将更改为新颜色:

    private Bitmap bitmap; // drawing area for display or saving
    private Canvas bitmapCanvas; // used to draw on bitmap
    private Paint paintLine; // used to draw lines onto bitmap   
    private Path mPath; 
    private ArrayList<Path> paths = new ArrayList<Path>();
    private ArrayList<Path> undonePaths = new ArrayList<Path>();
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;    

    // DoodleView constructor initializes the DoodleView

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

       // set the initial display settings for the painted line
       paintLine = new Paint();
       paintLine.setAntiAlias(true); // smooth edges of drawn line
       paintLine.setDither(true);
       paintLine.setColor(Color.BLACK); // default color is black
       paintLine.setStyle(Paint.Style.STROKE); // solid line
       paintLine.setStrokeJoin(Paint.Join.ROUND);
       paintLine.setStrokeWidth(5); // set the default line width
       paintLine.setStrokeCap(Paint.Cap.ROUND); // rounded line ends

       bitmapCanvas = new Canvas();
       mPath = new Path();            
     } // end DoodleView constructor

     // Method onSizeChanged creates BitMap and Canvas after app displays

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

     @Override

     protected void onDraw(Canvas canvas) 
     {         
       for (Path p : paths){canvas.drawPath(p, paintLine);}  
       canvas.drawPath(mPath, paintLine);
       Log.i("OnDRAWING", "REACH ON DRAW");        
    } 

  // START TOUCH: handle touch event
     @Override

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

          switch (event.getAction())
          {
              case MotionEvent.ACTION_DOWN:
                  touch_start(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_MOVE:
                 touch_move(x, y);
                  invalidate();
                  break;
              case MotionEvent.ACTION_UP:
                  touch_up();
                  invalidate();
                  break;
          }
          return true;
    }

     private void touch_start(float x, float y) 
     {
       undonePaths.clear();
       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);      
        bitmapCanvas.drawPath(mPath, paintLine);// commit the path to our offscreen  
        paths.add(mPath);
        mPath = new Path(); 
    }

    public void onClickUndo() 
    { 
       if (paths.size()>0) 
        { 
           undonePaths.add(paths.remove(paths.size()-1));
           invalidate();
        }      
       else Toast.makeText(getContext(), "nothing more to undo", Toast.LENGTH_SHORT).show();  
    }

设置绘图颜色:

   public void setDrawingColor(int color) // set the painted line's color 
   {       
       paintLine.setColor(color);
   } 

如何修改上面的代码,使前一行固定为原来的颜色???谢谢!!

4

3 回答 3

1

您需要跟踪路径颜色并onDraw恢复最初用于绘制路径的颜色。

 protected void onDraw(Canvas canvas) 
 {          
   for (Path p : paths){canvas.drawPath(p, paintLine);}  
   paintLine.setColor(restoreColorForPath(p));
   canvas.drawPath(mPath, paintLine);
   Log.i("OnDRAWING", "REACH ON DRAW");        
} 

你只需要实现restoreColorForPath它是对等的部分storeColorForPath(path,color)(可能是一个简单的Map

这个类似的问题可以帮助你:如何用不同的颜色绘制多条线并撤消,重做android中的路径?

于 2013-02-21T12:31:53.080 回答
0
protected void onDraw(Canvas canvas) 
 {         
   for (Path p : paths){canvas.drawPath(p, paintLine);}  
   canvas.drawPath(mPath, paintLine);
   Log.i("OnDRAWING", "REACH ON DRAW");        
} 

试试这个代码而不是上面的

protected void onDraw(Canvas canvas) 
 {         
   canvas.drawBitmap(bitmapCanvas, 0,0,paintLine);
   canvas.drawPath(mPath, paintLine);
   Log.i("OnDRAWING", "REACH ON DRAW");        
}
于 2013-02-21T12:36:45.263 回答
0
@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    for (int i = 0; i < position1.size(); i += 2) {
        float x1 = position1.get(i);
        float y1 = position1.get(i + 1);
        float x2 = position2.get(i);
        float y2 = position2.get(i + 1);

        Paint paint = new Paint();
        if(words[a].getMEANING().equals(s2[b])){
            paint.setPathEffect(new CornerPathEffect(10));
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(13);
            paints.add(paint);
        }else{
            paint.setPathEffect(new CornerPathEffect(10));
            paint.setColor(Color.RED);
            paint.setStrokeWidth(13);
            paints.add(paint);

        }



        canvas.drawLine(x1,y1, x2,y2, paints.get(i));
    }
}
于 2019-10-22T13:28:07.070 回答