userRav
userRav

Reputation: 1

how to get position(x and y coordinates) of circle drwan on canvas in android?

I have two doubts in canvas android.

They are explained below:

Firstly, I had drawn a row of circles on canvas, i want to capture the x and y coordinates of the circle(only on one circle)drawn so that I can draw a bitmap over the circle(in center).

Secondly, I want to imply touch event on circle i.e I want them to change color when some one touch them Can anybody help me with this?

Upvotes: 0

Views: 3227

Answers (2)

ligi
ligi

Reputation: 39549

for #2: calculate the distance between the center of your point and the touch event - if the distance is smaller than the radius of your circle - you pressed on the circle

Upvotes: 1

Yahor10
Yahor10

Reputation: 2132

First you should create GridSector class for your "circle zone".

public class GridSector {

    private int x = 0;
    private int y = 0;
    private Rect rect = new Rect(0, 0, 0, 0);

    public GridSector(int x, int y, Rect rect) {
        this.x = x;
        this.y = y;
        this.rect = rect;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public Rect getRect() {
        return rect;
    }

    public void setRect(Rect rect) {
        this.rect.set(rect.left, rect.top, rect.right, rect.bottom);
    }
}

Then Create view what you wanna touch.

    public class GridSectorsView extends View {


        GridSector currentSelectedSector;
        @Override
            protected void onDraw(Canvas canvas) {
                super.onDraw(canvas);
                drawCircle(canvas); // draw your circles;
            }

          @Override
            public boolean onTouchEvent(MotionEvent ev) {
                switch (ev.getAction()) {
                    case MotionEvent.ACTION_DOWN: {
                    invalidate(currentSelectedSector.getRect()); // update your circles (call  onDraw Function ) ;
               }
            }
}

Upvotes: 0

Related Questions