cmhdev
cmhdev

Reputation: 43

Multiple Colors drawn on canvas

I am trying to draw with multiple colors on a canvas , the initial color works well but when it is switched to another color the line is drawn with the previous color however subsequent lines are drawn with the correct color. ie once if I switch from green to black the next line I draw is green but from then on its black. any help is appreciated.

public class DrawingPanel extends View implements OnTouchListener {
    private static final String TAG = "DrawView";
    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;
    private Canvas  mCanvas;
    private Path    mPath;
    private Paint   mPaint; 
    private Paint   bmPaint;
    private Paint   gmPaint;
    private ArrayList<Path> paths = new ArrayList<Path>();
    private Map<Path, Integer> colorsMap = new HashMap<Path,Integer>();
    private int selectedcolor;
    SharedPreferences settings= getSharedPreferences("Macaroni",0);
    private String selColr = settings.getString("DrawColor", "");

    public DrawingPanel(Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);

        this.setOnTouchListener(this);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(Color.BLACK);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(6);
        mCanvas = new Canvas();

         mPath = new Path();
         paths.add(mPath);
         colorsMap.put(mPath,getColorSel());

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

        @Override
        protected void onDraw(Canvas canvas) {
            for (Path p : paths){

                int grabColorFromHash =colorsMap.get(p);    
                mPaint.setColor(grabColorFromHash);
                 canvas.drawPath(p, 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;
            Log.i("touch_start","touch_start");
           }

    private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            Log.i("touch_move","touch_move");

            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);
            mPath = new Path();
            paths.add(mPath);
            colorsMap.put(mPath,getColorSel());
            mPaint.setColor(getColorSel());

        }
    public int getColorSel() 
    {

            selColr = settings.getString("DrawColor", "");

         if (selColr.equalsIgnoreCase("Black")){
            mPaint.setColor(Color.BLACK);
            selectedcolor = mPaint.getColor();
           }

           if (selColr.equalsIgnoreCase("Red"))
           { 
            mPaint.setColor(Color.RED);
            selectedcolor = mPaint.getColor();
           }

           if (selColr.equalsIgnoreCase("Green"))
           { 
            mPaint.setColor(Color.GREEN);
            selectedcolor = mPaint.getColor();
           }

        return selectedcolor;

    }
    @Override
    public boolean onTouch(View arg0, 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;
    }
}

Upvotes: 1

Views: 1162

Answers (1)

spartygw
spartygw

Reputation: 3462

In your touch_start you reset the path but you don't add it to the ArrayList until touch_up so I don't see how it's getting drawn at all while you are in the act of dragging your finger around.

Your onDraw is only drawing Paths as it iterates through the ArrayList. In other words, add some code to onDraw to also draw mPath along with all those already saved in the ArrayList.

It's hard to say without being able to run your stuff in a debugger but I think this might solve your problem.

Upvotes: 1

Related Questions