android中的分层视图
我正在尝试用饼状的块做一个色轮,其中每一块饼都是一个按钮。下面是其中两个片段的代码。目前,第二个切片用白色背景覆盖第一个切片。我的目标可能实现吗android中的分层视图,android,Android,我正在尝试用饼状的块做一个色轮,其中每一块饼都是一个按钮。下面是其中两个片段的代码。目前,第二个切片用白色背景覆盖第一个切片。我的目标可能实现吗 ShapeDrawable slice1 = new ShapeDrawable(new ArcShape(0, 30)); slice1.setIntrinsicHeight(100); slice1.setIntrinsicWidth(100); slice1.getPaint().setColo
ShapeDrawable slice1 = new ShapeDrawable(new ArcShape(0, 30));
slice1.setIntrinsicHeight(100);
slice1.setIntrinsicWidth(100);
slice1.getPaint().setColor(Color.MAGENTA);
View slice1View = (View) findViewById(R.id.slice1);
slice1View.setBackgroundDrawable(slice1);
slice1View.setOnClickListener(magentaButtonListener);
ShapeDrawable slice2 = new ShapeDrawable(new ArcShape(30, 60));
slice2.setIntrinsicHeight(100);
slice2.setIntrinsicWidth(100);
slice2.getPaint().setColor(Color.BLUE);
View slice2View = (View) findViewById(R.id.slice2);
slice1View.setBackgroundDrawable(slice2);
slice1View.setOnClickListener(blueButtonListener);
布局代码为:
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:id="@+id/pie" >
<View
android:id="@+id/slice1"
android:layout_width="40dp"
android:layout_height="40dp"/>
<View
android:id="@+id/slice2"
android:layout_width="40dp"
android:layout_height="40dp"/>
</FrameLayout>
编辑!解决方案:
我将x和y坐标设置为圆直径的一半。然后:我使用getAngle方法来计算角度,它将决定单击的颜色
//from StackOverflow post by Dave Discoid, at http://stackoverflow.com/questions/2676719/calculating-the-angle-between-the-line-defined-by-two-points
public double getAngle(float x, float y )
{
double dx = x - DIAMETER/2;
// Minus to correct for coord re-mapping
double dy = -(y - DIAMETER/2);
double inRads = Math.atan2(dy,dx);
// We need to map to coord system when 0 degree is at 3 O'clock, 270 at 12 O'clock
if (inRads < 0)
inRads = Math.abs(inRads);
else
inRads = 2*Math.PI - inRads;
return Math.toDegrees(inRads);
}
//来自Dave Discoid的StackOverflow post,位于http://stackoverflow.com/questions/2676719/calculating-the-angle-between-the-line-defined-by-two-points
公共双getAngle(浮点x,浮点y)
{
双dx=x-直径/2;
//减去以更正坐标重新映射
双dy=-(y-直径/2);
双inRads=数学atan2(dy,dx);
//当0度在3点,270度在12点时,我们需要映射到坐标系
如果(印度卢比<0)
inRads=数学绝对值(inRads);
其他的
inRads=2*Math.PI-inRads;
返回数学toDegrees(印度卢比);
}
你能发布你的xml布局吗?仅供参考,如果我这样做的话,我的方法是使用一个自定义的绘制/绘制方法和一个自定义的onClick/onTouch,它计算x轴和用户触摸的位置之间的角度。我添加了布局代码。你的方法听起来是可行的。你能告诉我更多关于如何在onClick方法中知道这个角度的信息吗?我想onClick刚刚得到了点击的坐标,你发布的代码正是我所想的。这很酷,因为您可以拥有任意数量的颜色:)