Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are a few things you can do to improve efficiency.</p> <p>Your first block of code could be made slightly more efficient:</p> <pre><code>for (int t = 0, size = roads.size(); t &lt; size; t++) { //Avoid calling '.size()' over and over for (int i = 0; i &lt; roads.get(t).size() - 1; i++) {//Avoid calling '.size()' over and over final GeoPoint road = roads.get(t).get(i); //Reduce the number of get() calls. if (boundBox[0] &gt;= road.getLatitudeE6() &amp;&amp; boundBox[1] &gt;= road.getLongitudeE6() &amp;&amp; boundBox[2] &lt;= road.getLatitudeE6() &amp;&amp; boundBox[3] &lt;= road.getLongitudeE6()) { MyOverlay mOverlay = new MyOverlay(); mOverlay.setColor(Color.GREEN); mOverlay.setWidth(4); mOverlay.setPair(road, roads.get(t).get(i + 1)); mapOverlays.add(mOverlay); } } } </code></pre> <p>But most importantly, the biggest performance drain I can see in your code is that you are allocating a new rendering objects (Paint, Path, Point) every time draw() is called. This can be refactored so you reuse the same Paint instance:</p> <pre><code>class MyOverlay extends Overlay { GeoPoint gp1 = new GeoPoint(0, 0); GeoPoint gp2 = new GeoPoint(0, 0); Point p1 = new Point(); Point p2 = new Point(); Path path = new Path(); int colr=0,width=0; public MyOverlay() { Paint mPaint = new Paint(); mPaint.setDither(true); mPaint.setColor(colr); mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mPaint.setStrokeJoin(Paint.Join.ROUND); mPaint.setStrokeCap(Paint.Cap.ROUND); mPaint.setStrokeWidth(width); } public void draw(Canvas canvas, MapView mapv, boolean shadow) { super.draw(canvas, mapv, false); path.reset(); Projection projection = mapv.getProjection(); projection.toPixels(gp1, p1); projection.toPixels(gp2, p2); path.moveTo((float) p2.x, (float) p2.y); path.lineTo((float) p1.x, (float) p1.y); canvas.drawPath(path, mPaint); } } </code></pre> <p>For more info see the 'Do's and Dont's section of the article here: <a href="http://android-developers.blogspot.com.au/2011/03/android-30-hardware-acceleration.html" rel="nofollow">http://android-developers.blogspot.com.au/2011/03/android-30-hardware-acceleration.html</a>. </p> <p>The relevant point from the article is: <em>"Don't create render objects in draw methods: a common mistake is to create a new Paint, or a new Path, every time a rendering method is invoked. This is not only wasteful, forcing the system to run the GC more often, it also bypasses caches and optimizations in the hardware pipeline."</em></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload