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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:54:07+00:00 2026-06-03T18:54:07+00:00

I’m trying to develop a simple draw-on-touch and I want to draw the different

  • 0

I’m trying to develop a simple “draw-on-touch” and I want to draw the different paths that the user places in an onTouch listener.
I’ve got a simple problem. When I draw the paths I obtain a single path drawn in the points of the last onTouch entry.
I think the paths are all drawn over the last path because I use a Paint with a 150 alpha and it looks more solid in the second and succesive onTouch entries.

How could I solve that? Thanks!

public class PaintView extends View implements OnTouchListener {


List<List<Point>> paths = new ArrayList<List<Point>>();
List<Point> points = new ArrayList<Point>();
Paint paintLine = new Paint();
Paint paintCircle = new Paint();

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

    this.setOnTouchListener(this);

    paintLine = new Paint(Paint.ANTI_ALIAS_FLAG);
    paintLine.setStyle(Paint.Style.STROKE);
    paintLine.setStrokeWidth(10);
    paintLine.setColor(Color.GREEN);
    paintLine.setAlpha(150);
}

public void onDraw(Canvas canvas) {
    Path path = new Path();
    List<Point> pointsList;
    List<Path> pathList = new ArrayList<Path>();
    Point point = new Point();

    for (int i = 0; i < paths.size(); i++) {
        pointsList = paths.get(i);

        path.reset();
        boolean first = true;

        for (int j = 0; j < points.size(); j+=2 ) {
            point = pointsList.get(j);

            if (first) {
                first = false;
                path.moveTo(point.x, point.y);
            } else if (j < pointsList.size() - 1 ) {
                Point nextPoint = pointsList.get(j+1);
                path.quadTo(point.x, point.y, nextPoint.x, nextPoint.y);
            } else {
                path.lineTo(point.x, point.y);
            }

        }

        pathList.add(path);
    }

    for (Path pathDraw : pathList) {
        canvas.drawPath(pathDraw, paintLine);
    }

}

public boolean onTouch(View view, final MotionEvent event) {
    Point point = new Point();
    point.x = event.getX();
    point.y = event.getY();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        // DO SOMETHING
        points.clear();
        points.add(point);
        break;
    case MotionEvent.ACTION_MOVE:
        points.add(point);
        break;
    case MotionEvent.ACTION_UP:
        points.add(point);
        paths.add(points);
        break;
    default:
        return false;
    }
    invalidate();
    return true;


}
}
  • 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-03T18:54:11+00:00Added an answer on June 3, 2026 at 6:54 pm

    The problem is that you are drawing paths constructed with the points in the points list repeatedly.

    Note that invalidate() call in onTouch, each of this call will trigger a redraw event, which causes onDraw to be called, everything looks fine at this point. The problem is you only did points.clear() on ACTION_DOWN event, but you also should have cleared them on ACTION_MOVE and ACTION_UP events. Instead, you keep all the points collected previously and draw the paths constructed with all those points in onDraw. So you are effectively drawing repeatedly some of the paths constructed with the points collected previously.

    Note in a drag motion, there will be 1 ACTION_DOWN, N ACTION_MOVE, 0 or 1 ACTION_UP events.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.