3

我正在尝试用饼形块制作一个色轮,其中每块饼都是一个按钮。这是两个切片的代码。目前,第二个切片用白色背景覆盖了第一个切片。我的目标可能吗?

    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);
    }
4

0 回答 0