如何创建这样的自定义按钮?
How to create such custom button?
如何创建具有自定义触摸区域的按钮?
我想创建如下所示的弯曲按钮:
但是只有曲线下的区域应该是可点击的,所以简单的 ImageButtons 将不起作用,因为点击框是矩形的。
1张图片。它如何与 ImageButtons
一起工作
2张必须的版本之一
视图本质上是矩形。
因此,只需将背景图像放在容器布局中,然后将您的(透明)ImageView 或 TextView(内部没有任何图像或文本)放在上面。
并对他们的点击做出反应 listener/s。
这是基本思路:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rise_call_bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<TextView
android:id="@+id/txtLeft"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtMidLeft"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtMidRite"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtRite"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
</LinearLayout>
</RelativeLayout>
结果如下:
每个 TextView 都是可点击的。
因此,只需添加点击 listener/s(集中点击或单独点击 - 您选择),就完成了。
用户会认为按钮就是 s/he 所看到的:在 TextView 下方绘制的文本。
注意:我选择了所有 TextView,只是为了让它们在设计时可见。
它们在运行时将完全透明。
您可能想要将高度提高一点,比如提高到 120dp。
尝试使用此解决方案。
核心思想是检测位图中是否有触摸。
好的,我已经通过一个 ImageView 和 onTouchListener 解决了这个问题。我已经计算了每个按钮区域的公式并交叉检查了触摸视图的坐标。
Check 和 Fold 按钮的代码如下:
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // нажатие
if (y > 0.98 * x +75) {
Log.d("TEST", x+" __FOLD__ "+y);
img.setBackgroundResource(R.drawable.fold_all);
} else
if(y > -0.98 * (x-1200)){
Log.d("TEST", x+" __CHECK__ "+y);
img.setBackgroundResource(R.drawable.check_all);
} else {
Log.d("TEST", x+" __NOTHING__ "+y);
}
break;
case MotionEvent.ACTION_MOVE: // движение
break;
case MotionEvent.ACTION_UP: // отпускание
case MotionEvent.ACTION_CANCEL:
img.setBackgroundResource(R.drawable.all);
break;
}
return true;
}
对不起我的英语。
如何创建具有自定义触摸区域的按钮?
我想创建如下所示的弯曲按钮:
但是只有曲线下的区域应该是可点击的,所以简单的 ImageButtons 将不起作用,因为点击框是矩形的。
1张图片。它如何与 ImageButtons
一起工作
2张必须的版本之一
视图本质上是矩形。
因此,只需将背景图像放在容器布局中,然后将您的(透明)ImageView 或 TextView(内部没有任何图像或文本)放在上面。
并对他们的点击做出反应 listener/s。
这是基本思路:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/rise_call_bg"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
>
<TextView
android:id="@+id/txtLeft"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtMidLeft"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtMidRite"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
<TextView
android:id="@+id/txtRite"
android:layout_height="100dp"
android:layout_width="0dp"
android:layout_weight="1"
android:clickable="true"
/>
</LinearLayout>
</RelativeLayout>
结果如下:
每个 TextView 都是可点击的。
因此,只需添加点击 listener/s(集中点击或单独点击 - 您选择),就完成了。
用户会认为按钮就是 s/he 所看到的:在 TextView 下方绘制的文本。
注意:我选择了所有 TextView,只是为了让它们在设计时可见。
它们在运行时将完全透明。
您可能想要将高度提高一点,比如提高到 120dp。
尝试使用此解决方案。 核心思想是检测位图中是否有触摸。
好的,我已经通过一个 ImageView 和 onTouchListener 解决了这个问题。我已经计算了每个按钮区域的公式并交叉检查了触摸视图的坐标。 Check 和 Fold 按钮的代码如下:
public boolean onTouch(View v, MotionEvent event) {
x = event.getX();
y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: // нажатие
if (y > 0.98 * x +75) {
Log.d("TEST", x+" __FOLD__ "+y);
img.setBackgroundResource(R.drawable.fold_all);
} else
if(y > -0.98 * (x-1200)){
Log.d("TEST", x+" __CHECK__ "+y);
img.setBackgroundResource(R.drawable.check_all);
} else {
Log.d("TEST", x+" __NOTHING__ "+y);
}
break;
case MotionEvent.ACTION_MOVE: // движение
break;
case MotionEvent.ACTION_UP: // отпускание
case MotionEvent.ACTION_CANCEL:
img.setBackgroundResource(R.drawable.all);
break;
}
return true;
}
对不起我的英语。