Java 如何使用 Android xml 形状在圆内画圆?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21613308/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to draw a circle inside a circle using Android xml shapes?
提问by jas7457
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. Below is my code...
我正在尝试为我的应用程序制作一个搜索栏的拇指,并且我想要一个由不同的更大(半透明)外圆包围的内圆。我正在尝试使用layer-list
,但遇到了问题。下面是我的代码...
<?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?
我希望在更大的蓝色圆圈之上看到一个红色的小圆圈,但我看到的只是红色的小圆圈。有没有人有任何想法?
回答by Евгений Дюндин
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>
回答by Sean Barbeau
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:
...看起来像这样:
回答by Amir
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>
回答by scottyab
I managed to solve this by setting the width and height of the <item>
in the <layer-list>
. Probably not as best practise, but seem ok as was for background of icon in list view that had fixed dimensions.
我设法通过设定的宽度和高度,以解决这个问题<item>
的<layer-list>
。可能不是最佳实践,但似乎与具有固定尺寸的列表视图中的图标背景一样好。
<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>
回答by Niranjan
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>
回答by Ankit Pandey
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" />
回答by Mightian
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>
回答by ND1010_
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
输出
回答by Albert Vila Calvo
In case you need to draw 3 or more circlesfollow this pattern:
如果您需要绘制3 个或更多圆圈,请遵循以下模式:
<?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.
请注意,与此处的其他答案不同,此解决方案不会在其他圆的顶部绘制圆,从而避免 overdraw。