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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:28:04+00:00 2026-06-17T23:28:04+00:00

I have this code in onDraw(). radius = drawGmpImage(this.gmpImage, canvas); canvas.drawCircle(kHorizontalOffset, kScreenVerticalOffset, radius ,

  • 0

I have this code in onDraw().

radius = drawGmpImage(this.gmpImage, canvas);
canvas.drawCircle(kHorizontalOffset, kScreenVerticalOffset, radius , maskPaint);

drawGmpImage creates a complex graphic which is a circle with many lines drawn on it. It’s a library function which I cannot change. The lines are polygons and can extend beyond the circumference of the circle.

The need is to “blank out” everything drawn outside the circle.

This is a port from iOS and the original developers solution is to use a simple bitmap mask, stored as a resource, with a transparent circle which matches the size of the circle drawn. Simply drawing the bitmap over the drawn circle has the desired effect but is not an option on Android as I need to support all possible resolutions and ratios.

Therefore, the canvas.drawCircle() call is the beginning of my attempt to mask out everything outside the circle. It works fine in that a filled circle is drawn over my drawn circle so that the only thing left are the polygon lines outside the drawn circles circumference. Radius is the radius of the drawn circle.

How can I invert this so that I am left with the contents of the circle?

  • 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-17T23:28:05+00:00Added an answer on June 17, 2026 at 11:28 pm

    Why is it that you can spend hours working on something, give up, ask the question, then stumble upon the answer 20 minutes later? The joys of life.

    Path path = new Path();
    path.addCircle(kHorizontalOffset, kScreenVerticalOffset, radius, Path.Direction.CW);
    canvas.clipPath(path);
    

    I’d missed the clipPath method which will take any path and use it as a clipping region. Adding my masking circle to the path does exactly what I need.

    [EDIT]

    This works well, but there is a problem. It doesn’t work if hardware acceleration is turned on. I could turn acceleration off, but then I lose a lot of performance in the rest of the draw which is complex.

    Here’s how I finally solved it:

    In onSizeChanged(), create a bitmap mask. I draw a transparent circle in the right place on the bitmap using this paint. The key to is to use a PorterDuffXfermode.

     maskPaint = new Paint();
     maskPaint.setColor(Color.TRANSPARENT);
     maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
     maskPaint.setStyle(Paint.Style.FILL);
    

    then create the bitmap

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
    
        super.onSizeChanged(w, h, oldw, oldh);
    
        createMask(w,h,this.radius);
    
    }
    
    private void createMask(int w,int h, int radius){
    
        if (mask!=null){mask.recycle();}
    
        mask = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas maskCanvas = new Canvas(mask);
        maskCanvas.drawCircle(w, h, radius, maskPaint);
    
    }
    

    Then in onDraw(), I simply draw the mask over the entire view:

    @Override
    protected void onDraw(Canvas canvas){
    
        // draw the image();
        setRadius(drawGmpImage(this.gmpImage, canvas));
        canvas.drawCircle(kHorizontalOffset, kScreenVerticalOffset, radius , maskPaint);
    
        // overlay the mask bitmap
        if (mask != null) {
            canvas.drawBitmap(mask, 0f, 0f, bitmapPaint);
        }
    

    If the radius changes, the mask is recreated:

     private void setRadius(int radius){
    
         this.radius = radius;
         createMask(kHorizontalOffset, kScreenVerticalOffset, radius);
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code: protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub
I have this code <div id=main style=background:#aaaaaa;float:left;height:160px;margin:5px;position:relative;display:block;width:630px;> <div id=1 class=item style=background:#ffaacc;float:left;width:200px;height:150px;margin:5px;position:absolute;left:0px;top:0px;> </div> <div id=2
I have this code : void Main() { System.Timers.Timer t = new System.Timers.Timer (1000);
I have this code for changing the image of a button: - (void)mouseEntered:(NSEvent *)event
I have this code in XAML <Grid x:Name=LayoutRoot Background=Transparent> <Grid.RowDefinitions> <RowDefinition Height=Auto/> <RowDefinition Height=*/>
I have this code: EditText value = ( EditText )findViewById( R.id.editbox ); Integer int_value
I have this code: ie1.link(:text, /Exception:/) It is producing an error message which I
I have this code: def __init__(self, a, b, c, d...): self.a = a self.b
I have this code to sort an array of objects. The data in the
I have this code: con.Open(); cmd = new MySqlCommand(String.Format(SELECT concat(name,'|',lastname) FROM {0} WHERE ID=

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.