我想以圆形形式进行相对布局。我不想使用画布,因为我无法处理画布中的可点击事件。我希望像鼠标点击等每个事件只能在该圆圈内执行。在圆圈外部单击时,不应执行任何布局事件。
我有以下代码,circle.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/white"/>
<size
android:width="10dp"
android:height="10dp"/>
</shape>
,img.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/circle"/>
</layer-list>
,布局
<RelativeLayout
android:id="@+id/test_layout"
android:layout_width="700px"
android:layout_height="700px"
android:background="@drawable/img" />
但是这段代码是在相对布局的背景上设置圆圈,这是以矩形的形式。无论我添加到相对布局的事件都在圆圈外执行。我希望整个布局是一个圆圈。我怎么能那样做?。
答案 0 :(得分:2)
创建xml
文件并添加到drawable
文件夹
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >
<solid android:color="#65BF59"/> <!-- this one is ths color of the Rounded Button -->
<corners
android:bottomRightRadius="100dp"
android:bottomLeftRadius="100dp"
android:topLeftRadius="100dp"
android:topRightRadius="100dp"/>
</shape>
在布局的背景中使用此文件。
答案 1 :(得分:1)
每个布局都是一个矩形。你不能改变它。这就是Android UI框架的工作原理。
但你可以画一个圆圈(你已经做过的方式)并自己处理触摸事件。
为此,您必须创建自定义相对布局(或您需要的任何其他类型的布局):
public class CircleRelativeLayout extends RelativeLayout {
和@Override
触摸方法:
public boolean onInterceptTouchEvent (MotionEvent event) {
if(isInsideCircle(event)){
return super.onInterceptTouchEvent(event);
} else {
return false;
}
}
public boolean onTouchEvent (MotionEvent event) {
if(isInsideCircle(event)){
return super.onTouchEvent(event);
} else {
return false;
}
}
然后只需使用几何体创建方法private boolean isInsiderCircle(MotionEvent event)
。