RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

Circle in canvas taking entire parent space

My parent view is a RelativeLayout, I added a triangle to it extending the View class. I draw the triangle using canvas and paint. The problem i am facing is that, when i touch on the relative layout, both touch listeners , the relative layout and triangle are being triggered. I just want the triangle to take the exact space as it requires. How can i limit my customview , triangle in this case to take it's space, rather than occupying the entire parent layout.

Actually my requirement is: i have one relative layout on the layout i am adding some custom views dynamically. those have touch listeners for for dragging points in the triangle,but those are taking full screen of my parent view, because of that i am unable to trigger touch listeners separately for parent and child view's.

I've spent countless hours developing solutions for this problem and I just couldn't get my head around it. Any help would be greatly appreciated.

Upvotes: 2

Views: 462

Answers (2)

btalb
btalb

Reputation: 7037

From what I am gathering you are saying,you have a triangle drawn within a RelativeLayout in a CustomView. The actual View size is FILL_PARENT for both width and height. So the square representing the touchable area of the triangle is the same as the square representing the touchable area of the parent RelativeLayout.

You would like touch events inside the triangle's drawn area to be received by the triangle View and anything outside to be received by the parent. It is not possible to define a custom touch area from what I understand.

That doesn't stop you manipulating drawing bounds and touch focus to accomplish what you want though.

How it can be done

  1. Ensure that your CustomView with the triange always receives the touch event. When it receives the touch event, it perform basic maths to figure out if the touch was within your triangle area (you must have your triangle bounds you use to draw your triangle saved as global variables for the CustomView).

  2. Then a simple conditional statement to determine whether to act on and consume or pass the event on (do you even want a TouchEvent on your RelativeLayout to do anything? If not that makes this a lot easier).

Example code skeleton:

@Override
public boolean onTouchEvent(MotionEvent event) {
    boolean inTriangle = false;
    float x = event.getX();
    float y = event.getY();

    // This really depends on what behaviour you want, if a drag extends outside the
    // triangle bounds, do you want this to end the touch event? This code could be done
    // more efficiently depending on your choices / needs

    // Do maths to figure out if the point is actually inside the triangle. Once again,
    // many solutions and some much easier than others depending on type of triangle

    // This flow will only register events in the triangle, and do nothing when outside
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if (inTriangle) {
            // Do whatever it is you want to do

            return true; // to say you consumed the event if necessary
        } else {
            return false; // parent RelativeLayout should now get touch event

    }
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
    if (inTriangle) {
        // Do whatever it is you want

        return true;
    } else {
        // Do nothing
        return true; // So is stil consumed
    }
}

return true;

}

To be honest, the question is way to open-ended / unclear to give anything more specific. But this is how you would accomplish what you asked. Just apply it to your specific scenario.

Note: I have encountered issues with TouchEvent passing, so if you really do want to pass them to specific places, you may have to investigate toying with View.dispatchTouchEvent() if the order your Touches are being processed becomes an issue.

Upvotes: 3

Dharmendra
Dharmendra

Reputation: 33996

You have an issue to handle the touch events of the triangle because you are getting touch on both the view RalativeLayout and Your customview.

To solve this issue you have to do some changes in the customview's onTouchEvent. Like If you are touching on the customview and if you want to handle the event then from onTouchEvent you will have to return true instead of false so that your parent view will not get the touch events.

Upvotes: 1

Related Questions