Android - 在Progress Dialog上添加三个点动画

时间:2017-03-23 10:35:26

标签: java android animation mobile

我正在处理一个进度对话框,当按下该按钮时会弹出该对话框。

我已经获得了3点动画(使用JumpingBeans)。

但是现在,我想将它添加到Android进度对话框的标题中,我认为这是不可能的,不是吗?或者如何将textview(移动点)附加到Progress对话框?

三个点按钮没有动画,这是制作此自定义进度对话框的最佳方式吗?

我正在尝试使用ProgressBar和TextView创建一个DialogFragment,但是当我调用setMax和setProgress时,它总是出现NPE错误(空指针异常)。

是否有更简单的方法或更好的方法来处理带有点动画的自定义对话框片段?

这是图片(我想让点移动)

enter image description here

1 个答案:

答案 0 :(得分:0)

这是我通过在单独的线程上更新消息来解决它的方法。

似乎这样也适用于标题:

主要活动:

public class SplashActivity extends Activity {

    public final static long RUN_TIME = 30000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);

        gotoMain();
    }

    private void gotoMain() {

        final String messagePrefix = "Loading ";
        final ProgressDialog startupProgressDialog =
                ProgressDialogUtil.createProgressDialog(
                        SplashActivity.this,

                        // space added on init so text doesn't shift when adding chars for first time
                        messagePrefix + "      "
                );

        startupProgressDialog.setTitle(null);
        startupProgressDialog.show();

        Thread thread = new Thread() {
            @Override
            public void run() {
                ProgressDialogUtil
                        .runAnimatedLoadingDots(
                                SplashActivity.this,
                                messagePrefix,
                                startupProgressDialog);
            }
        };
        thread.start();

        new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {

                startupProgressDialog.dismiss();

                Intent mainIntent = new Intent(SplashActivity.this, MainBottomNavActivity.class);
                SplashActivity.this.startActivity(mainIntent);
                SplashActivity.this.finish();
            }
        }, RUN_TIME);
    }

}

<强> ProgressDialogUtil.java

public class ProgressDialogUtil {

    public static ProgressDialog createProgressDialog(Context context, String message) {
        ProgressDialog dialog = new ProgressDialog(context, R.style.DialogTheme1);
        dialog.setTitle("App Initialization");
        dialog.setMessage(message);
        dialog.setCancelable(false);
        return dialog;
    }

    public static void runAnimatedLoadingDots(Activity activity, String messagePrefix, final ProgressDialog progressDialog) {

        int dotsCount = 0;
        while (progressDialog != null && progressDialog.isShowing()) {

            dotsCount++;
            dotsCount = dotsCount % 5; // looks good w/4 dots

            try {
                Thread.sleep(300);
            }
            catch (InterruptedException iEx) {}

            final StringBuffer updateValue = new StringBuffer(messagePrefix);
            for (int i = 0; i < dotsCount; i++) {
                updateValue.append('.');
            }

            activity.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    progressDialog.setMessage(updateValue.toString());
                }
            });
        }

    }
}