在Android中,我有一个Path对象,我碰巧知道它定义了一个封闭的路径,我需要弄清楚路径中是否包含给定的点.我所希望的是有些东西
path.contains(int x,int y)
但这似乎并不存在.
我正在寻找这个的具体原因是因为我在屏幕上定义了一组形状定义为路径,我想知道用户点击了哪一个.如果有更好的方法来接近这一点,比如使用不同的UI元素而不是自己"艰难"地做,那么我愿意接受建议.
如果必须的话,我愿意自己编写一个算法,但这意味着不同的研究.
我创建了一个带按钮和非按下状态选择器的ImageButton,这很好用.
但是按钮的形状不规则,我只希望它可以在底层矩形图像不透明的地方点击.
所以我实现了一个OnTouchListener,它根据Bitmap的像素值检查触摸事件的坐标(如第一个答案中所述:链接).就判断是否按下按钮的逻辑而言,这是有效的,但现在按钮的图像不再变为按下的图像.
这是我有的:
选择器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/button_start_call_pressed" />
<item android:drawable="@drawable/button_start_call_normal" />
</selector>
Run Code Online (Sandbox Code Playgroud)
布局中部分透明的ImageButton:
<ImageButton
android:id="@+id/dashboardStartCallButton"
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/start_call_button_selector"
/>
Run Code Online (Sandbox Code Playgroud)
在活动中:
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
...
ImageButton startCallButton = (ImageButton) this.findViewById(R.id.dashboardStartCallButton);
startCallButton.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
return OnStartCallButtonTouch(v,event);
}
});
}
public boolean OnStartCallButtonTouch(View v, MotionEvent event)
{
Bitmap TheBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.button_start_call_normal);
int eventPadTouch = event.getAction();
int iX = (int) event.getX();
int …Run Code Online (Sandbox Code Playgroud)