简体   繁体   中英

How to draw rectangle on Buttons by using canvas

I have 3 LinerLayouts inside a RelativeLayout, each LinerLayouts contain 9 Buttons. Now i want to draw rectangle boxe on top of Buttons whenever i touch the buttons and that rectangle box should slide(move) to each button how the finger is moving. Any idea how to do this.. thanks in advance.

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawRect(mRect, new Paint());
}

private Rect mRect = new Rect();

private static final int WIDTH = 100;
private static final int HEIGHT = 100;

private void setRectPosition(int x, int y) {
    mRect.set(x, y, WIDTH, HEIGHT);
    invalidate();
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    float x = event.getX();
    float y = event.getY();
    setRectPosition((int)x, (int)y);
    return super.onTouchEvent(event);
}

I think this can worked.

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