Android Open Source - arcgis-runtime-samples-android Dynamic Layer Renderer Activity






From Project

Back to project page arcgis-runtime-samples-android.

License

The source code is released under:

Apache License

If you think the Android project arcgis-runtime-samples-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/* Copyright 2014 ESRI
 *//from w w w .  jav a  2s  .c  o  m
 * All rights reserved under the copyright laws of the United States
 * and applicable international laws, treaties, and conventions.
 *
 * You may freely redistribute and use this sample code, with or
 * without modification, provided you include the original copyright
 * notice and use restrictions.
 *
 * See the sample code usage restrictions document for further information.
 *
 */

package com.arcgis.android.samples.dynamiclayer;

import java.util.List;
import java.util.Map;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISDynamicMapServiceLayer;
import com.esri.android.map.event.OnStatusChangedListener;
import com.esri.core.map.DrawingInfo;
import com.esri.core.map.DynamicLayerInfo;
import com.esri.core.renderer.AlgorithmicColorRamp;
import com.esri.core.renderer.ClassBreaksDefinition;
import com.esri.core.renderer.ClassBreaksDefinition.ClassificationMethod;
import com.esri.core.renderer.ClassBreaksRenderer;
import com.esri.core.renderer.ColorRamp;
import com.esri.core.renderer.NormalizationType;
import com.esri.core.renderer.RampDefinition;
import com.esri.core.tasks.ags.GenerateRendererTask;
import com.esri.core.tasks.ags.GenerateRendererTaskParameters;

/**
 * This app uses the DynamicLayerMapService and the DynamicLayer to demonstrate
 * the Class break rendering. On clicking the field from the spinner it will
 * display the rendered image classifying the attribute that you have selected.
 * 
 * Please be aware that the Dynamic Layer service is not guaranteed to be
 * running.
 * 
 */
public class DynamicLayerRendererActivity extends Activity {

  MapView mMapView = null;
  ProgressDialog progress = null;
  ActionBar action = null;
  ClassBreaksRenderer render = null;
  ArcGISDynamicMapServiceLayer dynamicLayer = null;
  
  // query menu items
  MenuItem mPop2007 = null;
  MenuItem mPop2000 = null;
  MenuItem mPop00_Sqmi = null;
  MenuItem mPop07_Sqmi = null;
  MenuItem mWhite = null;
  MenuItem mBlack = null;
  MenuItem mHispanic = null;
  MenuItem mAmeri_Es = null;

  final int layerid = 2;

  private static final int TRANSPARENCY = 10;
  private String classificationField = "POP2000";

  // Dialog to check progress for the service task
  static ProgressDialog dialog = null;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mMapView = (MapView) findViewById(R.id.map);
    // set layer to display
    int[] layers = new int[1];
    layers[0] = 2;

    dynamicLayer = new ArcGISDynamicMapServiceLayer(this.getResources()
        .getString(R.string.server_url));
    mMapView.addLayer(dynamicLayer);

     dynamicLayer.setOpacity((float) 0.5);

    action = getActionBar();

    // to check whether the dynamic map is loaded successfully or not
    mMapView.setOnStatusChangedListener(new OnStatusChangedListener() {
      private static final long serialVersionUID = 1L;

      public void onStatusChanged(final Object source, final STATUS status) {

        if (STATUS.LAYER_LOADED == status) {
          if (source instanceof ArcGISDynamicMapServiceLayer) {
            // ArcGISFeatureLayer loaded successfully
            System.out.println("Loaded Successfully " + source);

            // check whether the server offer the dynamic layer
            // service
            if (dynamicLayer.isDynamicLayersSupported() == true) {
              System.out
                  .println("supports dynamic layer service");

            } else {
              System.out
                  .println("doesnt support dynamic layer service");
            }
          }
        }
      }
    });

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // inflate the menu; this adds items to the action bar
    getMenuInflater().inflate(R.menu.action, menu);
    
    // get the query menu items
    mPop2007 = menu.getItem(0);
    mPop2000 = menu.getItem(1);
    mPop00_Sqmi = menu.getItem(2);
    mPop07_Sqmi = menu.getItem(3);
    mWhite = menu.getItem(4);
    mBlack = menu.getItem(5);
    mHispanic = menu.getItem(6);
    mAmeri_Es = menu.getItem(7);
    
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    super.onOptionsItemSelected(item);

    switch (item.getItemId()) {

    case R.id.pop2007:
      classificationField = "POP2007";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mPop2007.setChecked(true);
      break;

    case R.id.pop2000:
      classificationField = "POP2000";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mPop2000.setChecked(true);
      break;

    case R.id.pop07sqmi:
      classificationField = "POP07_SQMI";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mPop07_Sqmi.setChecked(true);
      break;

    case R.id.pop00sqmi:
      classificationField = "POP00_SQMI";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mPop00_Sqmi.setChecked(true);
      break;

    case R.id.white:
      classificationField = "WHITE";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mWhite.setChecked(true);
      break;

    case R.id.black:
      classificationField = "BLACK";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mBlack.setChecked(true);
      break;

    case R.id.hispanic:
      classificationField = "HISPANIC";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mHispanic.setChecked(true);
      break;
      
    case R.id.ameries:
      classificationField = "AMERI_ES";
      // defining the class break renderer
      getClassBreaksRenderer(classificationField,
          ClassificationMethod.NaturalBreaks, 5,
          NormalizationType.None);
      mAmeri_Es.setChecked(true);
    }
    return true;
  }

  /**
   * 
   * @param classificationMethod
   * @param numBreaks
   * @param normalizationMethod
   * @return classbreakrenderer
   */
  private void getClassBreaksRenderer(String classField,
      ClassificationMethod classificationMethod, int numBreaks,
      NormalizationType normalizationMethod) {

    ClassBreaksDefinition classificationDef = null;

    classificationDef = ClassBreaksDefinition.createByClassificationMethod(
        classField, classificationMethod, numBreaks);
    // define start and end colors for ramp
    int[] colors = { Color.YELLOW, Color.RED };
    ColorRamp colorRamp = new AlgorithmicColorRamp(colors[0], colors[1],
        RampDefinition.Algorithm.HSV);
    classificationDef.setColorRamp(colorRamp);
    // execute the task
    RenderTask renderTask = new RenderTask();
    renderTask.execute(classificationDef);

  }

  /**
   * Called when the activity is destroyed
   */
  protected void onDestroy() {
    super.onDestroy();
  }

  /**
   * Called when the activity is Paused
   */
  protected void onPause() {
    super.onPause();
    mMapView.pause();
  }

  /**
   * Called when the activity is Resumed
   */
  protected void onResume() {
    super.onResume();
    mMapView.unpause();
  }

  private class RenderTask extends
      AsyncTask<ClassBreaksDefinition, Void, ClassBreaksRenderer> {

    protected void onPreExecute() {
      dialog = ProgressDialog.show(DynamicLayerRendererActivity.this, "",
          "Generating Renderer");
    }

    @Override
    protected ClassBreaksRenderer doInBackground(
        ClassBreaksDefinition... params) {

      // generate a class breaks renderer using our class breaks
      // definition
      GenerateRendererTask generateRenderTask = new GenerateRendererTask(
          dynamicLayer.getUrl() + "/" + layerid);
      GenerateRendererTaskParameters taskParams = new GenerateRendererTaskParameters(
          params[0]);

      ClassBreaksRenderer resultRenderer = null;

      try {
        resultRenderer = (ClassBreaksRenderer) generateRenderTask
            .execute(taskParams);
      } catch (Exception e) {
        e.printStackTrace();
      }
      return resultRenderer;

    }

    protected void onPostExecute(ClassBreaksRenderer renderer) {
      dialog.dismiss();

      // Get the layerInfo from the dynamic layer and set the drawing info
      List<DynamicLayerInfo> layerInfos = dynamicLayer.getDynamicLayerInfos();

      if (layerInfos != null) {
        // found dynamic layers
        Log.d("TAG", "dynamic layer infos found");
        for (DynamicLayerInfo dynamicLayerInfo : layerInfos) {
          if (dynamicLayerInfo.getId() == layerid) {
            // Create new drawing info from our renderer, with set
            // transparency
            Map<Integer, DrawingInfo> dInfo = dynamicLayer
                .getDrawingOptions();
            dInfo.put(Integer.valueOf(layerid), new DrawingInfo(
                renderer, TRANSPARENCY));
          }
        }
      }

      // Refresh the layer
      dynamicLayer.refresh();

    }

  }

}




Java Source Code List

com.arcgis.android.samples.ExportTileCacheTask.ExportTileCacheTaskSampleActivity.java
com.arcgis.android.samples.cloudportal.featureservicetablequery.MainActivity.java
com.arcgis.android.samples.cloudportal.querycloudfeatureservice.MainActivity.java
com.arcgis.android.samples.dynamiclayer.DynamicLayerRendererActivity.java
com.arcgis.android.samples.geometrysample.BufferFragment.java
com.arcgis.android.samples.geometrysample.GeometrySampleActivity.java
com.arcgis.android.samples.geometrysample.GeometryUtil.java
com.arcgis.android.samples.geometrysample.SampleListFragment.java
com.arcgis.android.samples.geometrysample.SpatialRelationshipsFragment.java
com.arcgis.android.samples.geometrysample.UnionDifferenceFragment.java
com.arcgis.android.samples.localdata.localrasterdata.EditTextUtils.java
com.arcgis.android.samples.localdata.localrasterdata.FileBrowserFragment.java
com.arcgis.android.samples.localdata.localrasterdata.HillshadeRendererParametersFragment.java
com.arcgis.android.samples.localdata.localrasterdata.MainActivity.java
com.arcgis.android.samples.localdata.localrasterdata.OnDialogDismissListener.java
com.arcgis.android.samples.localdata.localrasterdata.RendererType.java
com.arcgis.android.samples.localdata.localrasterdata.StretchParametersFragment.java
com.arcgis.android.samples.maps.basemaps.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.BasemapListFragment.java
com.arcgis.android.samples.maps.fragmentmanagement.MainActivity.java
com.arcgis.android.samples.maps.fragmentmanagement.MapFragment.java
com.arcgis.android.samples.maps.helloworld.MainActivity.java
com.arcgis.android.samples.maps.maplegend.LegendDialogFragment.java
com.arcgis.android.samples.maps.maplegend.MainActivity.java
com.arcgis.android.samples.maps.maprotation.Compass.java
com.arcgis.android.samples.maps.maprotation.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MainActivity.java
com.arcgis.android.samples.maps.switchmaps.MapFragment.java
com.arcgis.android.samples.milsym2525c.MainActivity.java
com.arcgis.android.samples.milsym2525c.Mil2525cMessageParser.java
com.arcgis.android.samples.oauth2sample.MapFragment.java
com.arcgis.android.samples.oauth2sample.OAuth2Sample.java
com.arcgis.android.samples.oauth2sample.UserContentActivity.java
com.arcgis.android.samples.oauth2sample.UserContentArrayAdapter.java
com.arcgis.android.samples.oauth2sample.UserContentFragment.java
com.arcgis.android.samples.oauth2sample.UserWebmaps.java
com.arcgis.android.samples.search.placesearch.MainActivity.java
com.esri.android.sample.closestfacilities.MainActivity.java
com.esri.android.samples.mbtiles.LocalMBTiles.java
com.esri.android.samples.mbtiles.MBTilesLayer.java
com.esri.android.samples.mgrsgrid.LocateMGRSActivity.java
com.esri.arcgis.android.sample.runtimegeodb.CreateRuntimeGeodatabaseActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapActivity.java
com.esri.arcgis.android.sample.simplemap.SimpleMapFragment.java
com.esri.arcgis.android.sample.simplemap.SingleFragmentActivity.java
com.esri.arcgis.android.samples.GeoJSONEarthquakeMap.GeoJSONEarthquakeMapActivity.java
com.esri.arcgis.android.samples.PopupUICustomization.LayerQueryTask.java
com.esri.arcgis.android.samples.PopupUICustomization.MyAttachmentsView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyEditAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyMediaView.java
com.esri.arcgis.android.samples.PopupUICustomization.MyReadOnlyAttributesAdapter.java
com.esri.arcgis.android.samples.PopupUICustomization.MyTitleView.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupFragment.java
com.esri.arcgis.android.samples.PopupUICustomization.PopupUICustomizationActivity.java
com.esri.arcgis.android.samples.addcsv2graphic.AddCSVActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeEditorActivity.java
com.esri.arcgis.android.samples.attributeeditor.AttributeItem.java
com.esri.arcgis.android.samples.attributeeditor.AttributeListAdapter.java
com.esri.arcgis.android.samples.attributeeditor.FeatureLayerUtils.java
com.esri.arcgis.android.samples.basiclicense.MainActivity.java
com.esri.arcgis.android.samples.basiclicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.classbreaksrenderer.ClassBreaksRendererActivity.java
com.esri.arcgis.android.samples.featuredusergroup.FeaturedGroupsActivity.java
com.esri.arcgis.android.samples.featuredusergroup.GroupsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.ItemsFragment.java
com.esri.arcgis.android.samples.featuredusergroup.MapActivity.java
com.esri.arcgis.android.samples.geometryeditor.ConfirmDiscardDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.EditFailedDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeData.java
com.esri.arcgis.android.samples.geometryeditor.FeatureTypeDialogFragment.java
com.esri.arcgis.android.samples.geometryeditor.GeometryEditorActivity.java
com.esri.arcgis.android.samples.identifytask.Identify.java
com.esri.arcgis.android.samples.measure.MainActivity.java
com.esri.arcgis.android.samples.nearby.Nearby.java
com.esri.arcgis.android.samples.offlineeditor.GDBUtil.java
com.esri.arcgis.android.samples.offlineeditor.OfflineActions.java
com.esri.arcgis.android.samples.offlineeditor.OfflineEditorActivity.java
com.esri.arcgis.android.samples.offlineeditor.TemplatePicker.java
com.esri.arcgis.android.samples.offlineroutingandgeocoding.RoutingAndGeocoding.java
com.esri.arcgis.android.samples.popupinwebmapforediting.PopupInWebmapForEditing.java
com.esri.arcgis.android.samples.popupinwebmapforviewing.PopupInWebmapForViewing.java
com.esri.arcgis.android.samples.querytask.MainActivity.java
com.esri.arcgis.android.samples.routing.MyAdapter.java
com.esri.arcgis.android.samples.routing.RoutingDialogFragment.java
com.esri.arcgis.android.samples.routing.RoutingListFragment.java
com.esri.arcgis.android.samples.routing.RoutingSample.java
com.esri.arcgis.android.samples.servicearea.EditFragment.java
com.esri.arcgis.android.samples.servicearea.ServiceAreaSample.java
com.esri.arcgis.android.samples.standardlicense.MainActivity.java
com.esri.arcgis.android.samples.standardlicense.MessageDialogFragment.java
com.esri.arcgis.android.samples.standardlicenseoffline.MainActivity.java
com.esri.arcgis.android.samples.standardlicenseoffline.MessageDialogFragment.java
com.esri.arcgis.android.samples.uniquevaluerenderer.UniqueValueRendererSampleActivity.java
com.esri.arcgis.android.samples.viewshed.Viewshed.java
com.esri.arcgis.android.samples.wmslayer.MainActivity.java
com.esri.arcgis.samples.offlineanalysis.MainActivity.java