Kotlin programmatically Drawable - change Gradient colors (startColor, centerColor, endColor)

977 Views Asked by At

Please help me translate to programmatically progressDrawable. I need three kinds of colors to:

        <gradient
            android:id="@+id/progressCompensationColor"
            android:startColor="@color/colorCompensationEasyGreen"
            android:centerColor="@color/colorCompensationMediumGreen"
            android:endColor="@color/colorCompensationHardGreen"
            android:centerX="0.3"
            android:type="sweep" />

MainDashboardFragment.kt

    var colorEasy = ContextCompat.getColor(activity as MainActivity, R.color.colorProgressBlueLight)
    var colorMedium = ContextCompat.getColor(activity as MainActivity, R.color.colorProgressBlueLight)
    var colorHard = ContextCompat.getColor(activity as MainActivity, R.color.colorProgressBlueLight)
    if (value <= 33.0) {
        colorEasy = ContextCompat.getColor(activity as MainActivity, R.color.colorRedEasy)
        colorMedium = ContextCompat.getColor(activity as MainActivity, R.color.colorRedMedium)
        colorHard = ContextCompat.getColor(activity as MainActivity, R.color.colorRedHard)
    } else if (value >= 66.0) {
        colorEasy = ContextCompat.getColor(activity as MainActivity, R.color.colorGreenEasy)
        colorMedium = ContextCompat.getColor(activity as MainActivity, R.color.colorGreenMedium)
        colorHard = ContextCompat.getColor(activity as MainActivity, R.color.colorGreenHard)
    }val customProgessBar = view.findViewById<ProgressBar>(R.id.progressCompensationScore)

fragment_main_dashboard_paired.xml

                <com.mysasy.mysasymobile.ui.CustomProgressBar
                android:id="@+id/progressCompensationScore"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="150dp"
                android:layout_height="150dp"
                android:background="@drawable/background_progress_bar_circular_blue"
                android:indeterminate="false"
                android:min="50"
                android:max="150"
                android:progress="75"
                android:progressDrawable="@drawable/progress_circle" />

progress_circle.xml

<item>
    <shape
        android:innerRadiusRatio="2.15"
        android:shape="ring"
        android:thicknessRatio="50.0" >
        <gradient
            android:id="@+id/progressCompensationColor"
            android:startColor="@color/colorCompensationEasyGreen"
            android:centerColor="@color/colorCompensationMediumGreen"
            android:endColor="@color/colorCompensationHardGreen"
            android:centerX="0.3"
            android:type="sweep" />
    </shape>
</item>

Implement? Is this the right procedure?

        val customProgessBar = view.findViewById<ProgressBar>(R.id.progressCompensationScore)
    val draw = createProgressBar(Color.RED, Color.GREEN, Color.BLUE)
    customProgessBar.progressDrawable = draw

my inspiration: Gradient Circular Progress Bar

1

There are 1 best solutions below

1
On

You need GradientDrawable.

GradientDrawable drawable = new GradientDrawable();
drawable.setInnerRadiusRatio(2.15F);
drawable.setThicknessRatio(50F);
drawable.setShape(GradientDrawable.RING);
drawable.setColors(new int[]{colorStart, colorCenter, colorEnd});
drawable.setGradientCenter(0.3F, 0F);
drawable.setGradientType(GradientDrawable.SWEEP_GRADIENT);

// for demo displaying
drawable.setLevel(10000);

Or check this, a simple kotlin ext for dsl android shape resource.