如何使路径描绘更有效

如何使路径描绘更有效

本文介绍了如何使路径描绘更有效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在code我用画的路线。当我有1000点,航线严重放缓的UI。也许有人可以提供一个code段或说明如何执行路径描绘更有效地链接?我知道有办法来解决,这是缓存路径位图,但不知道该怎么做。

 公共类PathOverlay扩展覆盖{

私人的GeoPoint的startPoint;
私人的GeoPoint finishPoint;
私人的ArrayList<的GeoPoint> pathPoints;
民营涂料粉刷;
私人路径路径;
私人点pathStartPoint;
私人点pathEndPoint;

私人浮动DX;
私人浮动DY;


公共PathOverlay(GeoPoint对象的startPoint,GeoPoint对象finishPoint,ArrayList的<的GeoPoint> pathPoints,INT的颜色){
    this.startPoint =的startPoint;
    this.finishPoint = finishPoint;
    this.pathPoints = pathPoints;
    this.paint =新的油漆();
    this.paint.setAntiAlias​​(真正的);
    this.paint.setDither(真正的);
    this.paint.setColor(颜色);
    this.paint.setAlpha(150);
    this.paint.setStrokeWidth(4);
    this.paint.setStyle(Paint.Style.STROKE);
}

@覆盖
公共无效画(油画overlayCanvas,图形页面图形页面,布尔影子){
    如果(路径== NULL){
        PATH = getPath(图形页面);
    } 其他 {
        PATH = transformPath(图形页面);
    }
    overlayCanvas.drawPath(路径,油漆);
    super.draw(overlayCanvas,图形页面,阴影);
}

私人路径getPath(图形页面图形页面){
    投影投影= MapView.getProjection()在;
    如果(路径== NULL){
        路径=新路径();
        path.setFillType(FillType.WINDING);
    } 其他 {
        path.rewind();
    }
    点对点=新的点();
    pathStartPoint =新的点();
    pathEndPoint =新的点();

    projection.toPixels(的startPoint,点);
    projection.toPixels(的startPoint,pathStartPoint);
    path.moveTo(point.x,point.y);
    path.addCircle(point.x,point.y,(浮动)2.0,Direction.CCW);
    如果(pathPoints!= NULL){
        的for(int i = 0; I< pathPoints.size();我++){
            projection.toPixels(pathPoints.get(我),点);
            path.lineTo(point.x,point.y);
        }
    }
    projection.toPixels(finishPoint,点);
    projection.toPixels(finishPoint,pathEndPoint);
    path.lineTo(point.x-5,point.y);
    path.addCircle(point.x-5,point.y,(浮动)2.0,Direction.CCW);


    返回路径;
}

私人路径transformPath(图形页面图形页面){
    投影投影= MapView.getProjection()在;

    点sPoint =新的点();
    点ePoint =新的点();
    projection.toPixels(的startPoint,sPoint);
    projection.toPixels(finishPoint,ePoint);

    浮动SX =((浮点)ePoint.x  - (浮点)sPoint.x)/((浮动)pathEndPoint.x  - (浮点)pathStartPoint.x);
    浮SY =((浮点)ePoint.y  - (浮点)sPoint.y)/((浮动)pathEndPoint.y  - (浮点)pathStartPoint.y);

    如果为(sx = 1.0&安培;!&安培;!SY = 1.0){
        Log.i(PathOverlay,调整大小);
        返回getPath(图形页面);
    } 其他 {
        Log.i(PathOverlay,感动);
        字模=新的Matrix();

        DX =(浮点)sPoint.x  - (浮点)pathStartPoint.x;
        DY =(浮点)sPoint.y  - (浮点)pathStartPoint.y;

        matrix.postTranslate(DX,DY);
        pathStartPoint = sPoint;
        pathEndPoint = ePoint;
        path.transform(矩阵);

        返回路径;
    }
}

}
 

解决方案

您可以绘制路径透明位图对象(无论大小,你看装修 - 越大它是,更好的路径内存消耗还更高)的细节。

请确保您有 Bitmap.config.ARGB_8888 创建它的透明度。

一旦你做到了这一点,你就可以使用两个矩形显示在重叠的路径:

  • 在源矩形,以确定哪些路径的一部分是可见
  • 在一个目标矩形,以确定要显示这片上的重叠的帆布的路径。

您将使用 Canvas.drawBitmap(位图位图,矩形SRC,RectF DST,油漆涂​​料)

应该不会太困难,你做最你的 transformPath 方法的重要计算。

补充:

您可以真正做到既拿着画对位图的路径和重绘的实际路径点的组合。使用上述用于当周围的地图的用户移动的技术或放大/缩小,然后重画的路径,当用户放开屏幕

This is the code I'm using to draw route. When i have 1000 of points, route severely slows ui. Maybe someone could provide a code snippet or a link which explains how to do route drawing more efficiently? I know that one way to solve this is caching path to bitmap, but have no idea how to do it.

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);
}

@Override
public 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, point.y, (float) 2.0, 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;
    }
}

}
解决方案

You can draw the path to a transparent Bitmap object (whatever size you see fitting - the bigger it is, the better the detail of the path yet higher memory consumption).

Make sure you create it with Bitmap.config.ARGB_8888 for transparency.

Once you've done this, you'll be using two rectangles to display the path on the Overlay:

  • A source rectangle to determine which part of the path is visible
  • A destination rectangle to determine where you want to display this piece of the path on the Overlay's canvas.

You'll be using Canvas.drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint)

Shouldn't be too difficult, you've done most of the important calculations in your transformPath method.

Added:

You can actually do a combination of both holding a path drawn to a Bitmap and redrawing the actual path points. Use the technique described above for when the user moves around the map or zooms in/out then redraw the path when the user lets go of the screen.

这篇关于如何使路径描绘更有效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 00:31