简体   繁体   中英

Ring Drawable on pre 5.1 android

I'm new to android i'am creating an app where I include an ring shape inside one layout for that i'm using the below code:

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


              Log.e("(width/2)-70", (width/2)-70+"%%%");
              RingDrawable ring = new RingDrawable(0,(width/2)-70 , 0, 0);
               ring.setColor(Color.parseColor("#0f000000"));

               backgroundSpeaker.setBackground(ring);
             //  backgroundSpeaker.setAnimation(zoom);
               backgroundSpeaker.invalidate();
               speaker_layout.invalidate();

above code working fine but the issue is it's work only below 5.1.i'am not able to get any ring shape which in above 5.1 version.What went wrong is the code needs to be modified,Please help!!.Thanks in advance

To create a ring shape for older android versions, you can define a transparent circle with a colored stroke. The ring is the stroke itself:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval" >

<solid android:color="@color/transparent"/>
<stroke android:color="#fff" android:width="5dp" />

or the xml-less way:

GradientDrawable shape = new GradientDrawable();
shape.setColor(Color.Transparent);
shape.setStroke(20, Color.White);

Try using this one from here: https://github.com/MinaSamy/DailySelfie/blob/master/app/src/main/res/drawable/progress_drawable.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="100"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" >

    <shape
        android:innerRadius="20dp"
        android:shape="ring"
        android:thickness="4dp"
        android:useLevel="false" >
        <size
            android:height="48dp"
            android:width="48dp" />

        <gradient
            android:centerColor="@color/colorAccent"
            android:centerY="0.5"
            android:endColor="#00FFFFFF"
            android:startColor="@color/colorPrimaryDark"
            android:type="sweep"
            android:useLevel="false" />
    </shape>

</rotate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM