Layering views in android

I am trying to make a colored pie wheel in which each pie is a button. Here is the code for two of the slices. The second slice currently covers the first slice against a white background. Is my goal possible?

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

      

link code:

    <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>

      

Edit! Solution: I set the x and y coordinates to half the diameter of the circle. Then: I used the getAngle method to calculate the angle, which will determine which color was clicked.

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

      

+3
android


source to share


No one has answered this question yet

See similar questions:

37
Calculating the angle between a line defined by two points

or similar:

3606
Close / hide Android soft keyboard
3295
Why is the Android emulator so slow? How can we speed up the development of an Android emulator?
3288
Correct use cases for Android UserManager.isUserAGoat ()?
2609
Is there a unique identifier for an Android device?
2510
How to persist android activity state by persisting instance state?
2097
Is there a way to run Python on Android?
1858
"Debug certificate expired" error in Android Eclipse plugins
1844
What is "Context" on Android?
1296
Reloading activity on android rotation
815
Android "Only the original thread that created the view hierarchy can touch its views."



All Articles
Loading...
X
Show
Funny
Dev
Pics