Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/186.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 添加许多覆盖后,“地图视图”平移和缩放速度较慢_Android_Performance_Overlay_Android Mapview_Panning - Fatal编程技术网

Android 添加许多覆盖后,“地图视图”平移和缩放速度较慢

Android 添加许多覆盖后,“地图视图”平移和缩放速度较慢,android,performance,overlay,android-mapview,panning,Android,Performance,Overlay,Android Mapview,Panning,所以我花了很多时间试图找出如何加快速度,但我现在没有主意了。我有一个类,mapPopup,在这个类中,MapView显示在整个屏幕上。在mapPopup中有一个地质点数组的数组,我想在数组的第二维度中的每个地质点之间画一条线。我已经使用一个自定义类mappoverlay完成了这项任务,该类扩展了Overlay,但我遇到的问题是,一旦绘制了所有地图覆盖,地图的缩放或平移速度就非常慢。一旦将所有覆盖图添加到地图中,通常会有2000多个覆盖图,但它们都非常小 我认为,如果覆盖层较少,地图的绘制速度会更

所以我花了很多时间试图找出如何加快速度,但我现在没有主意了。我有一个类,
mapPopup
,在这个类中,
MapView
显示在整个屏幕上。在
mapPopup
中有一个
地质点数组的数组,我想在数组的第二维度中的每个地质点之间画一条线。我已经使用一个自定义类
mappoverlay
完成了这项任务,该类扩展了
Overlay
,但我遇到的问题是,一旦绘制了所有地图覆盖,地图的缩放或平移速度就非常慢。一旦将所有覆盖图添加到地图中,通常会有2000多个覆盖图,但它们都非常小

我认为,如果覆盖层较少,地图的绘制速度会更快,因此我将所有线条绘制为三个独立的覆盖层,而不是每一条线条单独的覆盖层。这实际上导致了地图平移和缩放的速度变慢,所以我又回到了许多小的叠加

如果能提供一些信息丰富且易于理解的方法描述,我将不胜感激,因为我可以使用这些方法来加快地图的绘制速度。潜在方法的伪代码或实代码也会帮助我更好地理解它。我的代码贴在下面。再次,请注意,我的覆盖图和地图显示正确;我只想有一个方法,将允许更快的平移和缩放

mapOverlay类

public class mapOverlay extends Overlay {

private Projection projection;
private GeoPoint gp1;
private GeoPoint gp2;
private int color;

public mapOverlay(int color, MapView map, GeoPoint geo1, GeoPoint geo2) {
    // super();
    this.projection = map.getProjection();
    this.gp1 = geo1;
    this.gp2 = geo2;
    this.color = color;
}

public void draw(Canvas canvas, MapView mapv, boolean shadow) {
    super.draw(canvas, mapv, false);

    Paint mPaint = new Paint();
    mPaint.setDither(true);
    mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(4);
    mPaint.setColor(this.color);

    Point p1 = new Point();
    Point p2 = new Point();
    Path path1 = new Path();

    projection.toPixels(gp1, p1);
    projection.toPixels(gp2, p2);
    path1.moveTo(p1.x, p1.y);
    path1.lineTo(p2.x, p2.y);
    canvas.drawPath(path1, mPaint);

}

}
mapPopup类

public class mapPopup extends MapActivity {

public String[] trailNames;
public String tableName = "";
public int numTrails = 0;
public static GeoPoint[][] geoPoints;
public int[] colors = new int[] { Color.WHITE, Color.BLUE, Color.CYAN,
        Color.RED, Color.YELLOW, Color.MAGENTA, Color.GRAY, Color.LTGRAY };
public int[] newColors;
// public Bitmap b;
public GeoPoint firstP;

public void loadMapData(Bitmap b, MapView map, int[] colors,
        GeoPoint[][] GPAA, int ZoomLevel) {
    // GPAA holds an array of an array of GeoPoint
    Log.i("DEBUG", "starting loadMapDataTask");

    map.setSatellite(true);
    MapController mc = map.getController();

    mapOverlay[][] mapOverlay = new mapOverlay[GPAA.length][];
    Log.i("DEBUG", "length of GPAA is: " + GPAA.length);

    // i cycles through the first dimension of GPAA
    for (int i = 0; i < GPAA.length; i++) {
        GeoPoint[] geoPoints = GPAA[i];
        int length = geoPoints.length - 1;
        mapOverlay[i] = new mapOverlay[length]; //
        int pointCount = 0;
        // z cycles through the second dimension of GPAA
        for (int z = 0; z < length; z++) {
            mapOverlay[i][z] = new mapOverlay(colors[i], map,
                    geoPoints[pointCount], geoPoints[pointCount + 1]);
            pointCount++;
        }
    }

    // Actually adds overlays to map
    List<Overlay> mapOverlays = map.getOverlays();
    for (int i = 0; i < mapOverlay.length; i++) {
        int length = mapOverlay[i].length;
        Log.i("DEBUG", "Adding map overlays for trail: " + i);
        Log.i("DEBUG", "Length of mapOverlay[i] is: " + length);
        for (int z = 0; z < length; z++) {
            mapOverlays.add(mapOverlay[i][z]);
        }
    }

    mc.animateTo(GPAA[0][0]);
    mc.setZoom(ZoomLevel);
    Rect r = new Rect();
    map.getDrawingRect(r);
    map.invalidate(r);
}

public static class runBGLoad extends
        AsyncTask<bgLoadParam, Integer, GeoPoint[][]> {

    public GeoPoint[][] geoPoints;

    protected GeoPoint[] getGPa(Context context, String name, int ID) {
        File file = context.getFileStreamPath(name);
        if (file.exists() == false) {
            Log.i("DEBUG", "Creating file");
            InputStream is;
            FileOutputStream fos;

            try {
                Log.i("DEBUG", "id is " + ID);
                is = context.getResources().openRawResource(ID);
                byte[] buffer = new byte[is.available()];
                is.read(buffer);
                fos = context.openFileOutput(name, Context.MODE_PRIVATE);
                fos.write(buffer);
                fos.close();
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        } else {
            Log.i("DEBUG", "File already exists");
        }

        // Log.i("DEBUG", "starting to get geopoints");

        List<Location> gpsPoints = XMLParser.getPoints(file);

        int i = 0;
        int index = 0;

        GeoPoint[] geoPoints = new GeoPoint[gpsPoints.size()];

        // makes list of gpsPoints into GeoPoint[]
        ListIterator<Location> it = gpsPoints.listIterator();

        while (it.hasNext()) {
            index = it.nextIndex();
            Location loc = gpsPoints.get(index);
            geoPoints[i] = new GeoPoint((int) (loc.getLatitude() * 1E6),
                    (int) (loc.getLongitude() * 1E6));
            it.next();
            i++;
        }

        return geoPoints;
    }

    @Override
    protected GeoPoint[][] doInBackground(bgLoadParam... params) {

        Context context = params[0].getContext();
        int tNLength = params[0].getTNames().length;
        geoPoints = new GeoPoint[tNLength][];

        for (int i = 0; i < params[0].getTNames().length; i++) {
            String modName = params[0].getTNames()[i].toLowerCase()
                    .replace(' ', '_');
            int identifier = context.getResources().getIdentifier(modName,
                    "raw", context.getPackageName());
            geoPoints[i] = getGPa(params[0].getContext(), modName
                    + "_mapfile", identifier);
        }
        Log.i("DEBUG", "TEST");
        mapPopup.geoPoints = geoPoints;
        Log.i("DEBUG", "TEST2");
        return geoPoints;
    }

    @Override
    protected void onPostExecute(GeoPoint[][] result) {
        Log.i("DEBUG", "The points are loaded.");
        mapPopup.geoPoints = result;
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    Intent intent = getIntent();
    String[] extras = intent.getStringArrayExtra("strings");

    tableName = extras[1];
    numTrails = Integer.parseInt(extras[2]);
    trailNames = intent.getStringArrayExtra("trailNamesA");

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

    newColors = new int[numTrails];
    for (int i = 0; i < numTrails; i++) {
        newColors[i] = colors[i];
    }
    ViewGroup layout = (ViewGroup) findViewById(R.id.map_popup);
    TextView[] tVs = new TextView[numTrails];

    for (int i = 0; i < numTrails; i++) {
        LayoutParams params = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        tVs[i] = new TextView(getApplicationContext());
        tVs[i].setText(trailNames[i]);
        tVs[i].setId(i + 700);
        tVs[i].setTextColor(colors[i]);
        tVs[i].setBackgroundColor(Color.BLACK);

        if (i > 0) {
            params.addRule(RelativeLayout.BELOW, (699 + i));
        }

        layout.addView(tVs[i], params);
    }

}

@Override
public void onWindowFocusChanged(boolean hasFocus) {
    super.onWindowFocusChanged(hasFocus);

    MapView map = (MapView) findViewById(R.id.popupMV);
    Bitmap b = Bitmap.createBitmap(map.getWidth(), map.getHeight(),
            Bitmap.Config.RGB_565);

    try {
        trailsActivity.mapPreLoad.get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    loadMapData(b, map, newColors, geoPoints, 17);
}

@Override
protected boolean isRouteDisplayed() {
    // TODO Auto-generated method stub
    return false;
}

}    
公共类mapPopup扩展了MapActivity{
公共字符串[]trailNames;
公共字符串tableName=“”;
公共int numTrails=0;
公共静态地质点[][]地质点;
public int[]colors=new int[]{Color.WHITE,Color.BLUE,Color.CYAN,
Color.RED、Color.YELLOW、Color.洋红、Color.GRAY、Color.LTGRAY};
公共int[]新颜色;
//公共图书馆b;
公共地质点firstP;
public void loadMapData(位图b、MapView贴图、int[]颜色、,
地质点[][]GPAA,int ZoomLevel){
//GPAA保存一个地质点数组的数组
Log.i(“调试”,“启动loadMapDataTask”);
地图.固定卫星(真);
MapController mc=map.getController();
mapOverlay[]]mapOverlay=新的mapOverlay[GPAA.length][];
Log.i(“调试”,“GPAA的长度为:“+GPAA.length”);
//我循环通过GPAA的第一维度
对于(int i=0;ipublic override bool OnTouchEvent (MotionEvent e, Android.GoogleMaps.MapView mapView)
{
    if (e.Action == MotionEventActions.Down)
        _mustDraw = false;
    else if (e.Action == MotionEventActions.Up)
        _mustDraw = true;
    return base.OnTouchEvent (e, mapView);
}

public override void Draw (Android.Graphics.Canvas canvas, Android.GoogleMaps.MapView mapView, bool shadow)
{
    if (shadow || !_mustDraw)
        return;
    // ...
}