Dynamiczna zmiana koloru shape'a

0

Witam,

Mam pliczek xml opisujący progress bar:

 
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/background">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:useLevel="false"
        android:thicknessRatio="6.4">
        <solid android:color="@android:color/transparent" />
        <stroke 
            android:width="2dp"
            android:color="@android:color/darker_gray" />
    </shape>
</item>
<item android:id="@+id/progress">
    <rotate
        android:fromDegrees="270"
        android:toDegrees="270">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="7.0">
            <solid
                android:id="@+id/progressColor"
                android:color="#FBB817" />
        </shape>
    </rotate>
  </item>
</layer-list>

Używany tutaj:

 
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="150dip"
    android:id="@+id/relativeLayout">

    <ProgressBar
        android:id="@+id/progressBar"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:layout_centerHorizontal="true"
        android:indeterminate="false"
        android:progressDrawable="@drawable/progressbar" />

    <TextView
        android:id="@+id/tvProgress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="progress" />
</RelativeLayout>

Potrzebuję zmienić kolor solid'u o id progressColor w czasie rzeczywistym.

Próbowałem w ten sposób:

LayerDrawable layerDrawable = (LayerDrawable) getResources().getDrawable(R.drawable.progressbar);
layerDrawable.setDrawableByLayerId(R.id.progressColor, new ColorDrawable(Color.BLUE));
progressBar.setProgressDrawable(layerDrawable);

Niestety nic się nie dzieje. Kolor paska pozostaje ten sam.

Znalazłem jakąś solucję na stack overflow:

View v = findViewById(R.id.relativeLayout);
LayerDrawable layerDrawable = (LayerDrawable) v.getBackground();
final GradientDrawable shape = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.progressColor);
shape.setColor(Color.BLACK);

Ale niestety ten kod wykrzacza apkę.

Z góry dziękuję za pomoc.

1
progressColor.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);
0
panryz napisał(a):
progressColor.getProgressDrawable().setColorFilter(Color.RED, Mode.SRC_IN);

Dzięki za odpowiedź. Nie potrafiłem wymusić zmiany koloru danego elementu przy pomocy setColorFilter().

Przy wywołaniu:

 
progressBar.getProgressDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

Cały progressBar zamieniał kolor na czerwony. A nie za bardzo wiem jak odnieść się bezpośrednio do "progressColor".

Aczkolwiek poradziłem sobie. W trochę mało elegancki sposób, ale działa. Stworzyłem kilka drawablów, z różnymi kolorami i podmieniam je przy pomocy:

 
Drawable drawable = getResources().getDrawable(R.drawable.progressbar1);
progressBar.setProgressDrawable(drawable);

1 użytkowników online, w tym zalogowanych: 0, gości: 1