Нажатие кнопки - анимация прогресса - PullRequest
1 голос
/ 13 марта 2019

Как получить эффект анимации, подобный следующему при нажатии кнопки?

enter image description here

Мне не нужно контролировать прогресс, мне просто нужноон заполняется через секунду после нажатия кнопки.Мне также не нужно менять цвет текста.

Я в растерянности относительно того, с чего начать такую ​​анимацию.Буду признателен за любую помощь, чтобы направить меня в правильном направлении.

1 Ответ

3 голосов
/ 13 марта 2019

Сначала создайте макет фрейма, как показано ниже:

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:indeterminate="false"
        android:progressDrawable="@drawable/progress_bar_style" />

    <TextView
        android:id="@+id/text_view_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:padding="18dp"
        android:text="Downloading"
        android:textColor="@android:color/holo_blue_bright"
        android:textSize="16sp"
        android:textStyle="bold" />

</FrameLayout>

Затем создайте файл progress_bar_style.xml поместите его в папку для рисования

Содержимое progress_bar_style.xml будет следующим

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape>
            <gradient
                android:startColor="#FFFFFF"
                android:centerColor="#FFFEFE"
                android:centerY="0.75"
                android:endColor="#FFFFFF"
                android:angle="270"
                />
        </shape>
    </item>



    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <gradient
                    android:startColor="#3F51B5"
                    android:centerColor="#3F51B5"
                    android:centerY="0.75"
                    android:endColor="#3F51B5"
                    android:angle="270"
                    />
            </shape>
        </clip>
    </item>

</layer-list>

Вот как вы будете реализовывать в упражнении / фрагменте

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_bar);

        final ObjectAnimator objectAnimator = ObjectAnimator.ofInt(progressBar, "progress",
                progressBar.getProgress(), 100).setDuration(2000);

        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                int progress = (int) valueAnimator.getAnimatedValue();
                progressBar.setProgress(progress);
            }
        });

        TextView btn = (TextView) findViewById(R.id.text_view_button);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                objectAnimator.start();
            }
        });
    }
}

Вывод

enter image description here

...