简体   繁体   中英

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 ie I want them to change color when some one touch them Can anybody help me with this?

对于#2:计算您的点的中心和触摸事件之间的距离-如果该距离小于圆的半径-您在圆上按了

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 ) ;
               }
            }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM