如何在Android中绘制一个半圈 [英] how to draw a half circle in android

查看:75
本文介绍了如何在Android中绘制一个半圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using this code to draw a half in my app:

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

输出:

但是我需要类似下面的内容:

but i need something like below :

如何绘制?

推荐答案

我建议通过代码绘制.

1-创建类MyView并将其放在下面的代码中.

1- Create class MyView and put below code.

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

2-Initialize this class inside you activity or fragment:-

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

这篇关于如何在Android中绘制一个半圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆