2011-01-11 4 views
0

Le code suivant devrait accélérer mon MapView car je gère un grand nombre de routes. Malheureusement, la partie "else" ne dessine rien. Quelqu'un pourrait-il m'aider à comprendre cela? ThxAndroid MapView Bitmap Problème

public boolean draw(Canvas canvas, MapView mapv, boolean shadow, long when) { 

    if ((bmap == null) || (lastLatSpan != mapv.getLatitudeSpan())) { 
     // bitmap is null - so we haven't previously drawn the path, OR 
     // the map has been zoomed in/out, so we're gonna re-draw it anyway 
     // (alternatively, I could have tried scaling the bitmap... might 
     // be worth investigating if that is more efficient) 

     // create a new bitmap, the size of the map view 
     Canvas offscreencanvas = new Canvas(); 
     bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(), 
       Bitmap.Config.ARGB_8888); 
     offscreencanvas.setBitmap(bmap); 

     Path path = null; 

     Projection projection = mapv.getProjection(); 

     // lastLatSpan = mapv.getLatitudeSpan(); 

     for (int j = 0; j < mTourenArray.length; j++) { 
      GeoPoint[] route = GeoPointFactory 
        .createGeoPointArray(mTourenArray[j].getGeoPointArray()); 
      defaultColor = mTourenArray[j].getColorByCat(); 
      path = new Path(); 
      Paint polyPaint = new Paint(); 
      polyPaint.setColor(defaultColor); 

      for (int i = 0; i < route.length - 1; i++) { 
       GeoPoint gp1 = route[i]; 
       GeoPoint gp2 = route[i + 1]; 

       if (shadow == false) { 
        polyPaint.setAntiAlias(true); 
        Point point = new Point(); 
        projection.toPixels(gp1, point); 
        // mode=1&#65306;start 
        if (i == 0) { 
         RectF oval = new RectF(point.x - mRadius, point.y 
           - mRadius, point.x + mRadius, point.y 
           + mRadius); 
         // start point 
         canvas.drawOval(oval, polyPaint); 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 

         path.moveTo(point.x, point.y); 
         path.lineTo(point2.x, point2.y); 
        } 
        /* mode=3&#65306;end */ 
        else if (i == route.length - 2) { 
         /* the last path */ 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 
         path.lineTo(point2.x, point2.y); 

         RectF oval = new RectF(point2.x - mRadius, point2.y 
           - mRadius, point2.x + mRadius, point2.y 
           + mRadius); 
         /* end point */ 
         polyPaint.setAlpha(255); 
         offscreencanvas.drawOval(oval, polyPaint); 
        } 
        // mode=2&#65306;path 
        else if (i < route.length - 2 & i != 0) { 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 
         path.lineTo(point2.x, point2.y); 

        } 

       } 
      } 

      // create an off-screen canvas to prepare new bitmap, and 
      // draw path on to it 
      polyPaint.setStrokeWidth(5); 
      polyPaint.setStyle(Paint.Style.STROKE); 
      polyPaint.setAntiAlias(true); 
      polyPaint 
        .setAlpha(defaultColor == Color.parseColor("#6C8715") ? 220 
          : 120); 

      offscreencanvas.drawPath(path, polyPaint); 

     } // Outer For End 

     // draw the bitmap of the path onto my map view's canvas 
     canvas.drawBitmap(bmap, 0, 0, null); 

     // make a note of where we put the bitmap, so we know how 
     // much we 
     // we need to move it by if the user pans the map 
     mapStartPosition = projection.fromPixels(0, 0); 

    } else { 
     // as we're in onDraw, we think the user has panned/moved the map 
     // if we're in here, the zoom level hasn't changed, and 
     // we've already got a bitmap with a drawing of the route path 

     Projection proj = mapv.getProjection(); 

     // where has the mapview been panned to? 
     Point offsetPt = new Point(); 

     proj.toPixels(mapStartPosition, offsetPt); 
     Paint tmpPaint = new Paint(); 

     // create a new bitmap, the size of the map view 
     // draw the bitmap in the new correct location 
     canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, tmpPaint); 

    } 
    return super.draw(canvas, mapv, shadow, when); 
} 

Répondre

0

thx etteyafed pour votre réponse

le problème était

if (shadow == false) 

le code de travail ici:

if (shadow == false) { 
     if ((bmap == null) || (lastLatSpan != mapv.getLatitudeSpan()) || true) { 
      // bitmap is null - so we haven't previously drawn the path, OR 
      // the map has been zoomed in/out, so we're gonna re-draw it 
      // anyway 
      // (alternatively, I could have tried scaling the bitmap... 
      // might 
      // be worth investigating if that is more efficient) 

      // create a new bitmap, the size of the map view 
      Canvas offscreencanvas = new Canvas(); 
      bmap = Bitmap.createBitmap(mapv.getWidth(), mapv.getHeight(), 
        Bitmap.Config.ARGB_8888); 
      bmap.copy(Config.ARGB_4444, true); 
      offscreencanvas.setBitmap(bmap); 
      mCanvas = offscreencanvas; 

      Path path = null; 

      Projection projection = mapv.getProjection(); 

      lastLatSpan = mapv.getLatitudeSpan(); 

      for (int j = 0; j < mTourenArray.length; j++) { 
       GeoPoint[] route = GeoPointFactory 
         .createGeoPointArray(mTourenArray[j] 
           .getGeoPointArray()); 
       defaultColor = mTourenArray[j].getColorByCat(); 
       path = new Path(); 
       Paint polyPaint = new Paint(); 
       polyPaint.setColor(defaultColor); 

       for (int i = 0; i < route.length - 1; i++) { 
        GeoPoint gp1 = route[i]; 
        GeoPoint gp2 = route[i + 1]; 

        polyPaint.setAntiAlias(true); 
        Point point = new Point(); 
        projection.toPixels(gp1, point); 
        // mode=1&#65306;start 
        if (i == 0) { 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 

         RectF oval = new RectF(point.x - mRadius, point.y 
           - mRadius, point.x + mRadius, point.y 
           + mRadius); 
         // start point 
         offscreencanvas.drawOval(oval, polyPaint); 

         path.moveTo(point.x, point.y); 
         path.lineTo(point2.x, point2.y); 
        } 
        /* mode=3&#65306;end */ 
        else if (i == route.length - 2) { 
         /* the last path */ 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 
         path.lineTo(point2.x, point2.y); 

         RectF oval = new RectF(point2.x - mRadius, point2.y 
           - mRadius, point2.x + mRadius, point2.y 
           + mRadius); 
         /* end point */ 
         polyPaint.setAlpha(255); 
         offscreencanvas.drawOval(oval, polyPaint); 
        } 
        // mode=2&#65306;path 
        else if (i < route.length - 2 & i != 0) { 
         Point point2 = new Point(); 
         projection.toPixels(gp2, point2); 
         path.lineTo(point2.x, point2.y); 

        } 

       } 

       // create an off-screen canvas to prepare new bitmap, and 
       // draw path on to it 
       polyPaint.setStrokeWidth(5); 
       polyPaint.setStyle(Paint.Style.STROKE); 
       polyPaint.setAntiAlias(true); 
       polyPaint.setAlpha(defaultColor == Color 
         .parseColor("#6C8715") ? 220 : 120); 

       offscreencanvas.drawPath(path, polyPaint); 

      } // Outer For End 

      // draw the bitmap of the path onto my map view's canvas 
      // And set these pixels to destination bitmap: 
      canvas.drawBitmap(bmap, 0, 0, null); 

      panAfterZoom(mapv, 600); 
      // make a note of where we put the bitmap, so we know how 
      // much we 
      // we need to move it by if the user pans the map 
      mapStartPosition = projection.fromPixels(0, 0); 

     } else { 
      // as we're in onDraw, we think the user has panned/moved the 
      // map 
      // if we're in here, the zoom level hasn't changed, and 
      // we've already got a bitmap with a drawing of the route path 

      Projection proj = mapv.getProjection(); 

      // where has the mapview been panned to? 
      Point offsetPt = new Point(); 

      proj.toPixels(mapStartPosition, offsetPt); 

      // start point 

      // create a new bitmap, the size of the map view 
      // draw the bitmap in the new correct location 
      canvas.drawBitmap(bmap, offsetPt.x, offsetPt.y, null); 


     } 
    } 
    return super.draw(canvas, mapv, shadow, when); 
} 
0

Cela peut sembler évident, mais je ne suis pas sûr de ce que le contexte de votre méthode est (ou ce que certains de vos champs membres sont holding), mais il y a certaines choses que vous pouvez vérifier. mapStartPosition est-il égal à la position actuelle? Je ne suis pas familier avec la signature de méthode pour cet appel super.draw (Canvas, MapView, boolean, long), mais d'après mon expérience, il n'est généralement pas nécessaire/utile d'appeler la méthode super.draw directement. Doc dit que MapView hérite du tirage de la vue qui ne prend que Canvas comme argument, donc je ne suis pas sûr de ce que vous faites là. En général, j'appelle simplement dessiner sur la toile ou tout autre objet pouvant être dessiné.

Je sais que ce n'est pas vraiment une "réponse" mais peut-être que ça va aider. Bonne chance!