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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:16:09+00:00 2026-06-13T20:16:09+00:00

How can I set a polygon (which would be specified by a collection of

  • 0

How can I set a polygon (which would be specified by a collection of points) to transparent using a PNG image on Android?

  • 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-13T20:16:10+00:00Added an answer on June 13, 2026 at 8:16 pm

    In order to accomplish this, you would have to first get a reference to the resource from a BitmapFactory:

    Bitmap my_image = BitmapFactory.decodeResource(getResources(), R.drawable.[your_image]);
    

    Then, you would have to use the setPixels()method to define the region on the PNG that are to be made transparent. Transparency, I believe, is achieved my setting the pixel Color to 0.

    Deriving the polygon would be some computation that you would have to do in your application, followed by passing the dimensions of the polygon into the setPixels() method. See this link taken from the Android developer documents for an idea on how to use the setPixels() method.

    Note that this assumes that your PNG file is mutable. You will get an IllegalStateException otherwise.


    Deriving The Area for an N-Sided Polygon

    This is where your problem gets significantly more challenging. Since you cannot use conventional area formulas (as the area has nothing to do with this example, but rather where the vertices are located), you must figure out a way to calculate the region that must be transparent yourself.

    One way I can think of doing this is to test all the pixels below a part of the user-drawn line and mark them as transparent until an intersection is met, and repeating this until the bottom of the image is reached. See this drawing for reference:

    enter image description here

    ΔX here, is an arbitrarily-defined pixel length that can be either increased to improve accuracy, or decreased to improve performance. The process of deriving this transparency would be as follows:

    1. Obtain the value farthest to the left that is part of a vertex a user draws;
    2. Progress down the image (by decreasing the value of Y in the getPixel() method until you collide with another user-drawn vertex, changing the pixels to transparent as you go using setPixel() and passing in your current coordinates;
    3. When you collide with another vertex, stop changing the transparency of the pixels, but continue to parse down the column;
    4. Again, if you reach another vertex, begin marking the following pixels as transparent;
    5. Repeat this until you get to the bottom of the image;
    6. Increment the X value by the constant ΔX that you predefined and repeat this on the next column.

    Note that it is important that you toggle between setting the transparency and not setting transparency (you could use a boolean to maintain the state) as it is possible for polygons to “curve around” and come back into the column you are parsing.

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

Sidebar

Related Questions

I have a set of CGPoints which make up a polygon shape, how can
I can set the event on the calendar on my device by using this
I understand that we can set the various style attributes from code behind using
I know I can set a CultureInfo object with an specified culture in the
I Would like to check if a set of N points describe a convex
I can set number of threads for the build process using -j argument. For
i am trying to make an polygon ROI for an matplotlib imshow(image). Using the
Given a set of points in the plane T={a1,a2,...,an} then Graphics[Polygon[T]] will plot the
I'm going to set image perspective. I have image of laptop with blank polygon
I can set the width and height of my canvas by setting the width

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.