在对话框/弹出窗口上加载图像时的ANDROID进度对话框

时间:2016-05-16 07:18:54

标签: java android

我有这个工作函数从url加载图像。唯一的事情是它在加载图像时没有显示任何进展,只是屏幕变得更暗,并且在一段时间之后仅显示图片。这使得用户认为它不起作用。那么如何在加载图像时添加进度对话框或以何种方式显示图像正在加载?

public void showImage() {
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    ImageView imageView = new ImageView(this);
    Picasso.with(this).load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png").into(imageView);
    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();
}

2 个答案:

答案 0 :(得分:4)

尝试添加PlaceHolder 将其添加到布局文件夹

<RelativeLayout
android:id="@+id/hoteldetails_myContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent">


<ProgressBar
    android:id="@+id/hoteldetails_progressBar"
    style="?android:attr/progressBarStyleSmall"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:visibility="gone"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layout_marginTop="20dp"
    android:layout_marginBottom="20dp" />

<ImageView
    android:id="@+id/hoteldetails_imageView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scaleType="fitXY"/>
</RelativeLayout>

这是加载代码

Picasso.with(this)
        .load(hoteImageStr)
        .error(R.drawable.loading)
        .into(img,  new com.squareup.picasso.Callback() {
            @Override
            public void onSuccess() {
                if (progress != null) {
                    progress .setVisibility(View.GONE);
                }
            }

            @Override
            public void onError() {

            }
        });

答案 1 :(得分:2)

public void showImage() {
    Dialog builder = new Dialog(this);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(android.graphics.Color.TRANSPARENT));
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });

    final ProgressDialog progressDialog = new ProgressDialog(this);
    ImageView imageView = new ImageView(this);
    Picasso.with(this)
            .load("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png")
            .into(imageView, new com.squareup.picasso.Callback() {
                @Override
                public void onSuccess() {

                    // The image has loaded you can make the progress bar invisible
                    if (progressDialog.isShowing())
                        progressDialog.dismiss();
                }

                @Override
                public void onError() {
                    // Show some error message
                    if (progressDialog.isShowing())
                        progressDialog.dismiss();
                }
            });


    builder.addContentView(imageView, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    builder.show();

    progressDialog.setMessage("please wait");
    progressDialog.setCancelable(false);
    progressDialog.show();
}