动画安卓进度条

时间:2016-08-19 15:20:04

标签: android

我在我的视图中创建了ProgressBar,我希望它在更新值时设置动画。我在API 23上运行了代码。如何在更新它的值时为progressBar设置动画?

这是我的活动:

package com.brylkowski.cleaner.cleaner.Errands;

import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;

import com.brylkowski.cleaner.cleaner.R;

public class UploadErrandActivity extends AppCompatActivity {

    public static final String PROGRESS = "progress";
    UploadRequestTask task;
    private ProgressBar progressBar;
    private Handler handler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upload_errand);
        progressBar = (ProgressBar) findViewById(R.id.upload_progress);
        progressBar.setMax(6);
        task = new UploadRequestTask();
        handler = new Handler() {
            public void handleMessage(android.os.Message msg) {
                progressBar.incrementProgressBy(1);

            }
        };
        task.execute();
    }

    public class UploadRequestTask extends AsyncTask<Void, Void, Boolean> {

        UploadRequestTask() {
        }

        @Override
        protected Boolean doInBackground(Void... params) {
            for (int i = 1; i <=6; i++){
                Message msg = new Message();
                Bundle bundle = new Bundle();
                bundle.putInt(PROGRESS, i);
                msg.setData(bundle);
                handler.sendMessage(msg);
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            return true;
        }

        @Override
        protected void onPostExecute(final Boolean success) {
        }

        @Override
        protected void onCancelled() {
        }
    }
}

和我的观点:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:gravity="center"
    tools:context="com.brylkowski.cleaner.cleaner.Errands.UploadErrandActivity">

    <ProgressBar
        style="?android:attr/progressBarStyleHorizontal"
        android:indeterminate="false"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/upload_progress"
        android:layout_gravity="bottom"
        android:longClickable="false"/>


</LinearLayout>

1 个答案:

答案 0 :(得分:0)

检查this有关如何制作ProgressBar动画的信息。您需要的唯一修改是在初始化视图时应用动画一次,您需要调用

ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, progressBar.getProgress(), to);
anim.setDuration(1000);
progressBar.startAnimation(anim);  // play animation immediately

每次您想要更新价值。 Here是视图动画的文档。