Jak zmienić kolor postępu paska postępu w Androidzie

Używam poziomego paska postępu w mojej aplikacji na Androida i chcę zmienić jego kolor postępu (domyślnie Żółty). Jak mogę to zrobić używając code (nie XML)?

Author: Mahozad, 2010-01-07

30 answers

Przykro mi, że to nie jest odpowiedź, ale co napędza wymóg ustawiania go z kodu ? I .setProgressDrawable powinno działać, jeśli jest poprawnie zdefiniowane

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/background">
    <shape>
        <corners android:radius="5dip" />
        <gradient
                android:startColor="#ff9d9e9d"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:angle="270"
        />
    </shape>
</item>

<item android:id="@android:id/secondaryProgress">
    <clip>
        <shape>
            <corners android:radius="5dip" />
            <gradient
                    android:startColor="#80ffd300"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:angle="270"
            />
        </shape>
    </clip>
</item>

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners
                android:radius="5dip" />
            <gradient
                android:startColor="@color/progress_start"
                android:endColor="@color/progress_end"
                android:angle="270" 
            />
        </shape>
    </clip>
</item>

</layer-list>
 303
Author: Alex Volovoy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-27 11:23:46

Dla poziomego paska postępu, możesz użyć ColorFilter, również, jak to:

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

Czerwony pasek postępu za pomocą filtra kolorów

Uwaga: zmienia to wygląd wszystkich pasków postępu w aplikacji. Aby zmodyfikować tylko jeden konkretny pasek postępu, zrób to:

Drawable progressDrawable = progressBar.getProgressDrawable().mutate();
progressDrawable.setColorFilter(Color.RED, android.graphics.PorterDuff.Mode.SRC_IN);
progressBar.setProgressDrawable(progressDrawable);

Jeśli pasek postępu jest nieokreślony, użyj getIndeterminateDrawable() zamiast getProgressDrawable().

Od Lollipop (API 21) można ustawić Odcień postępu:

progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));

Czerwony pasek postępu przy użyciu odcienia postępu

 500
Author: Torben Kohlmeier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-24 12:12:55

To nie jest programowo, ale myślę, że i tak może pomóc wielu ludziom.
Bardzo się starałem i najskuteczniejszym sposobem było dodanie tej linii do mojego Progresbara w .plik xml:

            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary"

Więc w końcu ten kod zrobił to za mnie:

<ProgressBar
            android:id="@+id/progressBar"
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_marginTop="50dp"
            android:layout_marginBottom="50dp"
            android:visibility="visible"
            android:indeterminate="true"
            android:indeterminateTintMode="src_atop"
            android:indeterminateTint="@color/secondary">

To rozwiązanie działa dla API 21+

 427
Author: FFirmenich,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-06-17 11:39:24

Dla mojego nieokreślonego progresbara (spinnera) ustawiłem filtr kolorów na rysunku. Działa świetnie i tylko jedna linijka.

Przykład gdzie ustawienie koloru na czerwony:

ProgressBar spinner = new android.widget.ProgressBar(
                context,
                null,
                android.R.attr.progressBarStyle);

spinner.getIndeterminateDrawable().setColorFilter(0xFFFF0000, android.graphics.PorterDuff.Mode.MULTIPLY);

Tutaj wpisz opis obrazka

 238
Author: jhavatar,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-07-31 07:05:11

To jest stare pytanie, ale używanie tematu nie jest tutaj wymienione. Jeśli domyślnym motywem jest AppCompat, zdefiniowany przez Ciebie Kolor ProgressBar będzie colorAccent.

Zmiana colorAccent zmieni również kolor twojego ProgressBar, ale tam zmiany odzwierciedlają się również w wielu miejscach. Tak więc, jeśli chcesz inny kolor tylko dla określonego PregressBar, możesz to zrobić, stosując motyw do tego ProgressBar:

  • Rozszerz domyślny motyw i nadpisaj colorAccent

    <style name="AppTheme.WhiteAccent">
        <item name="colorAccent">@color/white</item> <!-- Whatever color you want-->
    </style>
    
  • I w ProgressBar Dodaj atrybut android:theme:

    android:theme="@style/AppTheme.WhiteAccent"
    

Więc będzie to wyglądało mniej więcej tak:

<ProgressBar
        android:id="@+id/loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:padding="10dp"
        android:theme="@style/AppTheme.WhiteAccent" />

Więc po prostu zmieniasz colorAccent dla swojego konkretnego ProgressBar.

Uwaga : użycie {[15] } nie zadziała. Musisz używać tylko android:theme. Możesz znaleźć więcej wykorzystania motywu tutaj: https://plus.google.com/u/0 / + AndroidDevelopers / posts / JXHKyhsWHAH

 204
Author: kirtan403,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-07-09 13:08:28

Wszystkie API

Jeśli używasz wszystkich API, Utwórz motyw w stylu

Style.xml

<resources>

    //...

    <style name="progressBarBlue" parent="@style/Theme.AppCompat">
        <item name="colorAccent">@color/blue</item>
    </style>

</resources>

I używać w toku

<ProgressBar
    ...
    android:theme="@style/progressBarBlue" />

API poziom 21 i wyższy

Jeśli jest używany w API na poziomie 21 i wyższym, użyj tego kodu:

<ProgressBar
   //...
   android:indeterminate="true"
   android:indeterminateTintMode="src_atop"
   android:indeterminateTint="@color/secondary"/>
 63
Author: Rasoul Miri,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-09-24 14:03:38

To mi pasuje. Działa również dla niższej wersji zbyt. Dodaj to do swoich syles.xml

<style name="ProgressBarTheme" parent="ThemeOverlay.AppCompat.Light">
<item name="colorAccent">@color/colorPrimary</item>
</style>

I używaj go tak w xml

<ProgressBar
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:theme="@style/ProgressBarTheme"
   />
 45
Author: Devinder Jhinjer,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-09-25 05:39:24

Zgodnie z niektórymi sugestiami, można określić kształt i clipdrawable z kolorem, a następnie ustawić go. Mam taki program pracy. Tak to robię..

Najpierw upewnij się, że zaimportowałeś bibliotekę drawable..

import android.graphics.drawable.*;

Następnie użyj kodu podobnego do poniższego;

ProgressBar pg = (ProgressBar)row.findViewById(R.id.progress);
final float[] roundedCorners = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 };
pgDrawable = new ShapeDrawable(new RoundRectShape(roundedCorners, null,null));
String MyColor = "#FF00FF";
pgDrawable.getPaint().setColor(Color.parseColor(MyColor));
ClipDrawable progress = new ClipDrawable(pgDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
pg.setProgressDrawable(progress);   
pg.setBackgroundDrawable(getResources().getDrawable(android.R.drawable.progress_horizontal));
pg.setProgress(45);
 34
Author: PaulieG,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-03-06 09:46:33

To mi pomogło:

<ProgressBar
 android:indeterminateTint="#d60909"
 ... />
 33
Author: MOH3N,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-06 05:40:16

Jeśli Nieokreślony:

((ProgressBar)findViewById(R.id.progressBar))
    .getIndeterminateDrawable()
    .setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
 30
Author: the.knife,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-03-22 18:37:47

Obecnie w 2016 znalazłem kilka urządzeń pre-Lollipop nie honoruje ustawienia colorAccent, więc moje ostateczne rozwiązanie dla wszystkich API jest teraz następujące:

// fixes pre-Lollipop progressBar indeterminateDrawable tinting
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

    Drawable wrapDrawable = DrawableCompat.wrap(mProgressBar.getIndeterminateDrawable());
    DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(getContext(), android.R.color.holo_green_light));
    mProgressBar.setIndeterminateDrawable(DrawableCompat.unwrap(wrapDrawable));
} else {
    mProgressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), android.R.color.holo_green_light), PorterDuff.Mode.SRC_IN);
}

Dla punktów bonusowych, nie używa żadnego przestarzałego kodu. Spróbuj!

 24
Author: Henrique de Sousa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-04-21 10:55:00

To właśnie zrobiłem. Zadziałało.

ProgressBar:

<ProgressBar
            android:id="@+id/progressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="4"
            android:indeterminateDrawable="@drawable/progressdrawable"
           />

/ align = "left" / xml:
tutaj użyj gradientu, aby zmienić kolor, jak chcesz. I android: toDegrees= " X " increse wartość X i pasek postępu obraca się szybko. Zmniejsz i obróć powoli.Dostosuj do swoich potrzeb.

<?xml version="1.0" encoding="utf-8"?>
     <rotate xmlns:android="http://schemas.android.com/apk/res/android"
            android:duration="4000"
            android:fromDegrees="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toDegrees="360" >

            <shape
                android:innerRadius="20dp"
                android:shape="ring"
                android:thickness="4dp"
                android:useLevel="false" >
                <size
                    android:height="48dp"
                    android:width="48dp" />

                <gradient
                    android:centerColor="#80ec7e2a"
                    android:centerY="0.5"
                    android:endColor="#ffec7e2a"
                    android:startColor="#00ec7e2a"
                    android:type="sweep"
                    android:useLevel="false" />
            </shape>

        </rotate>

Próbka: Tutaj wpisz opis obrazka

 21
Author: Debasish Ghosh,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-01-31 06:44:23

Wystąpił ten sam problem podczas pracy nad modyfikacją wyglądu domyślnego paska postępu. Oto kilka informacji, które, mam nadzieję, pomogą ludziom:)

  • nazwa pliku xml musi zawierać tylko znaki: a-z0-9_. (tj. bez stolic!)
  • aby odnieść się do twojego "drawable" to R.drawable.filename
  • aby nadpisać domyślny wygląd, używasz myProgressBar.setProgressDrawable(...), jednak nie musisz po prostu odnosić się do niestandardowego układu jako R.drawable.filename, musisz pobrać go jako Drawable:
    Resources res = getResources();
    myProgressBar.setProgressDrawable(res.getDrawable(R.drawable.filename);
    
  • musisz ustawić styl przed ustawieniem progress/secondary progress / max (ustawienie go później dla mnie spowodowało 'pusty' pasek postępu)
 18
Author: pyko,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-02-28 12:41:32
android:progressTint="#ffffff" 
 18
Author: Paul,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-09-14 13:34:15

Jest chyba jedna rzecz, o której nie wspomniano w tej odpowiedzi:

Jeśli Twój motyw dziedziczy z Theme.AppCompat, ProgressBar przyjmie kolor zdefiniowany jako "colorAccent" w Twoim motywie.

Więc, używając..

<item name="colorAccent">@color/custom_color</item>

..automatycznie odbarwi kolor paska postępu na @color/custom_color.

 15
Author: Henrique de Sousa,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2016-01-21 18:53:00

Możesz spróbować zmienić swoje style, motywy lub używając Androida: indeterminateTint=" @ color / yourColor", gdziekolwiek chcesz, ale jest tylko jeden sposób, który będzie działał na dowolnej wersji Androida SKD:

Jeśli pasek postępu nie jest nieokreślony, użyj:

progressBar.getProgressDrawable().setColorFilter(ContextCompat.getColor(context, R.color.yourColor), PorterDuff.Mode.SRC_IN );

Jeśli pasek postępu jest nieokreślony, użyj:

progressBar.getIndeterminateDrawable().setColorFilter(ContextCompat.getColor(getContext(), R.color.yourColor), PorterDuff.Mode.SRC_IN );
To smutne, że Android jest taki bałagan!
 15
Author: Adriano Moutinho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-04 16:53:02

Jak to zrobiłem w Progresbarze poziomym:

    LayerDrawable layerDrawable = (LayerDrawable) progressBar.getProgressDrawable();
    Drawable progressDrawable = layerDrawable.findDrawableByLayerId(android.R.id.progress);
    progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
 14
Author: mieszk3,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-12-16 11:36:05

Najprostsze rozwiązanie jeśli chcesz zmienić kolor w pliku XML układu, użyj poniższego kodu i użyj właściwości indeterminateTint dla żądanego koloru.

    <ProgressBar
      android:id="@+id/progressBar"
      style="?android:attr/progressBarStyle"
      android:layout_width="wrap_content"
      android:indeterminate="true"
      android:indeterminateTintMode="src_atop"
      android:indeterminateTint="#ddbd4e"
      android:layout_height="wrap_content"
      android:layout_marginBottom="20dp"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
 13
Author: Naveed Ahmad,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-11-25 04:27:46

To rozwiązanie zadziałało dla mnie:

<style name="Progressbar.White" parent="AppTheme">
    <item name="colorControlActivated">@color/white</item>
</style>

<ProgressBar
    android:layout_width="@dimen/d_40"
    android:layout_height="@dimen/d_40"
    android:indeterminate="true"
    android:theme="@style/Progressbar.White"/>
 12
Author: S.Javed,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-08-01 15:11:17

Jeszcze jedna mała rzecz, rozwiązanie motyw działa, jeśli dziedziczysz motyw podstawowy, więc dla App compact Twój motyw powinien być:

<style name="AppTheme.Custom" parent="@style/Theme.AppCompat">
    <item name="colorAccent">@color/custom</item>
</style>

A następnie ustaw to w temacie paska postępu

<ProgressBar
    android:id="@+id/progressCircle_progressBar"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:theme="@style/AppTheme.Custom"
    android:indeterminate="true"/>
 8
Author: Calin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 19:27:53

Aby zmienić kolor paska postępu poziomego (w kotlinie):

fun tintHorizontalProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.progressTintList = ColorStateList.valueOf(color)
    } else{
        val layerDrawable = progress.progressDrawable as? LayerDrawable
        val progressDrawable = layerDrawable?.findDrawableByLayerId(android.R.id.progress)
        progressDrawable?.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
    }
}

Aby zmienić nieokreślony Kolor paska postępu:

fun tintIndeterminateProgress(progress: ProgressBar, @ColorInt color: Int = ContextCompat.getColor(progress.context, R.color.colorPrimary)){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        progress.indeterminateTintList = ColorStateList.valueOf(color)
    } else {
        (progress.indeterminateDrawable as? LayerDrawable)?.apply {
            if (numberOfLayers >= 2) {
                setId(0, android.R.id.progress)
                setId(1, android.R.id.secondaryProgress)
                val progressDrawable = findDrawableByLayerId(android.R.id.progress).mutate()
                progressDrawable.setColorFilter(color, PorterDuff.Mode.SRC_ATOP)
            }
        }
    }
}

I w końcu normalnie odcień pre-Lollipop progressBars

przyciemniane postępy na api 19

 7
Author: John,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-06-20 09:12:55

Po prostu użyj:

DrawableCompat.setTint(progressBar.getIndeterminateDrawable(),yourColor)
 7
Author: Amir Hossein Ghasemi,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-05-03 14:15:57

Zgodnie z sugestią Muhammada-Adila dla SDK ver 21 i nowszych

android:indeterminateTint="@color/orange"

W XML działa dla mnie, jest wystarczająco łatwe.

 6
Author: mughil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-09 10:56:10

Poziomy pasek postępu niestandardowy styl materiału:

Aby zmienić kolor tła i postęp poziomego paska postępu.

<style name="MyProgressBar" parent="@style/Widget.AppCompat.ProgressBar.Horizontal">
    <item name="android:progressBackgroundTint">#69f0ae</item>
    <item name="android:progressTint">#b71c1c</item>
    <item name="android:minWidth">200dp</item>
</style>

Zastosuj go do paska postępu, ustawiając atrybut style, dla niestandardowych stylów materiału i niestandardowego wyboru paska postępu http://www.zoftino.com/android-progressbar-and-custom-progressbar-examples

 6
Author: Arnav Rao,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-21 14:53:24

Użyj Androida .wsparcie.v4.grafika.drawable.DrawableCompat :

            Drawable progressDrawable = progressBar.getIndeterminateDrawable();
            if (progressDrawable  != null) {
                Drawable mutateDrawable = progressDrawable.mutate();
                DrawableCompat.setTint(mutateDrawable, primaryColor);
                progressBar.setProgressDrawable(mutateDrawable);
            }
 6
Author: Alécio Carvalho,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-03-15 13:35:17

Wysłany, aby dodać informacje o odpowiedź Pauliega, ponieważ ateiob poprosił mnie o wyjaśnienie czegoś...


Mogę powiedzieć ,że istnieje (a przynajmniej był, w momencie pisania, kiedy patrzyłem na tę bieżącą wersję kodu źródłowego Androida) błąd/problem/optymalizacja w kodzie ProgressBar, który ignoruje próbę ustawienia postępu na wartość, która już jest.

  • tzn. jeśli progress = 45 i spróbujesz ustawić go na 45, kod nic nie zrobi, a nie przerysuje progress .

Po wywołaniu ProgressBar.setProgressDrawable() pasek postępu będzie pusty(ponieważ zmieniłeś część drawable).

Oznacza to, że musisz ustawić postęp i przerysować go. Ale jeśli ustawisz postęp na zachowaną wartość, nic nie zrobi.

Najpierw musisz ustawić go na 0, a następnie ponownie na wartość "old", a pasek zostanie ponownie narysowany.


Więc podsumowując:

  • zachowaj "starą" wartość postępu
  • update the drawable / colour (sprawia, że pasek jest pusty)
  • Resetuj postęp do 0 (w przeciwnym razie następna linia nic nie robi)
  • Resetuj postęp do "starej" wartości (Pasek poprawek)
  • unieważnić

Poniżej jest metoda, którą mam, która robi to:

protected void onResume()
{
    super.onResume();
    progBar = (ProgressBar) findViewById(R.id.progress_base);

    int oldProgress = progBar.getProgress();

    // define new drawable/colour
    final float[] roundedCorners = new float[]
        { 5, 5, 5, 5, 5, 5, 5, 5 };
    ShapeDrawable shape = new ShapeDrawable(new RoundRectShape(
        roundedCorners, null, null));
    String MyColor = "#FF00FF";
    shape.getPaint().setColor(Color.parseColor(MyColor));
    ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,
        ClipDrawable.HORIZONTAL);
    progBar.setProgressDrawable(clip);

    progBar.setBackgroundDrawable(getResources().getDrawable(
        android.R.drawable.progress_horizontal));

    // work around: setProgress() ignores a change to the same value
    progBar.setProgress(0);
    progBar.setProgress(oldProgress);

    progBar.invalidate();
}

Jeśli chodzi o rozwiązanie HappyEngineer, myślę, że było to podobne obejście, aby ręcznie ustawić przesunięcie "postępu". W obu przypadkach powyższy kod powinien działać dla Ciebie.

 6
Author: Richard Le Mesurier,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-12-15 20:38:42

Po prostu użyj:

PorterDuff.Mode mode = PorterDuff.Mode.SRC_IN;
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.GINGERBREAD_MR1) {
    mode = PorterDuff.Mode.MULTIPLY;
}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    progressBar.setProgressTintList(ColorStateList.valueOf(Color.RED));
    progressBar.setProgressBackgroundTintList(ColorStateList.valueOf(Color.RED));
} else {
    Drawable progressDrawable;
    progressDrawable = (progressBar.isIndeterminate() ? progressBar.getIndeterminateDrawable() : progressBar.getProgressDrawable()).mutate();
    progressDrawable.setColorFilter(context.getResources().getColor(Color.RED), mode);
    progressBar.setProgressDrawable(progressDrawable);
}
 6
Author: Mohammad Fneish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2019-08-10 15:57:04

Najprostszym sposobem zmiany koloru pierwszego planu i tła paska postępu jest

<ProgressBar
                        style="@android:style/Widget.ProgressBar.Horizontal"
                        android:id="@+id/pb_main"
                        android:layout_width="match_parent"
                        android:layout_height="8dp"
                        android:progress="30"
                        android:progressTint="#82e9de"
                        android:progressBackgroundTint="#82e9de"
                        />

Po prostu dodaj

                        android:progressTint="#82e9de" //for foreground colour
                        android:progressBackgroundTint="#82e9de" //for background colour
 6
Author: Satyam Patil,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2020-04-18 06:50:19

Dla poziomego paska postępu stylów używam:

import android.widget.ProgressBar;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ClipDrawable;
import android.view.Gravity;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;

public void setColours(ProgressBar progressBar,
                        int bgCol1, int bgCol2, 
                        int fg1Col1, int fg1Col2, int value1,
                        int fg2Col1, int fg2Col2, int value2)
  {
    //If solid colours are required for an element, then set
    //that elements Col1 param s the same as its Col2 param
    //(eg fg1Col1 == fg1Col2).

    //fgGradDirection and/or bgGradDirection could be parameters
    //if you require other gradient directions eg LEFT_RIGHT.

    GradientDrawable.Orientation fgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;
    GradientDrawable.Orientation bgGradDirection
        = GradientDrawable.Orientation.TOP_BOTTOM;

    //Background
    GradientDrawable bgGradDrawable = new GradientDrawable(
            bgGradDirection, new int[]{bgCol1, bgCol2});
    bgGradDrawable.setShape(GradientDrawable.RECTANGLE);
    bgGradDrawable.setCornerRadius(5);
    ClipDrawable bgclip = new ClipDrawable(
            bgGradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);     
    bgclip.setLevel(10000);

    //SecondaryProgress
    GradientDrawable fg2GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg2Col1, fg2Col2});
    fg2GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg2GradDrawable.setCornerRadius(5);
    ClipDrawable fg2clip = new ClipDrawable(
            fg2GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Progress
    GradientDrawable fg1GradDrawable = new GradientDrawable(
            fgGradDirection, new int[]{fg1Col1, fg1Col2});
    fg1GradDrawable.setShape(GradientDrawable.RECTANGLE);
    fg1GradDrawable.setCornerRadius(5);
    ClipDrawable fg1clip = new ClipDrawable(
            fg1GradDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);        

    //Setup LayerDrawable and assign to progressBar
    Drawable[] progressDrawables = {bgclip, fg2clip, fg1clip};
    LayerDrawable progressLayerDrawable = new LayerDrawable(progressDrawables);     
    progressLayerDrawable.setId(0, android.R.id.background);
    progressLayerDrawable.setId(1, android.R.id.secondaryProgress);
    progressLayerDrawable.setId(2, android.R.id.progress);

    //Copy the existing ProgressDrawable bounds to the new one.
    Rect bounds = progressBar.getProgressDrawable().getBounds();
    progressBar.setProgressDrawable(progressLayerDrawable);     
    progressBar.getProgressDrawable().setBounds(bounds);

    // setProgress() ignores a change to the same value, so:
    if (value1 == 0)
        progressBar.setProgress(1);
    else
        progressBar.setProgress(0);
    progressBar.setProgress(value1);

    // setSecondaryProgress() ignores a change to the same value, so:
    if (value2 == 0)
        progressBar.setSecondaryProgress(1);
    else
        progressBar.setSecondaryProgress(0);
    progressBar.setSecondaryProgress(value2);

    //now force a redraw
    progressBar.invalidate();
  }

Przykładowe wywołanie to:

  setColours(myProgressBar, 
          0xff303030,   //bgCol1  grey 
          0xff909090,   //bgCol2  lighter grey 
          0xff0000FF,   //fg1Col1 blue 
          0xffFFFFFF,   //fg1Col2 white
          50,           //value1
          0xffFF0000,   //fg2Col1 red 
          0xffFFFFFF,   //fg2Col2 white
          75);          //value2

Jeśli nie potrzebujesz 'wtórnego postępu', po prostu ustaw wartość 2 na wartość 1.

 4
Author: Keith,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-10-27 16:04:40

Zastosuj ten niestandardowy styl na pasku postępu.

<style name="customProgress" parent="@android:style/Widget.ProgressBar.Small">
        <item name="android:indeterminateDrawable">@drawable/progress</item>
        <item name="android:duration">40</item>
        <item name="android:animationCache">true</item>
        <item name="android:drawingCacheQuality">low</item>
        <item name="android:persistentDrawingCache">animation</item>
    </style>

@ drawable / progress.xml -

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white"
    android:pivotX="50%"
    android:pivotY="50%" />

Użyj tego typu obrazu dla paska postępu. Tutaj wpisz opis obrazka

Aby uzyskać lepszy wynik, możesz użyć wielu obrazów postępu. I nie wahaj się używać obrazów, ponieważ sama Platforma Android używa obrazów do paska progressbar. Kod jest wydobywany z sdk:)

 4
Author: Neo,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-09-26 13:55:45