问题描述
我试着改变一些Android的组件,如按钮或视图的形状。我可以做的是一个形状分配与OnDraw中,法的背景。好了,它看起来像重塑,但触摸事件仍然会我定义形状以外的地方工作。有没有什么可行的办法来排除我的外形之外触摸?当然,我可以检查鼠标事件的每一个位置,但对于络合剂的形状,我不知道如何检查,如果一个点里面的造型。感谢您的答复。
I tried to change the shape of some Android components like Button or View. What I can do is assign a shape to the background with the onDraw-Method. Well, it looks like reshaped, but the touch-events will still work outside my defined shape. Is there any practicable way to exclude touches outside my shape? Certainly, I could check every position of the mouse-event, but for complexer shapes, I don't know how to check if a point is inside the shape.Thanks for all your replies.
西蒙
推荐答案
您可以在XML的。例如
You can define the shapes in xml (as you can see here).e.g. :
绘制/ sample_shape.xml:
drawable/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描述为绘制一个的ImageButton 。
绘制/ samplebutton.xml:
drawable/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>
在你的layoutfile:
in your layoutfile:
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/samplebutton">
</ImageButton>
这篇关于Android的全面形按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!