【问题标题】:how to draw a half circle in android如何在android中画一个半圆
【发布时间】:2015-07-29 16:14:37
【问题描述】:

我正在使用此代码在我的应用中绘制一半:

  <?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>

输出:

但我需要类似下面的东西:

这个怎么画?

【问题讨论】:

    标签: android


    【解决方案1】:

    我建议通过代码来绘制它。

    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-在您的活动或片段中初始化此类:-

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

    【讨论】:

    • @testStack 欢迎朋友。
    • 任何想法如何附加半圆弧例如?只从一侧盖帽?
    • 你添加了 Path path = new Path();.... 但你从不使用它....不必要的行
    【解决方案2】:

    您可以使用矩形 .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 不支持不同的角尺寸"
    【解决方案3】:

    您可以使用&lt;clip /&gt; drawable 来切断您的圈子的一部分。

    http://developer.android.com/guide/topics/resources/drawable-resource.html#Clip

    【讨论】:

    • 很好,但不是一个完整的答案。答案应该能够独立存在,并且只使用链接作为支持。这个想法是当 android 移动 URL 时,这个答案仍然有效。事实并非如此。因为我必须去其他地方了解剪辑标签是什么。
    • @StarWind 我的链接没有提供任何您必须遵循的即用型教程,它只是指向您需要阅读和理解的文档。将其复制到答案本身是没有意义的,因为它可能会过时。
    • @DmitryZaitsev 只是指出堆栈溢出指南。如果您不同意规则,请接受 SO。 stackoverflow.com/help/how-to-answer
    【解决方案4】:

    这就是我在可绘制的 xml 文件中创建半圆的方式。

    <size
        android:width="180dp"
        android:height="90dp"></size>
    
    <corners
        android:topLeftRadius="200dp"
        android:topRightRadius="200dp"></corners>
    

    【讨论】:

    • 创建一个矩形 schemas.android.com/apk/res/android" android:shape="rectangle">
    • 这会创建一个半圆。 OP 想要的是一条曲线。
    【解决方案5】:
    <?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>
    

    【讨论】:

    • 添加说明为什么以及如何解决答案中描述的问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 2016-06-09
    • 2018-10-03
    • 1970-01-01
    相关资源
    最近更新 更多