我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
查看Android SDK示例。ApiDemos项目中有几个示例:
/ApiDemos/res/可绘制/
黑色_箱.xml形状_5.xml等
对于带有渐变填充的圆,它看起来像这样:
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" > <gradient android:startColor="#FFFF0000" android:endColor="#80FF00FF" android:angle="270"/> </shape>
这是一个简单的圆圈,在Android中是可绘制的。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#666666"/>
<size
android:width="120dp"
android:height="120dp"/>
</shape>
简单圆代码
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
<solid android:color="#9F2200"/>
<stroke android:width="2dp" android:color="#fff" />
<size android:width="80dp" android:height="80dp"/>
</shape>
下面是一个简单的circle_background.xml,用于预材料:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true">
<shape android:shape="oval">
<solid android:color="@color/color_accent_dark" />
</shape>
</item>
<item>
<shape android:shape="oval">
<solid android:color="@color/color_accent" />
</shape>
</item>
</selector>
您可以在按钮的布局定义中使用属性'android:background=“@drawable/circle_background”
将此设置为视图背景
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="1dp"
android:color="#78d9ff"/>
</shape>
对于实心圆使用:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid
android:color="#48b3ff"/>
</shape>
实心带笔划:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="#199fff"/>
<stroke
android:width="2dp"
android:color="#444444"/>
</shape>
注意:要使椭圆形显示为圆形,在这些示例中,使用此形状作为背景的视图应该是正方形,或者必须将形状标记的高度和宽度财产设置为相等的值。
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<!-- fill color -->
<solid android:color="@color/white" />
<!-- radius -->
<stroke
android:width="1dp"
android:color="@color/white" />
<!-- corners -->
<corners
android:radius="2dp"/>
</shape>
您可以按如下方式使用VectorDrawable:
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="64"
android:viewportWidth="64">
<path
android:fillColor="#ff00ff"
android:pathData="M22,32
A10,10 0 1,1 42,32
A10,10 0 1,1 22,32 Z" />
</vector>
上述xml呈现为:
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<stroke
android:width="10dp"
android:color="@color/white"/>
<gradient
android:startColor="@color/red"
android:centerColor="@color/red"
android:endColor="@color/red"
android:angle="270"/>
<size
android:width="250dp"
android:height="250dp"/>
</shape>
如果你想要这样的圆圈
尝试使用以下代码:
<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="@android:color/white" />
<stroke
android:width="1dp"
android:color="@android:color/darker_gray" />
</shape>
由于某种原因,我无法在ConstraintLayout中画一个圆圈,我只是无法使用上面的任何答案。
当你按下“Alt+7”时,一个简单的文本视图显示了文本,效果很好:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#0075bc"
android:textSize="40dp"
android:text="•"></TextView>
你可以试试这个-
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="ring"
android:innerRadiusRatio="700"
android:thickness="100dp"
android:useLevel="false">
<solid android:color="#CCC" />
</shape>
此外,您可以通过调整android:厚度来调整圆的半径。
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="@color/text_color_green"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>
res/drawable/circle_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#e42828"/>
<stroke android:color="#3b91d7" android:width="5dp"/>
<!-- Set the same value for both width and height to get a circular shape -->
<size android:width="250dp" android:height="250dp"/>
</shape>
</item>
</selector>
你可以尝试使用这个
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape
android:innerRadius="0dp"
android:shape="ring"
android:thicknessRatio="2"
android:useLevel="false" >
<solid android:color="@color/button_blue_two" />
</shape>
</item>
如果您将其用于文本视图,则不必担心宽度和高度纵横比
Android XML可绘制文件中的圆形
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<solid android:color="@android:color/white" />
<stroke
android:width="1.5dp"
android:color="@android:color/holo_red_light" />
<size
android:width="120dp"
android:height="120dp" />
</shape>
截图
您可以创建自定义绘图来动态更改圆的颜色和半径
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.drawable.Drawable;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
public class CircleDrawable extends Drawable {
private Paint circlePaint;
private int fillColor;
private int strokeColor;
private float radius;
public CircleDrawable(int fillColor, int strokeColor, float radius) {
this.fillColor = fillColor;
this.strokeColor = strokeColor;
this.radius = radius;
circlePaint=new Paint(Paint.ANTI_ALIAS_FLAG);
}
@Override
public void draw(@NonNull Canvas canvas) {
int x=getBounds().centerX();
int y=getBounds().centerY();
//draw fill color circle
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setColor(fillColor);
canvas.drawCircle(x,y,radius,circlePaint);
// draw stroke circle
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setColor(strokeColor);
circlePaint.setStrokeWidth(5);
canvas.drawCircle(x,y,radius,circlePaint);
}
@Override
public void setAlpha(int alpha) {
circlePaint.setAlpha(alpha);
}
@Override
public void setColorFilter(@Nullable ColorFilter colorFilter) {
circlePaint.setColorFilter(colorFilter);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
}
从UI设置此项以获得圆形
imageView.setImageDrawable(new CircleDrawable(Color.RED,Color.YELLOW,100));
输出将是这样的
尝试以下带破折号的代码:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="@dimen/_60sdp"
android:height="@dimen/_60sdp" />
<solid android:color="@color/black" />
<stroke
android:width="@dimen/_1sdp"
android:color="@color/white"
android:dashWidth="@dimen/_1sdp"
android:dashGap="@dimen/_1sdp" />
尝试不带破折号的代码:
<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size
android:width="@dimen/_60sdp"
android:height="@dimen/_60sdp" />
<solid android:color="@color/black" />
<stroke
android:width="@dimen/_1sdp"
android:color="@color/white" />
首先创建可绘制资源文件
然后将此代码添加到文件中
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
// here define the shape
android:shape="oval">
<solid
// here define your color
android:color="#666666"/>
<size
// here define your shape size
android:width="120dp"
android:height="120dp"/>
</shape>