cGIS for Android 自定义地图比例尺

来源:互联网 发布:iphone搞怪视频软件 编辑:程序博客网 时间:2024/05/21 00:53

 在我们进行地图相关开发时候,避免不了要绘制比例尺。在百度,高德的地图API里都提供了比例尺控件,但是ArcGIS for Android里并没有提供。不过没关系,我们可以自己绘制一个比例尺来。


      在绘制比例尺前,我们先了解几个概念:


  1. PPI,Pixels Per Inch的所写,表示的是每英寸所拥有的像素数目;
  2. PX,像素,表示图像中的一个最小单位;
  3. DPI,Dots Per Inch,每英寸点数,即图像密度;
  4. .9.PNG,Android开发里面的一种特殊的图片,这种格式的图片通过ADT自带的编辑工具生成,使用九宫格切分的方法,使图片支持在Android环境下的自适应展示。即这种类型图片在Android里无论怎样拉伸缩小都不失真。


      其中PPI和DPI在实际生活中的定义是不太一样的,而在Android里,他们的含义却是相似的。单独把DPI拿出来主要是Android里有个方法可以分别获取到屏幕X轴和Y轴的像素密度。


      .9.PNG格式的图片不失真,正好适合我们做来做比例尺图片。


       好了,我们好绘制一个比例尺,需要做些什么呢?


       首先,我们得知道当前地图比例,这个参数可以通过MapView.getScale来获取;


       其次,我们要根据当前地图比例绘制一个比例尺。绘制的方案有两种,一个是固定尺子长度,根据当前地图比例更换比例尺的比,比如1:2000,1:3000等;另一种是固定一些比例单位,比如1:50000后就是1:20000,然后比例尺的长度根据实际长度会做一定伸缩。这里我采用第二种,因为第一种根据实际比例往往比例尺的比值不是整数,并且在较大比例时候显示位数较长,四舍五入又会失去精度。


      最后,是监听地图放大缩小事件,并作出响应改变比例尺。


      方法就是这么简单,那就实际开动吧。


第一步,实例化地图,加载图层:

[java] view plain copy
  1. mMapView=(MapView)findViewById(R.id.mapview);  
  2. mMapScaleView=(MapScaleView)findViewById(R.id.scaleView);  
  3. String path= StorageUtil.getSDCardRootPath(getApplicationContext());  
  4. ArcGISLocalTiledLayer layer=new ArcGISLocalTiledLayer(path);  
  5. mMapView.addLayer(layer,0);  
  6. mMapScaleView.setMapView(mMapView);  
  7. ArcGISRuntime.setClientId(System.clint);//设置许可  
  8. mMapView.setMapBackground(0xFAFAFA0xffffff0.0f, 0.0f);//地图背景  


第二步,自定义View,绘制比例尺:

      

      初始化自定义View:

[java] view plain copy
  1. public MapScaleView(Context context) {  
  2.     this(context, null);  
  3.     this.context=context;  
  4.     this.initVariables();  
  5. }  
  6.   
  7. public MapScaleView(Context context, AttributeSet attrs) {  
  8.     this(context, attrs, 0);  
  9.     this.context=context;  
  10.     this.initVariables();  
  11. }  
  12.   
  13. public MapScaleView(Context context, AttributeSet attrs, int defStyle) {  
  14.     super(context, attrs, defStyle);  
  15.     this.context=context;  
  16.     this.initVariables();  
  17. }  


      初始化自定义View的参数:

[java] view plain copy
  1. private void initVariables(){  
  2.     scaleWidth=104;//  
  3.     scaleHeight=8;//比比例尺宽度例尺高度  
  4.     textColor= Color.BLACK;//比例尺字体颜色  
  5.     text="20公里";//比例尺文本  
  6.     textSize=18;//比例尺宽度  
  7.     scaleSpaceText=8;//比例尺文本与图形的间隔高度  
  8.     mPaint = new Paint();//画笔  
  9. }  


      重写onMearsure()方法:

[java] view plain copy
  1. @Override  
  2. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  3.     super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  4.     int widthSize = getWidthSize(widthMeasureSpec);  
  5.     int heightSize = getHeightSize(heightMeasureSpec);  
  6.     setMeasuredDimension(widthSize, heightSize);  
  7. }  


      获取自定义View的宽和高:

[java] view plain copy
  1. **  
  2.  * 测量ScaleView的宽度  
  3.  * @param widthMeasureSpec  
  4.  * @return  
  5.  */  
  6. private int getWidthSize(int widthMeasureSpec){  
  7.     return MeasureSpec.getSize(widthMeasureSpec);  
  8. }  
  9.   
  10. /** 
  11.  * 测量ScaleView的高度 
  12.  * @param heightMeasureSpec 
  13.  * @return 
  14.  */  
  15. private int getHeightSize(int heightMeasureSpec){  
  16.     int mode = MeasureSpec.getMode(heightMeasureSpec);  
  17.     int height = 0;  
  18.     switch (mode) {  
  19.         case MeasureSpec.AT_MOST:  
  20.             height = textSize + scaleSpaceText + scaleHeight;  
  21.             break;  
  22.         case MeasureSpec.EXACTLY:{  
  23.             height = MeasureSpec.getSize(heightMeasureSpec);  
  24.             break;  
  25.         }  
  26.         case MeasureSpec.UNSPECIFIED:{  
  27.             height = Math.max(textSize + scaleSpaceText + scaleHeight, MeasureSpec.getSize(heightMeasureSpec));  
  28.             break;  
  29.         }  
  30.     }  
  31.   
  32.     return height;  
  33. }  


      重写onDraw()方法,绘制比例尺:

[java] view plain copy
  1. **  
  2.  * 绘制上面的文字和下面的比例尺,因为比例尺是.9.png,我们需要利用drawNinepath方法绘制比例尺  
  3.  */  
  4. @SuppressLint("DrawAllocation")  
  5. @Override  
  6. protected void onDraw(Canvas canvas) {  
  7.     super.onDraw(canvas);  
  8.     int width = scaleWidth ;  
  9.     mPaint.setColor(textColor);  
  10.     mPaint.setAntiAlias(true);  
  11.     mPaint.setTextSize(textSize);  
  12.     mPaint.setTypeface(Typeface.DEFAULT_BOLD);  
  13.     float textWidth = mPaint.measureText(text);  
  14.     canvas.drawText(text, (width - textWidth) / 2, textSize, mPaint);  
  15.     Rect scaleRect = new Rect(0, textSize + scaleSpaceText, width, textSize + scaleSpaceText + scaleHeight);  
  16.     drawNinepath(canvas, R.drawable.icon_scale, scaleRect);  
  17. }  


      绘制.9.PNG图片:

[java] view plain copy
  1. private void drawNinepath(Canvas canvas, int resId, Rect rect){  
  2.     Bitmap bmp= BitmapFactory.decodeResource(getResources(), resId);  
  3.     NinePatch patch = new NinePatch(bmp, bmp.getNinePatchChunk(), null);  

  1.     patch.draw(canvas, rect);  






宁波整容医院 http://www.lyxcl.org/