Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8644715
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:22:05+00:00 2026-06-12T12:22:05+00:00

I am using this piece of code for user to draw o line with

  • 0

I am using this piece of code for user to draw o line with a finger:

public class DrawingView extends View {

private Paint paint;
private Path path;

public DrawingView(Context context , AttributeSet attrs) {
    super(context, attrs);

    this.paint = new Paint();
    this.paint.setAntiAlias(true);
    this.paint.setColor(Color.BLACK);
    this.paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeWidth(5f);

    this.path = new Path();
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawPath(path, paint);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float eventX = event.getX();
    float eventY = event.getY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            path.moveTo(eventX, eventY);
            return true;
        case MotionEvent.ACTION_MOVE:
        case MotionEvent.ACTION_UP:
            path.lineTo(eventX, eventY);
            break;
        default:
            return false;
    }

    // Schedules a repaint.
    invalidate();
    return true;
}

public void clear() {
    path.reset();
    invalidate();
}

public void setPaintColor(int color) {
    paint.setColor(color);
}

public int getCurrentPaintColor() {
    return paint.getColor();
}
}

With method setPaintColor() I am changing the color of the paint. But when I change the color, the whole drawing gets changed (even the lines that I drew before). How to change the color of the paint and leave previous drawings untached? I tried to create new Path, but then the previous drawing disappears.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:22:06+00:00Added an answer on June 12, 2026 at 12:22 pm

    You need to make a small datastructure for this, which will store both color and path of the drawing.. here is an example:

    class PaintClass
    {
        Path path;
        int Color;
        public int getColor() { return color; }
        public void setColor(int color){this.Color = color;}
        public int getPath() { return path; }
        public void setPath(Path path){this.path = path;}
    
        ...
        ...
        ...   
    }
    

    Now maintain an arraylist having objects of PaintClass.

    Implement it like this in onDraw method

    {    
        for(PaintClass item : yourArrayListOfPaintClassObjects)
        {
            //set Paint color like this
            setPaintColor(item.getPaintColor());
            canvas.drawPath(.....,paint);
        }
    }
    

    Note: On each new drawing add newely made PaintClass object in arraylist…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using this piece of code (the port used is 995) : Private
I'm using this piece of code from a fellow SO user: $(#rightSubMenu).mousemove(function(e){ console.log('executed'); var
Using this piece of code in the head of a page: <script type=text/javascript> function
I'm using this piece of code to try and save an image to the
I have this piece of code for render a popup, using f:ajax: <h:commandButton id
I am using std::unique_ptr in this piece of code which compiles and runs as
I am using iTextSharp in VB.net. I have this piece of code from java
everyone. This is a piece of code from using sqlite in android tutorial: return
I am using this piece of code to make text button on web page
I am using this piece of code to check for a network connection within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.