0

嗨,我的 MapView 应用程序需要很长时间才能加载,所以我想在加载时显示一个水平样式的 ProgressDialog。当我尝试在 onCreate/onStart 方法中显示对话框时,对话框刚刚以 100% 完成的方式显示在最后,我现在意识到这是因为在 onCreate/onStart 方法之后没有将任何内容绘制到屏幕上。

所以我想做的是显示基本地图,然后在绘制地图后执行要求的代码,这样我也可以显示一个 ProgressDialog。这是可能的吗?如果是的话怎么办?

提前致谢!=]

编辑:

这是源代码:

public class GoogleMapsActivity extends MapActivity {
     private List<Overlay>  mapOverlays;

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);

          MapView mapView = (MapView) findViewById(R.id.mapview);
          mapView.setBuiltInZoomControls(true);
          mapView.setSatellite(false);
          mapView.setTraffic(false);

          mapOverlays = mapView.getOverlays();

          new ProgressTask(this).execute();
    }

    public void addToMap(ItemizedOverlay<OverlayItem> itemizedOverlay){
         mapOverlays.add(itemizedOverlay);
    }
}

和 AsyncTask 类

public class ProgressTask extends AsyncTask<String, Void, Boolean> {

    private ProgressDialog dialog;
    private GoogleMapsActivity activity;

    public ProgressTask(GoogleMapsActivity activity) {
        this.activity = activity;
        dialog = new ProgressDialog(activity);
    }

    @Override
    protected void onPreExecute() {
        dialog.setTitle("Loading Pictures");
        dialog.setMessage("Loading...");
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setCancelable(false);
        dialog.show();
    }

    @Override
    protected void onPostExecute(final Boolean sucess) {
        if (dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    @Override
    protected Boolean doInBackground(String... args) {
        try {

            ArrayList<String> list = getImageLocations();

            String filePath;
            String title;

            LayerDrawable drawable;
            ImageItemizedOverlay itemizedOverlay;

            GeoPoint point;
            OverlayItem overlayItem;

            int count = 0;

            if (list.size() > 0) {
                dialog.setMax(list.size());

                for (String s : list) {
                    filePath = s;
                    point = getImageGeoPoint(s);

                    if (point != null) {
                        title = s.substring(filePath.lastIndexOf("/") + 1);

                        drawable = createLayerDrawable(filePath);

                        itemizedOverlay = new ImageItemizedOverlay(drawable, activity, filePath);

                        overlayItem = new OverlayItem(point, title, null);

                        itemizedOverlay.addOverlay(overlayItem);
                        activity.addToMap(itemizedOverlay);
                    } 
                    dialog.incrementProgressBy(1);
                }

            } else {
                if(dialog.isShowing())
                    dialog.dismiss();

                CharSequence text = "No pictures with geolocations stored at DCIM/Camera.";
                Toast toast = Toast.makeText(activity, text, Toast.LENGTH_LONG);
                toast.show();
            }
            CharSequence text = count + " pictures didn't have a geolocation.";
            Toast toast = Toast.makeText(activity, text, Toast.LENGTH_LONG);
            toast.show();
            return true;
        } catch (Exception e) {
            Log.e("tag", "error: " + e.toString(), e);
            return false;
        }
    }

    private LayerDrawable createLayerDrawable(String filePath) {

        Bitmap bitmap = BitmapFactory.decodeFile(filePath);
        Bitmap thumbnail = ThumbnailUtils.extractThumbnail(bitmap, 50, 50);

        Drawable picture = new BitmapDrawable(thumbnail);

        Drawable pin = activity.getResources()
                .getDrawable(R.drawable.backgroundpin);

        InsetDrawable inset = new InsetDrawable(picture, 11, 15, 11, 23);
        Drawable[] list = { pin, inset };

        LayerDrawable layer = new LayerDrawable(list);

        return layer;
    }

    private static GeoPoint getImageGeoPoint(String filename) {
        GeoPoint gp = null;
        float[] latlong = new float[2];

        try {
            ExifInterface tag = new ExifInterface(filename);

            if (tag.getLatLong(latlong)) {
                gp = new GeoPoint((int) (latlong[0] * 1E6),
                        (int) (latlong[1] * 1E6));
            } else {
                // TODO error!
            }

        } catch (FileNotFoundException fnf) {
            // TODO error!
            fnf.printStackTrace();
        } catch (IOException io) {
            // TODO error!
            io.printStackTrace();
        }

        return gp;
    }

    private static ArrayList<String> getImageLocations() {
        File directory = new File("mnt/sdcard/DCIM/Camera");

        FilenameFilter imageFilter = new FilenameFilter() {
            public boolean accept(File dir, String name) {
                String lowercaseName = name.toLowerCase();
                if (lowercaseName.endsWith(".jpg")) {
                    return true;
                } else {
                    return false;
                }
            }
        };

        File[] files = directory.listFiles(imageFilter);
        ArrayList<String> paths = new ArrayList<String>();

        if (files != null && files.length > 0)
            for (File f : files) {
                paths.add(f.getAbsolutePath());
            }

        return paths;
    }

}
4

1 回答 1

1

如果您需要运行昂贵的代码,它应该在后台线程上执行,以防止您的 UI 锁定。为了显示与此过程相关的一些 UI 元素,Android 为此提供了AsyncTask该类。

要在您的实现中显示 a ProgressDialog,实现并创建并显示对话框,然后在 中,将其关闭。onPreExecute()AsyncTaskonPostExecute()

于 2012-04-15T20:11:01.023 回答