[英]How to draw a circle inside a circle using Android xml shapes?
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 use layer-list
, but I'm having issues. 我正在尝试使用layer-list
,但我遇到了问题。 Below is my code... 以下是我的代码......
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:shape="oval" >
<solid android:color="#00f" />
<size
android:height="15dp"
android:width="15dp" />
</shape>
</item>
<item>
<shape
android:shape="oval" >
<solid android:color="#f00" />
<size
android:height="10dp"
android:width="10dp" />
</shape>
</item>
</layer-list>
I would expect to see a small red circle on top of a larger blue circle, but all I'm seeing is the small red circle. 我希望在一个更大的蓝色圆圈上面看到一个小红圈,但我所看到的只是一个小红圈。 Does anyone have any ideas? 有没有人有任何想法?
The only way I've gotten this to work is to define a transparent stroke for the inner (ie, 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: ......看起来像这样:
hope it will help. 希望它会有所帮助。 This is drawable *.xml 这是可绘制的* .xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dip"
android:right="1dp"
android:top="1dp" />
<solid android:color="#000" />
</shape>
</item>
<item>
<shape android:shape="oval">
<padding
android:bottom="1dp"
android:left="1dip"
android:right="1dp"
android:top="1dp" />
<solid android:color="#EEE" />
</shape>
</item>
</layer-list>
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>
I managed to solve this by setting the width and height of the <item>
in the <layer-list>
. 我设法通过在<layer-list>
设置<item>
的宽度和高度来解决这个问题。 Probably not as best practise, but seem ok as was for background of icon in list view that had fixed dimensions. 可能不是最佳实践,但在列表视图中具有固定尺寸的图标背景似乎没问题。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- larger circle at the back -->
<item android:height="54dp" android:width="54dp" android:gravity="center">
<shape
android:shape="oval">
<solid
android:color="#0000FF"/>
<!-- thicker outer boarder -->
<stroke
android:width="2dp"
android:color="#000000"/>
</shape>
</item>
<!-- inner circle -->
<item android:height="40dp" android:width="40dp" android:gravity="center">
<shape
android:shape="oval" >
<solid
android:color="#00FF00"/>
<stroke
android:width="1dp"
android:color="#000000"/>
</shape>
</item>
</layer-list>
I ended up here in search of drawing concentric circle found only answers with layer list approach so adding my answer with only using the shape
, I hope it will help someone. 我最终在这里寻找绘制同心圆找到了只有层列表方法的答案,所以只使用shape
添加我的答案,我希望它会帮助某人。
<shape android:shape="oval">
<solid android:color="#FFF" />
<size
android:width="15dp"
android:height="15dp" />
<stroke
android:width="6dp"
android:color="#000" />
</shape>
Hope below code snippet helps :) 希望下面的代码片段有帮助:)
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- larger circle at the back -->
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="#FFFFFF" />
<stroke
android:width="1dp"
android:color="#000000" /></shape>
</item>
<!-- inner circle -->
<item >
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2.1"
android:useLevel="false" >
<solid android:color="#000000" />
<stroke
android:width="1dp"
android:color="#FFFFFF" /></shape>
</item>
</layer-list>
this is a short version of Sean Barbeau's answer 这是Sean Barbeau答案的简短版本
<stroke
android:width="1dp"
android:color="@color/white" />
<solid android:color="@color/dark_blue" />
<size
android:width="14dp"
android:height="14dp" />
Just you have to use Shap
Attribute 只是你必须使用Shap
属性
xml code drawable xml代码可绘制
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<stroke android:width="2dp" android:color="#B91969"/>
<size android:width="@dimen/dim_16dp" android:height="@dimen/dim_16dp" />
<solid android:color="#0f0" />
</shape>
Output 产量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.