0

请帮我翻译成程序化的progressDrawable。我需要三种颜色:

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

实施?这是正确的程序吗?

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

我的灵感:渐变圆形进度条

4

1 回答 1

2

你需要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);

或者检查一下,一个简单的 kotlin ext 用于 dsl android 形状资源。

于 2021-11-27T07:39:29.823 回答