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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:26:56+00:00 2026-05-28T01:26:56+00:00

How do I add, say, a line to an already drawn view? I tried

  • 0

How do I add, say, a line to an already drawn view?

I tried grabbing the canvas from the draw method and using it to draw to, but it doesn’t seem to be working.

protected void onDraw(Canvas canvas) {

   super.onDraw(canvas);
   mCanvas = canvas;
       //drawing code here
}

 public boolean onTouchEvent(MotionEvent event) {

    switch (event.getAction()) {
       case MotionEvent.ACTION_DOWN: {
             Paint paint = new Paint();
             paint.setColor(Color.BLACK);
             paint.setStrokeWidth(10);
             paint.setStyle(Style.FILL);
             mCanvas.drawLine(0, 0, 50, 50, paint);
      }
   }

 }
  • 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-05-28T01:26:57+00:00Added an answer on May 28, 2026 at 1:26 am

    Do the following steps:

    1. if you just want to draw something static, make a member variable as a boolean flag and set it to true when ACTION_DOWN was triggered.
    2. (for dynamic stuff: save the coordinates for later usage)
    3. make a invalidate() call in your case block
    4. in onDraw() draw your line if the flag is true

    edit

    You need to understand the drawing process to understand why saving the canvas object doesn’t work.
    Android works with two buffers that are flipped (naming them buffer A and B). You draw always on the buffer that isn’t visible. After you finished drawing, the buffers will be switched and you will see what you have drawn. Basically the canvas will be locked (to prevent flipping while drawing on the back buffer), and this canvas object will be provided to each view that has requested redrawing. After that the canvas will be unlocked which triggers that the buffers are switched. In that moment this canvas object may still be there, but every drawing you do on it will never be displayed.

    A bit more information can be found on my blog 2D Drawing Series

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

Sidebar

Related Questions

How would I add line numbers to Core Text say in a view down
If I add let's say 1 line at the beggining of a method, if
Let's say I'd like to add an extension method called IsOdd() to the number
When using tablesorter I cannot sort out properly when I add something to say
Suppose I have a 10 line function. If I add inline keyword, let's say
Say you write some code like this (using ruby-mode, but I've seen this happen
I have 3 buttons in my html page, say ADD , UPDATE & DELETE
How do I add let's say something like ajax=1 to all links on my
Say you git remote add origin git@git.assembla.com:myRepo.git And then .. you know .. you
Lets say I wan't to add 1 to an integer. This will only be

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.