Scalable circle to fit screen size

It was a while when I was stuck with this. I am an Android developer.

I have a circular circle.

circular_shape.xml

<?xml version="1.0" encoding="utf-8"?>    
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:useLevel="false"
    android:shape="ring"
    android:thickness="0.8dp">
    <solid android:color="@android:color/holo_blue_dark"/>
</shape>

Now I want to use this circle in the activity layout so that it covers the entire screen. That is, the width of the screen should be equal to the diameter of the circle. Please note that I did not set the radius of the circle.

example_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/exampleLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.anil.raceorganizer.ExampleActivity">
    <ImageView
        android:id="@+id/race_field"
        android:src="@drawable/circular_shape"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/darker_gray"
        android:padding="0dp"
        android:layout_margin="0dp"
        ></ImageView>
</FrameLayout>

Note that I tried to indent and margin as 0, but that didn't work. I also tried to set the size of the ImageView code through the code, but it only resized the ImageView as a whole, not just the circle.

This is what I get with this code.

enter image description here

Any help is greatly appreciated.

+4
4

:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:thickness="0.8dp"
    android:useLevel="false"
    android:innerRadiusRatio="2.1">
    <solid android:color="@android:color/holo_blue_dark" />
</shape>
+2

Canvas, , .

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

    Canvas c = new Canvas(bmp);

    RectF rect = new RectF(0,0,width,width);
    drawCircle(rect, c, width, height);

}

private void drawCircle(RectF rect, Canvas c, int width, int height) {
        Paint paint = new Paint();
        paint.setARGB(255, 255 , 10, 21);
        paint.setStrokeWidth(8);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStyle(Paint.Style.STROKE);
        int radius;
        if(width < height)
            radius = width/2;
        else 
            radius = height/2;

        radius-= 4;
        c.drawCircle(width/2, height/2, radius, paint);
    }

, . , . , downvotes!

+1

drawable

android:innerRadius="150dp"

Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;

,

<?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:useLevel="false"
        android:shape="ring"
        android:innerRadius="150dp"
        android:thickness="0.8dp">
        <solid android:color="@android:color/holo_blue_dark"/>
    </shape>
0

.

, 1:1

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<size android:width="50dp"
 android:height="50dp"/>

<stroke android:color="#000" android:width="0.1dp"/>

</shape>
0

Source: https://habr.com/ru/post/1651650/


All Articles