public class PathOverlay extends Overlay{private GeoPoint startPoint;private GeoPoint finishPoint;private ArrayList<GeoPoint> pathPoints;private Paint paint;private Path path;private Point pathStartPoint;private Point pathEndPoint;private float dx;private float dy;public PathOverlay(GeoPoint startPoint,GeoPoint finishPoint,ArrayList<GeoPoint> pathPoints,int color){ this.startPoint = startPoint; this.finishPoint = finishPoint; this.pathPoints = pathPoints; this.paint = new Paint(); this.paint.setAntiAlias(true); this.paint.setDither(true); this.paint.setcolor(color); this.paint.setAlpha(150); this.paint.setstrokeWIDth(4); this.paint.setStyle(Paint.Style.stroke);}@OverrIDepublic voID draw(Canvas overlayCanvas,MapVIEw mapVIEw,boolean shadow) { if(path == null) { path = getPath(mapVIEw); } else { path = transformPath(mapVIEw); } overlayCanvas.drawPath(path,paint); super.draw(overlayCanvas,mapVIEw,shadow);}private Path getPath(MapVIEw mapVIEw) { Projection projection = mapVIEw.getProjection(); if(path == null) { path = new Path(); path.setFillType(FillType.WINDING); } else { path.rewind(); } Point point = new Point(); pathStartPoint = new Point(); pathEndPoint = new Point(); projection.topixels(startPoint,point); projection.topixels(startPoint,pathStartPoint); path.moveto(point.x,point.y); path.addCircle(point.x,point.y,(float) 2.0,Direction.ccw); if (pathPoints != null) { for(int i=0;i<pathPoints.size();i++) { projection.topixels(pathPoints.get(i),point); path.lineto(point.x,point.y); } } projection.topixels(finishPoint,point); projection.topixels(finishPoint,pathEndPoint); path.lineto(point.x-5,point.y); path.addCircle(point.x-5,Direction.ccw); return path;}private Path transformPath(MapVIEw mapVIEw) { Projection projection = mapVIEw.getProjection(); Point sPoint = new Point(); Point ePoint = new Point(); projection.topixels(startPoint,sPoint); projection.topixels(finishPoint,ePoint); float sx = ((float)ePoint.x - (float)sPoint.x)/((float)pathEndPoint.x - (float)pathStartPoint.x); float sy = ((float)ePoint.y - (float)sPoint.y)/((float)pathEndPoint.y - (float)pathStartPoint.y); if(sx != 1.0 && sy != 1.0) { Log.i("PathOverlay","resized"); return getPath(mapVIEw); } else { Log.i("PathOverlay","moved"); Matrix matrix = new Matrix(); dx = (float)sPoint.x - (float)pathStartPoint.x; dy = (float)sPoint.y - (float)pathStartPoint.y; matrix.postTranslate(dx,dy); pathStartPoint = sPoint; pathEndPoint = ePoint; path.transform(matrix); return path; }}}解决方法 您可以绘制透明Bitmap对象的路径(无论您看到适合的大小 – 它越大,路径细节越好但内存消耗越高).
确保使用Bitmap.config.ARGB_8888创建透明度.
完成此 *** 作后,您将使用两个矩形在Overlay上显示路径:
>源矩形,用于确定路径的哪个部分可见
>目标矩形,用于确定要在Overlay画布上显示此路径的位置.
你将使用Canvas.drawBitmap(Bitmap bitmap,Rect src,RectF dst,Paint paint)
不应该太困难,你已经在transformPath方法中完成了大部分重要的计算.
添加:
实际上,您可以同时执行两者的组合,将路径绘制到位图并重新绘制实际路径点.当用户在地图上移动或放大/缩小时,使用上述技术,然后在用户离开屏幕时重绘路径.
总结以上是内存溢出为你收集整理的android – 如何使路线绘图更有效率?全部内容,希望文章能够帮你解决android – 如何使路线绘图更有效率?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)