Android fully formed button

I tried changing the shape of some Android components like Button or View. What I can do is assign the form to the background using the onDraw method. Well, it looks like it's changed, but sensory events will still work outside of my specific form. Is there any practical way to eliminate touches outside my form? Of course, I could check every position of the mouse event, but for complex shapes, I don’t know how to check if a point is inside the form. Thanks for all your answers.

Simon

+3
source share
1 answer

You can define shapes in xml (as you can see here) . eg

range hood / sample_shape.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <gradient
        android:startColor="#FFFF0000"
        android:endColor="#80FF00FF"
        android:angle="45"/>
    <padding android:left="7dp"
        android:top="7dp"
        android:right="7dp"
        android:bottom="7dp" />
    <corners android:radius="8dp" />
</shape>

xml drawable ImageButton.

/samplebutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
        android:drawable="@drawable/sample_shape" /> <!-- pressed -->
    <item android:state_focused="true"
       android:drawable="@drawable/sample_shape" /> <!-- focused -->
    <item android:drawable="@drawable/sample_shape" /> <!-- default -->
</selector>

:

<ImageButton 
    android:layout_height="wrap_content" 
    android:layout_width="wrap_content" 
    android:background="@drawable/samplebutton">
</ImageButton>
+2

All Articles