9

I want to create a drawable like this for my progress bar

ring shape color slice

and I've tried this code, but I didnt get what I want

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/progress">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:useLevel="false">
            <gradient
                android:centerColor="#ffff00"
                android:endColor="#00ff00"
                android:startColor="#fb0000"
                android:type="sweep" />
        </shape>
    </item>
</layer-list>

This is what I've got

gradient

Is it possible to do this with only xml? Or this is should be done with java code?

fadeltd
  • 349
  • 1
  • 4
  • 18

3 Answers3

1

Since you asked in comments how do you do that and I can't put these links in a comment, I'll suggest you these three links. Read them and extract what you want:

how to draw a half circle in android

Draw a semicircle in the background of a View

https://github.com/devadvance/circularseekbar

Community
  • 1
  • 1
A. Badakhshan
  • 1,045
  • 7
  • 22
0

Here's the solution for your question.

DifferentColorCircularBorder.java

public class DifferentColorCircularBorder {
    private RelativeLayout parentLayout;

    public DifferentColorCircularBorder(RelativeLayout parentLayout) {
        this.parentLayout = parentLayout;
    }

    public void addBorderPortion(Context context, int color, int startDegree, int endDegree) {
        ProgressBar portion = getBorderPortion(context, color, startDegree, endDegree);
        parentLayout.addView(portion);
    }

    private ProgressBar getBorderPortion(Context context, int color, int startDegree, int endDegree) {
        LayoutInflater inflater = LayoutInflater.from(context);

        ProgressBar portion = (ProgressBar) inflater.inflate(R.layout.border_portion, parentLayout, false);
        portion.setRotation(startDegree);
        portion.setProgress(endDegree - startDegree);

        portion.getProgressDrawable().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) portion.getLayoutParams();
        params.addRule(RelativeLayout.CENTER_IN_PARENT);
        portion.setLayoutParams(params);

        return portion;
    }
}

border_portion.xml

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="220dp"
    android:layout_height="220dp"
    android:progressDrawable="@drawable/circle_exterior"
    android:layout_centerInParent="true"
    android:max="360"/>

circle_exterior.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="ring"
    android:innerRadius="100dp"
    android:thickness="10dp" >

    <solid android:color="#ff111111" />
</shape>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RelativeLayout interiorLayout = (RelativeLayout) findViewById(R.id.interior);

        DifferentColorCircularBorder border = new DifferentColorCircularBorder(interiorLayout);
        border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleBlue), 0, 50);
        border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleGreen), 50, 120);
        border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleYellow), 120, 220);
        border.addBorderPortion(getApplicationContext(), ContextCompat.getColor(com.diffcolorborder.MainActivity.this, R.color.colorGoogleRed), 220, 360);
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#2d2d2d">

    <RelativeLayout
        android:id="@+id/interior"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <View
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:background="@drawable/profilepic" />
    </RelativeLayout>
</RelativeLayout>

enter image description here

MashukKhan
  • 1,946
  • 1
  • 27
  • 46
0

This is my try. A little different from what you want (it has transparent color).

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:useLevel="false">
            <gradient
                android:centerColor="#00FFFFFF"
                android:endColor="#00FFFFFF"
                android:startColor="#FF0000"
                android:type="sweep" />
        </shape>
    </item>
    <item>
        <rotate
            android:fromDegrees="120">
            <shape
                android:innerRadiusRatio="3"
                android:shape="ring"
                android:useLevel="false">
                <gradient
                    android:centerColor="#00FB0000"
                    android:endColor="#00FFFFFF"
                    android:startColor="#0000FF"
                    android:type="sweep" />
            </shape>
        </rotate>
    </item>
    <item>
        <rotate
            android:fromDegrees="240">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:useLevel="false">
            <gradient
                android:centerColor="#00FB0000"
                android:endColor="#00FFFFFF"
                android:startColor="#00FF00"
                android:type="sweep" />
        </shape>
        </rotate>
    </item>
</layer-list>

enter image description here

C.F.G
  • 817
  • 8
  • 16