Skip to content Skip to sidebar Skip to footer

Android: Can I Create A View/canvas That Is Not Rectangular, But E.g. Round?

I have a view that is round and hovering above (-> in z-axis direction coming out of the screen) the main content. When the someone taps the screen I want either the main conten

Solution 1:

As far as I know - it is impossible. I have checked the sources of View.java at git.kernel.org and it is based on Rect class - rects define position, size, regions to invalidate etc. ("The geometry of a view is that of a rectangle." - from the comments in View.java)

As for Canvas class - it is usually constructed over Bitmap or GL. Bitmap is definitely a rectange (a matrix), so canvas seems to represent a rectange too. If using GL a viewport is specified (which is a rectangle, too).

It seems to be the most obvious way to check if the coordinates passed to your onTouch() method fit you region and return false if they don't. Then the event will be passed to the View below and it should process the event.

Solution 2:

Probably not relevant to the original asker anymore, but for anyone who's still looking, it looks like this will be added in L: https://developer.android.com/preview/material/views-shadows.html#clip. The View.setClipToOutline method allows you to clip a view to the shape of a rectanglular, circular, or round rectangular drawable.

Solution 3:

You need to specify an Android Selector to avoid the default hovering image.

Post a Comment for "Android: Can I Create A View/canvas That Is Not Rectangular, But E.g. Round?"