如何在Android中绘制半圆

10

我在我的应用程序中使用此代码绘制半个:

  <?xml version="1.0" encoding="utf-8" ?>
<layer-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <item
        android:left="35dp"
        android:top="40dp"
        android:bottom="40dp"
        android:right="0dp">
        <shape
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:shape="oval" android:innerRadius="30dp" android:thickness="0dp">
            <solid android:color="@color/transparent"/>
            <stroke android:width="3dp" android:color="@color/White"/>

        </shape>
    </item>
</layer-list>
输出:

输出:

enter image description here

但我需要像下面这样的东西:

enter image description here

如何绘制它?

5个回答

22
我建议通过代码来绘制它。
1- 创建类MyView,并放置下面的代码。
public class MyView extends View {

    public MyView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {

        // TODO Auto-generated method stub
        super.onDraw(canvas);
        float width = (float) getWidth();
        float height = (float) getHeight();
        float radius;

        if (width > height) {
         radius = height / 4;
        } else {
         radius = width / 4;
        }

        Path path = new Path();
        path.addCircle(width / 2,
         height / 2, radius,
         Path.Direction.CW);

        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);

        float center_x, center_y;
        final RectF oval = new RectF();
        paint.setStyle(Paint.Style.STROKE);

        center_x = width / 2;
        center_y = height / 2;

        oval.set(center_x - radius,
            center_y - radius,
            center_x + radius,
            center_y + radius);
        canvas.drawArc(oval, 90, 180, false, paint);
    }
}

2-在您的activity或fragment中初始化该类:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(new MyView(this));
}

@testStack 您好朋友。 - Adarsh Yadav
有什么想法可以将分号附加到弧形上吗?例如,只从一侧拥有帽子? - vanste25
3
你添加了 Path path = new Path(); 的代码行,但却从未使用过它,这是不必要的。 - PeterPazmandi

13
您可以使用一个矩形形状的.xml文件,并仅在其中一侧编辑角落。
示例:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size android:height="30dp"
        android:width="30dp"/>
    <solid android:color="@color/black"/>
    <corners android:topLeftRadius="15dp"
        android:bottomLeftRadius="15dp"/>
</shape>

Path.addRoundRect不支持不同的角大小。 - Dogcat

12

9
好的,但这不是完整的答案。答案应该能够独立解决问题,并只使用链接作为支持。这意味着当Android移动URL时,此答案仍然有效。但事实并非如此。因为我必须去别处了解clip标签是什么。 - StarWind0
1
@StarWind 我的链接没有提供任何可直接使用的教程,你需要阅读和理解文档。将其复制到答案中是没有意义的,因为它可能会过时。 - Dmitry Zaytsev
1
@DmitryZaitsev 只是指出了 Stack Overflow 的准则。如果您不同意这些规则,请与 Stack Overflow 联系。http://stackoverflow.com/help/how-to-answer - StarWind0

2
这是我在一个可绘制的xml文件中创建半圆的方法。"Original Answer"翻译成"最初的回答"。
<size
    android:width="180dp"
    android:height="90dp"></size>

<corners
    android:topLeftRadius="200dp"
    android:topRightRadius="200dp"></corners>


创建一个矩形形状 <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> - Greece
1
这将创建一个半圆。OP想要的是一条曲线。 - Taslim Oseni

1
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <size
        android:width="180dp"
        android:height="90dp"></size>

    <corners
        android:topLeftRadius="200dp"
        android:topRightRadius="200dp"></corners>
    <stroke android:width="5px" android:color="@color/black" />
</shape>

添加一个描述,说明为什么以及如何解决答案中描述的问题 - Arghya Sadhu

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接