how to draw a half circle in android

后端 未结 5 556
忘掉有多难
忘掉有多难 2021-01-02 09:42

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

  


        
相关标签:
5条回答
  • 2021-01-02 10:04

    You can use <clip /> drawable in order to cut-off part of your circle.

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

    0 讨论(0)
  • 2021-01-02 10:04
    <?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>
    
    0 讨论(0)
  • 2021-01-02 10:13

    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));
    }
    
    0 讨论(0)
  • 2021-01-02 10:13

    this is how i created my semi circle in a drawable xml file.

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

    0 讨论(0)
  • Your can use a rectangle shape .xml file and edit the corners on one side only.

    Example:

    <?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>
    
    0 讨论(0)
提交回复
热议问题