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

  • Home
  • SEARCH
  • 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 9306731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T00:14:14+00:00 2026-06-19T00:14:14+00:00

I would like to know when will the method protected void onDraw(final Canvas canvas)

  • 0

I would like to know when will the method

protected void onDraw(final Canvas canvas) {}

will be called. I am asking about the control flow.constructor of this class is called from other class.When control comes to the constructor will it simply call all methods in this class??

Also i want to do some drawing when the draw image is touched and moved. for that i used onTouchEvent(MotionEvent event).But i dont know how to invoke onDraw after i do some coding in onTouch.That is i do change some coordinate values how will call onDraw to redraw image?

Can anyone help?

public class DrawView extends View {
Paint paint = new Paint();
public DrawView(Context context) {
    // TODO Auto-generated constructor stub
    super(context);
}

@Override
protected void onDraw(final Canvas canvas) {
    // TODO Auto-generated method stub

        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(3); 
        canvas.drawRect(30, 350, 50, 400, paint);
        super.onDraw(canvas);
// some other drawings
}


@Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN://some code
        break;

case MotionEvent.ACTION_MOVE://some code

            break;
case MotionEvent.ACTION_UP://some code  
    break;
        default:            break;
        }
        return super.onTouchEvent(event);
    }
}
  • 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-19T00:14:15+00:00Added an answer on June 19, 2026 at 12:14 am

    /**
    * @author rajeshcp
    */

    public class SimpleDrag extends View {
    
    
    
    
    private Paint mPaint;
    private Rect mRect;
    
    /**
     * @param context  
     * @return of type SimpleDrag
     * Constructor function
     * @since Feb 19, 2013 
     * @author rajeshcp
     */
    public SimpleDrag(Context context) {
        super(context);
        init();
    }
    
    /**
     * @param context
     * @param attrs  
     * @return of type SimpleDrag
     * Constructor function
     * @since Feb 19, 2013 
     * @author rajeshcp
     */
    public SimpleDrag(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }
    
    /**
     * @param context
     * @param attrs
     * @param defStyle  
     * @return of type SimpleDrag
     * Constructor function
     * @since Feb 19, 2013 
     * @author rajeshcp
     */
    public SimpleDrag(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }
    
    /* (non-Javadoc)
     * @see android.view.View#onDraw(android.graphics.Canvas)
     * @since Feb 19, 2013
     * @author rajeshcp 
     */
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.BLUE, PorterDuff.Mode.CLEAR);
        if( mRect != null )
        {
            mPaint.setColor(Color.RED);
            canvas.drawRect(mRect, mPaint);
        }
    }
    
    
    private void init()
    {
        mRect  = new Rect(0, 0, 50, 50);
        mPaint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
    }
    
    private Point mTouchPoint;
    
    /* (non-Javadoc)
     * @see android.view.View#onTouchEvent(android.view.MotionEvent)
     * @since Feb 19, 2013
     * @author rajeshcp 
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    
        final int action = event.getAction();
    
        if( action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_DOWN)
        {
            mTouchPoint = new Point((int)event.getX(), (int)event.getY());
            if( !mRect.contains(mTouchPoint.x, mTouchPoint.y) )
            {
                return false;
            }
        }
    
        if( action == MotionEvent.ACTION_MOVE )
        {
            final Point curretPoint = new Point((int)event.getX(), (int)event.getY());
            int xMoved = curretPoint.x - mTouchPoint.x;
            int yMoved = curretPoint.y - mTouchPoint.y;
            mRect.set(mRect.left + xMoved, mRect.top + yMoved, mRect.right + xMoved, mRect.bottom + yMoved);
            mTouchPoint = curretPoint;
            invalidate();
        }
        return true;
    }
    
    
    
    }
    

    Call Invalidate when ever you want onDraw method to get called.

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

Sidebar

Related Questions

I would like to know the procedure which will give the out put about
I would like to know when will the ApplicationContext call the method annotated with
I would like to know if it's possible to create a method that will
I would like to know what will happen in the following hypothetical situation. Let's
I would like to know how will performance go when I instantiate multiple classes
I would like to know if a QTimer will keep on counting once it
I would like to know high level idea of how Android Modem code will
I would like to know the process and service providers who will enable me
I would like to know, wether Server.Transfer will work in a Web Farm. Thanking
I would like to create a model method that will take the user (which

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.