如何在绘制主视图后显示ProgressDialog。 Android的

时间:2012-04-15 19:57:45

标签: android progressdialog oncreate

您的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;
    }

}

1 个答案:

答案 0 :(得分:1)

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

要显示ProgressDialog,在onPreExecute()实施中实施AsyncTask并创建并显示对话框,然后在onPostExecute()中将其解除。