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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:59:33+00:00 2026-05-28T04:59:33+00:00

i am able to get my bitmap set of points (as an array) using

  • 0

i am able to get my bitmap set of points (as an array) using this link
now my question is how can i bound these points as shape/region. Means when user touched on area of my bounded points, i want to move objects(shape) according to that. Above link return points of colored bitmap (it remove transparent part), only colored part points are return as an array.
This is what my code :

1) CustomSahpe.java

public class CustomShape {
private final Context context;

Bitmap bitmap;
int width, height;
int[] pixels;
private final ArrayList<Point> points = new ArrayList<Point>();
public CustomShape(Context context) {
    // TODO Auto-generated constructor stub
    // super(context);
    this.context = context;
    bitmap = BitmapFactory.decodeResource(context.getResources(),
            R.drawable.ic_menu_balloon);
    width = bitmap.getWidth();
    height = bitmap.getHeight();
    pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);        
    getActualBitmap();
}

public ArrayList<Point> getPoints(){
    return points;
}

public void getActualBitmap() {
    for (int x = 0; x < width; x+=2) {
        int firstY = -1, lastY = -1;
        for (int y = 0; y < height; y+=2) {
            boolean transparent = (pixels[y * width + x] == Color.TRANSPARENT);
            if (!transparent) {
                if (firstY == -1) {
                    firstY = y;
                }
                lastY = y;
            }
        }
        if (firstY != -1) {
            points.add(new Point(x, firstY));
            points.add(new Point(x, lastY));
        }
    }
}

}

2) MyShapre.java

class MyShape{
    CustomShape customShape ;
    Point points[];
    private int x, y;
    Path path = new Path();
    public MyShape(Context context) {           
        customShape = new CustomShape(ScaleTestActivity.this);
        points = new Point[customShape.getPoints().size()];
        for(int i=0;i<customShape.getPoints().size();i++){
            points[i] = new Point();
            points[i] = customShape.getPoints().get(i); 
        }
    }
    public Path getPath(){
        return path;
    }

    public void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        for(int i =0 ;i<points.length;i++){             
            Point point = new Point(points[i].x + getX(), points[i].y + getY());
            path.lineTo(points[i].x, points[i].y);
            canvas.drawPoint(point.x,point.y,paint);
        }
    }
    public void setX(int x) {
        this.x = x;
    }
    public int getX() {
        return x;
    }
    public void setY(int y) {
        this.y = y;
    }
    public int getY() {
        return y;
    }       
}

}

3) MainPanel.java

    class MainPanel extends View{
        Context context;
        MyShape myShape;
        boolean flag = false;
        public MainPanel(Context context) {         
            super(context);
            this.context = context;
            myShape = new MyShape(context);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            canvas.drawColor(Color.RED);
            myShape.onDraw(canvas);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            // TODO Auto-generated method stub          
            int x,y;
            x = (int)event.getX();
            y = (int)event.getY();
            Point point = new Point(x, y);

            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                myShape.setX(x);
                myShape.setY(y);
                RectF rectF = new RectF();
                Path path = myShape.getPath();
                path.computeBounds(rectF, true);
                Region region = new Region();

                region.setPath(path, new Region((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom));

                if(region.contains(x,y)){
                    flag = true;
                    Log.i("System out","onDown");
                }
                break;
            case MotionEvent.ACTION_MOVE:               
                Log.i("System out","onMove : "+flag);
                if(flag){
                    myShape.setX(x);
                    myShape.setY(y);
                    Log.i("System out","onMove");
                }
                break;
            case MotionEvent.ACTION_UP:
//              myShape.setX(x);
//              myShape.setY(y);
                flag = false;
                Log.i("System out","onUp");
                break;
            default:
                break;
            }
            invalidate();
            return true;
        }
    }

4) ScaleTestActivity.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);      
    setContentView(new MainPanel(this));
}
  • 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-28T04:59:34+00:00Added an answer on May 28, 2026 at 4:59 am

    I use a Polygon class to detect touches on rotated bitmaps. It’s based mostly on information and code from this site http://alienryderflex.com/polygon/. This should work with your code.

    public class Polygon {
    
    // Polygon coodinates.
    private final int[] polyY, polyX;
    
    // Number of sides in the polygon.
    private final int polySides;
    
    /**
     * Default constructor.
     * @param px Polygon y coods.
     * @param py Polygon x coods.
     * @param ps Polygon sides count.
     */
    public Polygon( final int[] px, final int[] py, final int ps ) {
        polyX = px;
        polyY = py;
        polySides = ps;
    }
    
    /**
     * Checks if the Polygon contains a point.
     * @see "http://alienryderflex.com/polygon/"
     * @param x Point horizontal pos.
     * @param y Point vertical pos.
     * @return Point is in Poly flag.
     */
    public boolean contains( final float x, final float y ) {
    
        boolean oddTransitions = false;
        for( int i = 0, j = polySides -1; i < polySides; j = i++ ) {
            if( ( polyY[ i ] < y && polyY[ j ] >= y ) || ( polyY[ j ] < y && polyY[ i ] >= y ) ) {
                if( polyX[ i ] + ( y - polyY[ i ] ) / ( polyY[ j ] - polyY[ i ] ) * ( polyX[ j ] - polyX[ i ] ) < x ) {
                    oddTransitions = !oddTransitions;          
                }
            }
        }
        return oddTransitions;
    }
    
    
    }
    

    You could add this constructor to help you convert a Point array to a Polygon object.

    public Polygon(Point[] points){
        polySides = points.length;
        polyY = new int[polySides];
        polyX = new int[polySides];
    
        for(int i = 0; i < polySides; i++){
            polyY[i] = points[i].y;
            polyX[i] = points[i].x;
        }
    }
    

    You might be able to use it in your MyShape class with this method.

     public boolean isTouched(final float X, final float Y){
       final Polygon p = new Polygon(points);
          return p.contains(X, Y);
    }
    

    Now if you have an odd shape you should be able to detect exactly if the use touches it. I have used this method many times.

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

Sidebar

Related Questions

I was able to get this working is SSRS 2008, but do to the
Right now I'm able flip views using sliding. I want to flip views automatically
I am loading an image using a Loader. Once loaded, I can get the
I'm currently able to get this to do most of what I want to.
I need to be able get a single specific attribute from an element with
Has anyone been able to get an NHibernate-based project up and running on a
Has anyone been able to get xinc to run correctly under OpenBSD's chrooted default
Has anyone been able to get a variable record length text file (CSV) into
I'm able to get cells to format as Dates, but I've been unable to
I am not able to get my jwysiwyg and Jhtmlarea text editors to work

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.