0

我正在尝试在以下代码中添加加载对话框以从服务器获取图像并将其显示在图库视图中。它显示空白屏幕,直到图像出现。请帮助我如何在从服务器获取图像时显示加载对话框。

这是代码,请帮助。

    public class ImagedisplaytestActivity extends Activity {
        private ImageView leftArrowImageView;
        private ImageView rightArrowImageView;
        private Gallery gallery;
        public int selectedImagePosition;
        private GalleryImageAdapter galImageAdapter;
        private String bitmapImg = "";
        Bitmap bitmap = null;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            setupUI();
        }

        private void setupUI() {
            Intent i = getIntent();
            Bundle extras=i.getExtras();
            bitmapImg = extras.getString("BitmapImage");
            selectedImagePosition = extras.getInt("Pos");

            leftArrowImageView = (ImageView) findViewById(R.id.left_arrow_imageview);
            rightArrowImageView = (ImageView) findViewById(R.id.right_arrow_imageview);
            gallery = (Gallery) findViewById(R.id.gallery);
            leftArrowImageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedImagePosition > 0) {
                        --selectedImagePosition;
                    }
                    gallery.setSelection(selectedImagePosition, false);
                }
            });

            rightArrowImageView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selectedImagePosition < DetailView.bitmapURL.size() - 1) {
                        ++selectedImagePosition;
                    }
                    gallery.setSelection(selectedImagePosition, false);
                }
            });

            gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
                    selectedImagePosition = pos;
                    if (selectedImagePosition > 0 && selectedImagePosition < DetailView.bitmapURL.size() - 1) {
                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_disabled));
                        rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
                    } else if (selectedImagePosition == 0) {
                        leftArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_left_enabled));
                    } else if (selectedImagePosition == DetailView.bitmapURL.size() - 1) {
rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_enabled));
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> arg0) {
                }
            });

            galImageAdapter = new GalleryImageAdapter(this, DetailView.bitmapURL);
            gallery.setAdapter(galImageAdapter);
            if (DetailView.bitmapURL.size() > 0) {
                gallery.setSelection(selectedImagePosition, false);
            }
            if (DetailView.bitmapURL.size() == 1) {
                rightArrowImageView.setImageDrawable(getResources().getDrawable(R.drawable.arrow_right_disabled));
            }

        }

        public class GalleryImageAdapter extends BaseAdapter {
            private Activity context;
            private  ImageView imageView;
            private List<String>  plotsImages;
            private ViewHolder holder;
            public GalleryImageAdapter(Activity context, List<String> plotsImages) {
                this.context = context;
                this.plotsImages = plotsImages;
            }

            @Override
            public int getCount() {
                return plotsImages.size();
            }

            @Override
            public Object getItem(int position) {
                return null;
            }

            @Override
            public long getItemId(int position) {
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                if (convertView == null) {
                    holder = new ViewHolder();
                    imageView = new ImageView(this.context);
                    imageView.setPadding(3, 3, 3, 3);
                    convertView = imageView;
                    holder.imageView = imageView;
                    convertView.setTag(holder);
                } else {
                    holder = (ViewHolder) convertView.getTag();
                }
                holder.imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);

                try {
                    bitmap = DownloadImage(plotsImages.get(position));
                    holder.imageView.setImageBitmap(bitmap);
                    bitmap = null;
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return imageView;
            }

            private class ViewHolder {
                ImageView imageView;
            }


             private Bitmap DownloadImage(String URL){
                    Bitmap bitmap = null;
                    try {
                        InputStream in = OpenHttpConnection(URL);
                        bitmap = BitmapFactory.decodeStream(in);
                        in.close();
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }
                    return bitmap;
                }

                private InputStream OpenHttpConnection(String urlString) throws IOException{
                    InputStream in = null;
                    int response = -1;
                    URL url = new URL(urlString);
                    URLConnection conn = url.openConnection();

                    if (!(conn instanceof HttpURLConnection)) {
     throw new IOException("Not an HTTP connection");
                    }
                    try{
                        HttpURLConnection httpConn = (HttpURLConnection) conn;
                        httpConn.setAllowUserInteraction(false);
                        httpConn.setInstanceFollowRedirects(true);
                        httpConn.setRequestMethod("GET");
                        httpConn.connect();
                        response = httpConn.getResponseCode();
                        if (response == HttpURLConnection.HTTP_OK) {
                            in = httpConn.getInputStream();
                        }
                    }
                    catch (Exception ex){
     throw new IOException("Error connecting");
                    }
                    return in;
                }
    }

        @Override
        public void onBackPressed() {
            DetailView.bundleID = DetailView.idList.get(selectedImagePosition);
                    super.onBackPressed();
        }
    }
4

4 回答 4

0

你应该使用AsyncTask!也看这个!& 在Vogella 示例中将消除您的疑问。看到这个惰性图像加载 演示!在github上。

于 2013-01-03T07:22:58.000 回答
0

使用 assynctask,在该 assynctask 方法中,您可以编写从服务器获取图像的方法doinbackground(),此时您可以使用显示警报对话框preexectue(),从服务器加载后只需关闭警报对话框postexectue()

于 2013-01-03T07:16:41.880 回答
0

这段代码对我有用

private class LoadGlobalDataSearch extends AsyncTask<String, Void, Void>
    {

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);

            progressdialog.dismiss();


        }


        @Override
        protected Void doInBackground(String... params)
        {

            //Load your images this task
                        setupUI();
            return null;
        }

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
            progressdialog = ProgressDialog.show(MainActivity.this, "",getString(R.string.inprogress));
        }
    }

在你的 oncreate 方法中调用这个类

new loadImages().execute(0);
于 2013-01-03T07:17:40.977 回答
0

您可以使用进度对话框,直到下载图像然后删除对话框。

在 OpenHttpConnection 添加这个——

dialog = ProgressDialog.show(DutyRotaActivity.this, "",
                "Please wait for few seconds...", true);

在 DownloadImage 中关闭对话框。

dialog.dismiss();

或者你也可以使用异步任务。导致网络操作应始终在不在主线程中的其他线程上进行。在这种情况下,preExecute添加对话框并postExecute关闭对话框。并doinbackground调用您的 downloadimage 函数,该函数也可以解决问题。

编辑

这是一个完整的源代码和一个在listView. 我认为它也可以帮助您解决问题。谢谢

https://github.com/nostra13/Android-Universal-Image-Loader

于 2013-01-03T07:17:42.977 回答