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 8377035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:34:06+00:00 2026-06-09T15:34:06+00:00

DrawView.java public class DrawView extends View implements OnTouchListener { private Canvas mCanvas; private Path

  • 0

DrawView.java

public class DrawView extends View implements OnTouchListener {
private Canvas mCanvas;
private Path mPath;
public Paint mPaint;
ArrayList<Path> paths = new ArrayList<Path>();
private ArrayList<Path> undonePaths = new ArrayList<Path>();

private MaskFilter mEmboss;
private MaskFilter mBlur;

private Bitmap im;

public DrawView(Context context) {
    super(context);
    setFocusable(true);
    setFocusableInTouchMode(true);
    this.setOnTouchListener(this);

    paths.clear();
    undonePaths.clear();

    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.BLUE);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(4);
    mEmboss = new EmbossMaskFilter(new float[] { 1, 1, 1 }, 0.4f, 6, 3.5f);

    mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

    mCanvas = new Canvas();
    mPath = new Path();
    // paths.add(mPath);

    im = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ic_launcher);

}

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

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(Share.cColor);

    for (Path p : paths) {

        canvas.drawPath(p, mPaint);
    }

    canvas.drawPath(mPath, mPaint);

}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {

    mPaint.setColor(Share.dColor);
    undonePaths.clear();
    mPath.reset();
    mPath.moveTo(x, y);
    mX = x;
    mY = y;

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
}

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);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    // kill this so we don't double draw
    paths.add(mPath);
    mPath = new Path();

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
}

public void onClickUndo() {

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
    if (paths.size() > 0) {
        undonePaths.add(paths.remove(paths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

public void onClickRedo() {

    Log.e("", "pathsize:::" + paths.size());
    Log.e("", "undonepathsize:::" + undonePaths.size());
    if (undonePaths.size() > 0) {
        paths.add(undonePaths.remove(undonePaths.size() - 1));
        invalidate();
    } else {

    }
    // toast the user
}

@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;
}

}

I am trying to draw finger paint with different colors but whenever i change the color of paint then all previous path or drawing get and draw with updated color i want to draw with different colors how can i do that ? please give me some solutions for this.

  • 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-09T15:34:08+00:00Added an answer on June 9, 2026 at 3:34 pm

    To do this, you’d have to create a new Paint for every object drawn. This is because when the Canvas redraws, it references that same Paint object every time, so all paths will use this paint.

    Firstly, I would change your paths array to contain both a Paint and a Path. You can achieve this using the Android type Pair.

    ArrayList<Pair<Path, Paint>> paths = new ArrayList<Pair<Path, Paint>>();
    

    You will also have to convert your undonePaths variable in this manner.

    Then, in your touch_up() method, you need to add this new Paint object.

    Paint newPaint = new Paint(mPaint); // Clones the mPaint object
    paths.add(new Pair<Path, Paint>(mPath, newPaint));
    

    Lastly, your loop has to be adjusted for this as well:

    for (Pair<Path, Paint> p : paths) {
        canvas.drawPath(p.first, p.second);
    }
    

    This is quite memory intensive, so you will have to take good care to reset these items when they’re no longer in use, but to have so many different colors, you must have all of these different Paint objects.

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

Sidebar

Related Questions

public class DrawView extends View { private ColorBall[] colorballs = new ColorBall[3]; // array
I have a class Drawview: public class DrawView extends View { private ColorBall[] colorballs
I have this custom class ... public class DrawView extends ImageView When I set
I have 2 classes, Main as main activity and DrawView (extends View) as a
I've created an activity that calls a DrawView extends View, I draw on a
For a single view app, a DrawView class is added, which is a subclass
I have a custom view called DrawView created in the main activity. I have
at android.hardware.Camera.native_setParameters(Native Method) at android.hardware.Camera.setParameters(Camera.java:647) at com.CameraApp.Preview.surfaceChanged(Preview.java:67) at android.view.SurfaceView.updateWindow(SurfaceView.java:538) at android.view.SurfaceView.dispatchDraw(SurfaceView.java:339) at android.view.ViewGroup.drawChild(ViewGroup.java:1638) at
I have DrawView. If I touch this view it draws small circles. I wont
I have a class that subclasses UIView called DrawView. This class contains custom drawing

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.