2

I am making an android colouring app which uses the draw feature. I have it so that there are multiple colours that the user can choose from and they can select a colour and draw however when they select a new colour it changes all previously drawn lines as well not just the new lines being drawn. Any help on how I can rectify this and have multiple colours used on the drawing is greatly appreciated. Some of my code is below: The first is the colouring class public class Colouring extends View {

// setup initial color
private int paintColor = Color.BLACK;
private Paint drawPaint;
private Path drawPath;
// stores the path
//private Path path = new Path();

public Colouring(Context context, AttributeSet attrs) {
    super(context, attrs);
    setFocusable(true);
    setFocusableInTouchMode(true);
    setupcolour();
}

private void setupcolour() {
    // Setup paint with color and stroke styles
    drawPath = new Path();
    drawPaint = new Paint();
    drawPaint.setColor(paintColor);
    drawPaint.setAntiAlias(true);
    drawPaint.setStrokeWidth(15);
    drawPaint.setStyle(Paint.Style.STROKE);
    drawPaint.setStrokeJoin(Paint.Join.ROUND);
    drawPaint.setStrokeCap(Paint.Cap.ROUND);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(drawPath, drawPaint);

}

public void setColor(String newColor) {
    invalidate();
    paintColor = Color.parseColor(newColor);
    drawPaint.setColor(paintColor);

}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float pointX = event.getX();
    float pointY = event.getY();
    // Checks for the event that occurs
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            drawPath.moveTo(pointX, pointY);
            return true;
        case MotionEvent.ACTION_MOVE:
            drawPath.lineTo(pointX, pointY);
            break;
        default:
            return false;
    }
    // Force a view to draw again
    invalidate();
    return true;
}

}

and the second is the main activity where the drawing happens. public class ColourImage extends Activity {

private Colouring drawView;
private ImageButton currcolour, save;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_colour_image);
    drawView = (Colouring)findViewById(R.id.colouringBook);
    LinearLayout paintLayout = (LinearLayout) findViewById(R.id.colours);
    currcolour = (ImageButton) paintLayout.getChildAt(0);
    currcolour.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

    save = (ImageButton)findViewById(R.id.save);
}

public void saveclicked(View view){
    drawView.setDrawingCacheEnabled(true);
    String saveImage = MediaStore.Images.Media.insertImage(
            getContentResolver(), drawView.getDrawingCache(),
            UUID.randomUUID().toString()+".png", "colouredImage");
    if(saveImage!=null){
        Toast savedToast = Toast.makeText(getApplicationContext(),
                "Your colouring has been saved to your phone", Toast.LENGTH_SHORT);
        savedToast.show();
    }
    else{
        Toast unsavedToast = Toast.makeText(getApplicationContext(),
                "Your colouring was not saved", Toast.LENGTH_SHORT);
        unsavedToast.show();
    }
    drawView.destroyDrawingCache();
}

public void paintClicked(View view){
    if(view!=currcolour){
        ImageButton colourImage = (ImageButton)view;
        String color = view.getTag().toString();

        drawView.setColor(color);
        colourImage.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
        currcolour.setImageDrawable(getResources().getDrawable(R.drawable.paint));
        currcolour=(ImageButton)view;
    }
}

}

Thanks again :)

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
anzeme
  • 19
  • 1
  • 1
    I think you need to do `new Paint()` and maybe `Path` everytime you pick a new color because otherwise, as you said, it changes the color for all previously drawn lines because the object reference doesn't change – OneCricketeer Feb 06 '16 at 16:31
  • ok that makes sense, but im slightly confused as to where i have to add the new Paint() and is it literally just that code that is missing? – anzeme Feb 06 '16 at 16:36
  • Wherever you do `drawPaint.setColor` you need a different reference to `drawPaint` in order to not set the colors for everything else you drew – OneCricketeer Feb 06 '16 at 16:40
  • I have tried this however it now removes the previously drawn line and only lets me then draw with a weird fill line which fills around the mouse and removes certain parts of it – anzeme Feb 06 '16 at 18:56

0 Answers0