我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
我在查找Android版XML中形状定义的文档时遇到了一些问题。我想在XML文件中定义一个用纯色填充的简单圆圈,以将其包含在布局文件中。
遗憾的是,android.com上的文档没有涵盖Shape类的XML属性。我想我应该用ArcShape来画一个圆,但没有关于如何设置大小、颜色或角度的说明。
当前回答
你可以试试这个-
<?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"?>
<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>
首先创建可绘制资源文件
然后将此代码添加到文件中
<?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>
<?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>
只需使用
ShapeDrawable circle = new ShapeDrawable( new OvalShape() );
您可以创建自定义绘图来动态更改圆的颜色和半径
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));
输出将是这样的