Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

how to draw a half circle in 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>

output :

enter image description here

but i need something like below :

enter image description here

how to draw this ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would suggest to draw it through code.

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-Initialize this class inside you activity or fragment:-

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...