How to represent a circle border with multiple colors in android

I am looking for a custom widget to draw a circle with several border colors.

Say, for example, if my common circle represents 0-360, I need to color the border of the circle with different colors.

For example, I need to mark 0-60 with red, 61-120 with green, 121-300 with purple and 301-360 with yellow border.

please suggest me how can i do this in android.

+4
source share
2 answers

Your application is pretty simple. I do not recommend using an external library. You can quickly implement a class that draws and controls your desired shape. An example is given:

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, Mode.SRC_ATOP);

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

        return portion;
    }

}

border_portion defined below:

<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 defined here:

<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 :

public class MainActivity extends Activity {

    @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(), Color.RED, 0, 40);
        border.addBorderPortion(getApplicationContext(), Color.GREEN, 40, 90);
        border.addBorderPortion(getApplicationContext(), Color.BLUE, 90, 270);
        border.addBorderPortion(getApplicationContext(), 0xFF123456, 270, 360);

    }
}

finally activity_main layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <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:background="@drawable/circle_interior_bg"
            android:layout_centerInParent="true" />

    </RelativeLayout>

</RelativeLayout>

. . , . .

: This is the result of my code.

+6

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


All Articles