How to draw a circle inside a circle using Android xml shapes?

后端 未结 9 2016
日久生厌
日久生厌 2020-12-01 05:15

I\'m trying to make a thumb for a seekbar for my app, and I want to have an inner circle surrounded by a different, larger (semi-transparent) outer circle. I\'m trying to us

相关标签:
9条回答
  • 2020-12-01 05:59

    In case you need to draw 3 or more circles follow this pattern:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
        <!-- Larger blue circle -->
        <item>
            <shape android:shape="oval">
                <padding
                    android:bottom="20dp"
                    android:left="20dp"
                    android:right="20dp"
                    android:top="20dp" />
                <size
                    android:width="100dp"
                    android:height="100dp" />
                <stroke
                    android:width="20dp"
                    android:color="#0000ff" />
            </shape>
        </item>
        <!-- Green circle in middle -->
        <item>
            <shape android:shape="oval">
                <padding
                    android:bottom="20dp"
                    android:left="20dp"
                    android:right="20dp"
                    android:top="20dp" />
                <size
                    android:width="100dp"
                    android:height="100dp" />
                <stroke
                    android:width="20dp"
                    android:color="#00ff00" />
            </shape>
        </item>
        <!-- Smaller red circle at front -->
        <item>
            <shape android:shape="oval">
                <size
                    android:width="100dp"
                    android:height="100dp" />
                <solid android:color="#ff0000" />
            </shape>
        </item>
    </layer-list>
    
    

    The result:

    Note that unlike other answers here, this solutions does not paint circles on top of other circles, which avoids overdraw.

    0 讨论(0)
  • 2020-12-01 06:00

    The only way I've gotten this to work is to define a transparent stroke for the inner (i.e., top) circle that's the difference between the size of the larger and smaller circle.

    For example, this:

    <?xml version="1.0" encoding="utf-8"?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Larger blue circle in back -->
    <item>
        <shape android:shape="oval">
            <solid android:color="#00f"/>
            <size
                    android:width="15dp"
                    android:height="15dp"/>
        </shape>
    </item>
    <!-- Smaller red circle in front -->
    <item>
        <shape android:shape="oval">
            <!-- transparent stroke = larger_circle_size - smaller_circle_size -->
            <stroke android:color="@android:color/transparent"
                    android:width="5dp"/>
            <solid android:color="#f00"/>
            <size
                    android:width="10dp"
                    android:height="10dp"/>
        </shape>
    </item>
    </layer-list>
    

    ...looks like this:

    enter image description here

    0 讨论(0)
  • 2020-12-01 06:00

    It's late but maybe helpful, you can use padding for center circle.

    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:shape="oval">
            <solid
                android:color="#00fff"/>
            <padding
                android:bottom="30dp"
                android:left="30dp"
                android:right="30dp"
                android:top="30dp"/>
            <stroke
                android:width="1dp"
                android:color="@color/holo_red_light"/>
        </shape>
    </item>
    <item>
        <shape
            android:shape="oval">
            <solid
                android:color="#00666666"/>
    
            <size
                android:width="120dp"
                android:height="120dp"/>
            <stroke
                android:width="1dp"
                android:color="@color/holo_red_light"/>
        </shape>
    
    </item>
    </layer-list>
    
    0 讨论(0)
提交回复
热议问题