执行AsyncTask时,Android自定义进度指示器 [英] Android custom progress indicator while doing AsyncTask

查看:65
本文介绍了执行AsyncTask时,Android自定义进度指示器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行AsyncTask时,我想显示一个进度指示器.这个库存很丑,所以我用自己的东西做了一个布局.但是,这里存在一个问题,它是一个单独的布局,这意味着我需要一个新的活动来显示它.

When running an AsyncTask I want to display a progress indicator. This stock one is ugly so I made a layout with my own. There lies the problem however, it's a separate layout meaning I need a new activity to display it.

我以为我可以startActivity(〜progressActivity〜)然后...销毁它?但是似乎无法阻止父母的孩子活动.

I thought I could startActivity(~progressActivity~) and then ... destroy it? But it seems impossible to stop a child activity from the parent.

我可以在AsyncTask-invoking-activity上隐藏进度指示器,然后在进行处理时使其显示并隐藏其余的布局.现在,我在主布局中需要两个子布局,并且每次都隐藏其中一个.这听起来像是骇客,我不太喜欢它.

I could hide the progress indicator on the AsyncTask-invoking-activity, then when processing make it display and hide the rest of the layout. Now I need two sub-layouts in my main layout and hide one of each every time. This sounds like a hack and I don't fancy it much.

有什么建议吗?

推荐答案

如果您需要对ProgressBar的更多控制,则可以使用WindowManager在所有内容之上添加视图.无需任何其他布局,活动或窗口即可完成此操作.您可以像常规视图一样控制动画,触摸,位置和可见性.完整的工作代码:

If you need more control over your ProgressBar, you can use WindowManager to add a view on top of everything. It can be done without any additional layout, activities or windows. You can control animation, touches, position and visibility just like in case of regular view. Fully working code:

final ProgressBar view = new ProgressBar(TestActivity.this);
view.setBackgroundColor(0x7f000000);
final LayoutParams windowParams = new WindowManager.LayoutParams();
windowParams.gravity = Gravity.CENTER;
windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
        | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
windowParams.format = PixelFormat.TRANSLUCENT;
windowParams.windowAnimations = 0;

new AsyncTask<Integer, Integer, Integer>() {
    public void onPreExecute() {
        // init your dialog here;
        getWindowManager().addView(view, windowParams);
    }

    public void onPostExecute(Integer result) {
        getWindowManager().removeView(view);
        // process result;
    }

    @Override
    protected Integer doInBackground(Integer... arg0) {
        // do your things here
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}.execute();

这篇关于执行AsyncTask时,Android自定义进度指示器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆