如何制作自定义圆形按钮? (只点击圆圈)
有什么方法可以用圆形png文件制作它吗?
我尝试使用 imageView 覆盖 onTouch 方法,但效果很差,因为 view.getWidth()、view.getHeight() 和 view.getTop... 方法效果很差
public boolean inCircle(MotionEvent e, int radius, int x, int y) {
int dx = (int) (e.getX() - x);
int dy = (int) (e.getY() - y);
double d = Math.sqrt((dx * dx) + (dy * dy));
if (d < radius)
return true;
return false;
}
谢谢。
最佳答案
非常简单。创建一个可绘制的自定义形状并将其设置为 View 的背景。示例:
drawable 中的 round_button_drawable.xml/
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="@android:color/holo_orange_dark"/>
</shape>
将此可绘制对象设置为任何 View 的背景。
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/round_button_drawable"
android:text="btn"
/>
关于安卓圆形按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11792431/