moagrius / TileView

TileView is a subclass of android.view.ViewGroup that asynchronously displays, pans and zooms tile-based images. Plugins are available for features like markers, hotspots, and path drawing.
MIT License
1.46k stars 337 forks source link

Fatal error when drawing a path #47

Closed ghost closed 10 years ago

ghost commented 10 years ago

From LogCat:

E/AndroidRuntime(26958): FATAL EXCEPTION: main
E/AndroidRuntime(26958): java.lang.UnsupportedOperationException
    at android.view.GLES20Canvas.quickReject(GLES20Canvas.java:490)
    at com.qozix.tileview.paths.PathManager.onDraw(PathManager.java:115)

Running on Android 4.0.4

Most likely relates to this: http://stackoverflow.com/questions/10173968/unsupportedoperationexception-in-gles20canvas-clippath-with-hardware-acceleratio so should be an easy fix.

ghost commented 10 years ago

Proposed fix:

@SuppressLint("NewApi")
@Override
public void onDraw( Canvas canvas ) {
    if ( shouldDraw ) {
        float scale = (float) detailManager.getScale();
        matrix.setScale( scale, scale );
        for ( DrawablePath drawablePath : paths ) {
            drawingPath.set( drawablePath.path );
            drawingPath.transform( matrix );
            if (android.os.Build.VERSION.SDK_INT >= 11 && canvas.isHardwareAccelerated()
            && android.os.Build.VERSION.SDK_INT < 16) {
                canvas.drawPath( drawingPath, drawablePath.paint );
            } else if ( !canvas.quickReject( drawingPath, Canvas.EdgeType.BW ) ) {
                canvas.drawPath( drawingPath, drawablePath.paint );
            }
        }
    }
    super.onDraw( canvas );
}

canvas.quickReject is available for hw accelerated canvas only starting from SDK 4.1 (16) thus calling this on earlier android builds will cause the fatal exception. Furthermore canvas.isHardwareAccelerated() check is available only since SDK 11 thus we need to apply multiple conditions here. I believe that pre SDK 11 builds have HW acceleration turned off by default so this shouldn't be causing any issues on older builds.

moagrius commented 10 years ago

included in your PR. Thanks.