org.oscim.android.canvas.AndroidGraphics Java Examples

The following examples show how to use org.oscim.android.canvas.AndroidGraphics. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MBTilesTileDataSource.java    From geopaparazzi with GNU General Public License v3.0 8 votes vote down vote up
@Override
public void query(MapTile tile, ITileDataSink sink) {
    QueryResult res = FAILED;

    try {
        byte[] imageBytes = db.getTile(tile.tileX, tile.tileY, tile.zoomLevel);
        if (transparentColor != null || alpha != null) {
            android.graphics.Bitmap bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (transparentColor != null) {
                bmp = ImageUtilities.makeTransparent(bmp, transparentColor);
            }
            if (alpha != null) {
                bmp = ImageUtilities.makeBitmapTransparent(bmp, alpha);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, bos);
            imageBytes = bos.toByteArray();
        }

        Bitmap bitmap = AndroidGraphics.decodeBitmap(new ByteArrayInputStream(imageBytes));

        sink.setTileImage(bitmap);
        res = QueryResult.SUCCESS;
    } catch (Exception e) {
        log.debug("{} Error: {}", tile, e.getMessage());
    } finally {
        sink.completed(res);
    }
}
 
Example #2
Source File: Tags.java    From trekarta with GNU General Public License v3.0 6 votes vote down vote up
public static Drawable getTypeDrawable(Context context, IRenderTheme theme, int type) {
    TagSet tags = new TagSet();
    setTypeTag(type, tags);
    if (tags.size() == 0)
        return null;

    RenderStyle[] styles = theme.matchElement(GeometryBuffer.GeometryType.POINT, tags, 17);
    if (styles != null) {
        for (RenderStyle style : styles) {
            if (style instanceof SymbolStyle) {
                try {
                    org.oscim.backend.canvas.Bitmap bitmap;
                    bitmap = ((SymbolStyle) style).bitmap;
                    return new BitmapDrawable(context.getResources(), AndroidGraphics.getBitmap(bitmap));
                } catch (ClassCastException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    return null;
}
 
Example #3
Source File: MapCoverageLayer.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private static Bitmap getHillshadesBitmap(int color) {
    Bitmap bitmap;
    try {
        bitmap = CanvasAdapter.getBitmapAsset("", "symbols/hillshades.svg", 0, 0, 70, 0);
    } catch (IOException e) {
        log.error("Failed to read bitmap", e);
        return null;
    }
    android.graphics.Bitmap bitmapResult = android.graphics.Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), android.graphics.Bitmap.Config.ARGB_8888);
    android.graphics.Canvas canvas = new android.graphics.Canvas(bitmapResult);
    android.graphics.Paint paint = new android.graphics.Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(AndroidGraphics.getBitmap(bitmap), 0, 0, paint);
    return new AndroidBitmap(bitmapResult);
}
 
Example #4
Source File: OsmcSymbolFactory.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
private void drawSymbol(Canvas canvas, String foreground, int size) {
    if (foreground.length() == 0)
        return;

    Integer foregroundColor = null;
    String symbol = null;
    if (VALID_FOREGROUNDS.contains(foreground)) {
        // foreground is encoded as symbol without color
        logger.debug("  foreground: black {}", foreground);
        if ("shell".equals(foreground) || "shell_modern".equals(foreground))
            foregroundColor = Color.YELLOW;
        else
            foregroundColor = Color.BLACK;
        symbol = foreground;

    } else {
        // foreground should contain coloured symbol
        String[] foreground_parts = foreground.trim().split("\\s*_\\s*", 2);
        logger.debug("  foreground: {}", Arrays.toString(foreground_parts));
        if (foreground_parts.length == 2) {
            foregroundColor = ColorsCSS.get(foreground_parts[0]);
            if (VALID_FOREGROUNDS.contains(foreground_parts[1]))
                symbol = foreground_parts[1];
        }
        if (BuildConfig.DEBUG && foregroundColor == null)
            logger.error("Unknown foreground color: {}", foreground_parts[0]);
        if (BuildConfig.DEBUG && symbol == null)
            logger.error("Unknown foreground symbol: {}", foreground_parts.length == 1 ?
                    foreground_parts[0] : foreground_parts[1]);
    }
    if (foregroundColor != null && symbol != null) {
        try {
            Bitmap symbolBitmap = CanvasAdapter.getBitmapAsset("symbols/osmc", symbol + ".svg", size, size, 100, foregroundColor);
            canvas.drawBitmap(AndroidGraphics.getBitmap(symbolBitmap), 0, 0, null);
        } catch (IOException e) {
            logger.error("Failed to load bitmap for " + symbol, e);
        }
    }
}
 
Example #5
Source File: BitmapCache.java    From trekarta with GNU General Public License v3.0 5 votes vote down vote up
public V get(K key) {
    synchronized(cache) {
        V bitmap = cache.get(key);
        if (bitmap != null && !AndroidGraphics.getBitmap(bitmap).isRecycled())
            return bitmap;
        return null;
    }
}
 
Example #6
Source File: MapFragment.java    From open with GNU General Public License v3.0 5 votes vote down vote up
private MarkerItem getUserLocationMarker() {
    MarkerItem markerItem = new MarkerItem("ME", "Current Location", getUserLocationPoint());
    MarkerSymbol symbol = AndroidGraphics.makeMarker(
            getResources().getDrawable(R.drawable.ic_locate_me),
            MarkerItem.HotspotPlace.CENTER);
    markerItem.setMarker(symbol);
    return markerItem;
}
 
Example #7
Source File: GeopackageTileDataSource.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void query(MapTile tile, ITileDataSink sink) {
    QueryResult res = FAILED;

    try {
        byte[] imageBytes = adb.getTile(tableName, tile.tileX, tile.tileY, tile.zoomLevel);
        if (transparentColor != null || alpha != null) {
            android.graphics.Bitmap bmp = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
            if (transparentColor != null) {
                bmp = ImageUtilities.makeTransparent(bmp, transparentColor);
            }
            if (alpha != null) {
                bmp = ImageUtilities.makeBitmapTransparent(bmp, alpha);
            }
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bmp.compress(android.graphics.Bitmap.CompressFormat.PNG, 100, bos);
            imageBytes = bos.toByteArray();
        }

        Bitmap bitmap = AndroidGraphics.decodeBitmap(new ByteArrayInputStream(imageBytes));

        sink.setTileImage(bitmap);
        res = QueryResult.SUCCESS;
    } catch (Exception e) {
        log.debug("{} Error: {}", tile, e.getMessage());
    } finally {
        sink.completed(res);
    }
}
 
Example #8
Source File: MapHandler.java    From PocketMaps with MIT License 5 votes vote down vote up
private MarkerItem createMarkerItem(Context appContext, GeoPoint p, int resource, float offsetX, float offsetY) {
//      Drawable drawable = activity.getDrawable(resource); // Since API21
      Drawable drawable = ContextCompat.getDrawable(appContext, resource);
      Bitmap bitmap = AndroidGraphics.drawableToBitmap(drawable);
      MarkerSymbol markerSymbol = new MarkerSymbol(bitmap, offsetX, offsetY);
      MarkerItem markerItem = new MarkerItem("", "", p);
      markerItem.setMarker(markerSymbol);
      return markerItem;
  }
 
Example #9
Source File: RoutePreviewFragment.java    From open with GNU General Public License v3.0 5 votes vote down vote up
private MarkerItem getMarkerItem(int icon, Location loc, MarkerItem.HotspotPlace place) {
    MarkerItem markerItem = new MarkerItem("Generic Marker",
            "Generic Description", locationToGeoPoint(loc));
    markerItem.setMarker(new MarkerSymbol(
            AndroidGraphics.drawableToBitmap(app.getResources().getDrawable(icon)), place));
    return markerItem;
}
 
Example #10
Source File: ImagesLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private static MarkerSymbol getMarkerSymbol(GPMapView mapView) {
    SharedPreferences peferences = PreferenceManager.getDefaultSharedPreferences(mapView.getContext());
    String textSizeStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_TEXT_SIZE, LibraryConstants.DEFAULT_NOTES_SIZE + ""); //$NON-NLS-1$
    textSize = Integer.parseInt(textSizeStr);
    colorStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_CUSTOMCOLOR, ColorUtilities.ALMOST_BLACK.getHex());
    Drawable imagesDrawable = Compat.getDrawable(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_images_48dp);

    imagesBitmap = AndroidGraphics.drawableToBitmap(imagesDrawable);

    return new MarkerSymbol(imagesBitmap, MarkerSymbol.HotspotPlace.UPPER_LEFT_CORNER, false);
}
 
Example #11
Source File: NotesLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private static MarkerSymbol getMarkerSymbol(GPMapView mapView) {
    SharedPreferences peferences = PreferenceManager.getDefaultSharedPreferences(mapView.getContext());
    // notes type
    boolean doCustom = peferences.getBoolean(LibraryConstants.PREFS_KEY_NOTES_CHECK, true);
    String textSizeStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_TEXT_SIZE, LibraryConstants.DEFAULT_NOTES_SIZE + ""); //$NON-NLS-1$
    textSize = Integer.parseInt(textSizeStr);
    colorStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_CUSTOMCOLOR, ColorUtilities.ALMOST_BLACK.getHex());
    Drawable notesDrawable;
    if (doCustom) {
        String opacityStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_OPACITY, "255"); //$NON-NLS-1$
        String sizeStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_SIZE, LibraryConstants.DEFAULT_NOTES_SIZE + ""); //$NON-NLS-1$
        int noteSize = Integer.parseInt(sizeStr);
        float opacity = Integer.parseInt(opacityStr);

        OvalShape notesShape = new OvalShape();
        android.graphics.Paint notesPaint = new android.graphics.Paint(android.graphics.Paint.ANTI_ALIAS_FLAG);
        notesPaint.setStyle(android.graphics.Paint.Style.FILL);
        notesPaint.setColor(ColorUtilities.toColor(colorStr));
        notesPaint.setAlpha((int) opacity);

        ShapeDrawable notesShapeDrawable = new ShapeDrawable(notesShape);
        android.graphics.Paint paint = notesShapeDrawable.getPaint();
        paint.set(notesPaint);
        notesShapeDrawable.setIntrinsicHeight(noteSize);
        notesShapeDrawable.setIntrinsicWidth(noteSize);
        notesDrawable = notesShapeDrawable;
    } else {
        notesDrawable = Compat.getDrawable(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_place_accent_24dp);
    }

    notesBitmap = AndroidGraphics.drawableToBitmap(notesDrawable);

    return new MarkerSymbol(notesBitmap, MarkerSymbol.HotspotPlace.CENTER, false);
}
 
Example #12
Source File: BookmarkLayer.java    From geopaparazzi with GNU General Public License v3.0 5 votes vote down vote up
private static MarkerSymbol getMarkerSymbol(GPMapView mapView) {
    SharedPreferences peferences = PreferenceManager.getDefaultSharedPreferences(mapView.getContext());
    String textSizeStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_TEXT_SIZE, LibraryConstants.DEFAULT_NOTES_SIZE + ""); //$NON-NLS-1$
    colorStr = peferences.getString(LibraryConstants.PREFS_KEY_NOTES_CUSTOMCOLOR, ColorUtilities.ALMOST_BLACK.getHex());
    Drawable imagesDrawable = Compat.getDrawable(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_bookmarks_48dp);

    imagesBitmap = AndroidGraphics.drawableToBitmap(imagesDrawable);

    return new MarkerSymbol(imagesBitmap, MarkerSymbol.HotspotPlace.UPPER_LEFT_CORNER, false);
}
 
Example #13
Source File: RoutePreviewFragment.java    From open with GNU General Public License v3.0 4 votes vote down vote up
private void displayRoute() {
    if (route == null) {
        return;
    }

    if (getActivity() == null) {
        return;
    }

    mapController.getMap().layers().remove(path);
    mapController.getMap().layers().remove(markers);
    path = new PathLayer(MapController.getMapController().getMap(), Color.DKGRAY, 8);
    markers = new ItemizedLayer<MarkerItem>(
            MapController.getMapController().getMap(), new ArrayList<MarkerItem>(),
            AndroidGraphics.makeMarker(getActivity().getResources()
                            .getDrawable(R.drawable.ic_pin),
                    MarkerItem.HotspotPlace.BOTTOM_CENTER), null);

    List<Location> points = route.getGeometry();
    long time = System.currentTimeMillis();
    Logger.d("RoutePreviewFragment::success Geometry points before: " + points.size());
    if (points.size() > REDUCE_TOLERANCE) {
        points = reduceWithTolerance(points, REDUCE_TOLERANCE);
    }
    Logger.d("Timing: " + String.valueOf(System.currentTimeMillis() - time));
    Logger.d("RoutePreviewFragment::success Geometry points after: " + points.size());
    path.clearPath();
    double minlat = Integer.MAX_VALUE;
    double minlon = Integer.MAX_VALUE;
    double maxlat = Integer.MIN_VALUE;
    double maxlon = Integer.MIN_VALUE;
    for (Location loc : points) {
        maxlat = Math.max(maxlat, loc.getLatitude());
        maxlon = Math.max(maxlon, loc.getLongitude());
        minlat = Math.min(minlat, loc.getLatitude());
        minlon = Math.min(minlon, loc.getLongitude());
        path.addPoint(locationToGeoPoint(loc));
    }

    BoundingBox bbox = new BoundingBox(minlat, minlon, maxlat, maxlon);
    int w = mapController.getMap().getWidth();
    int h = mapController.getMap().getHeight();
    MapPosition position = new MapPosition();
    position.setByBoundingBox(bbox, w, h);

    position.setScale(position.getZoomScale() * 0.85);

    mapController.getMap().setMapPosition(position);

    if (!mapController.getMap().layers().contains(path)) {
        mapController.getMap().layers().add(path);
    }

    if (!mapController.getMap().layers().contains(markers)) {
        mapController.getMap().layers().add(markers);
    }
    markers.removeAllItems();
    markers.addItem(getMarkerItem(R.drawable.ic_a, points.get(0),
            MarkerItem.HotspotPlace.CENTER));
    markers.addItem(getMarkerItem(R.drawable.ic_b, points.get(points.size() - 1),
            MarkerItem.HotspotPlace.BOTTOM_CENTER));
}
 
Example #14
Source File: GpsPositionTextLayer.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
private static MarkerSymbol getMarkerSymbol(GPMapView mapView) {
    Drawable notesDrawable = Compat.getDrawable(mapView.getContext(), eu.geopaparazzi.library.R.drawable.ic_place_accent_24dp);
    notesBitmap = AndroidGraphics.drawableToBitmap(notesDrawable);

    return new MarkerSymbol(notesBitmap, MarkerSymbol.HotspotPlace.CENTER, false);
}
 
Example #15
Source File: VtmUtilities.java    From geopaparazzi with GNU General Public License v3.0 4 votes vote down vote up
public static Bitmap getBitmapFromResource(Context context, int resourceId) throws IOException {
    Drawable activeGpsMarker = Compat.getDrawable(context, resourceId);
    return AndroidGraphics.drawableToBitmap(activeGpsMarker);
}
 
Example #16
Source File: ShadowMapView.java    From open with GNU General Public License v3.0 4 votes vote down vote up
public void __constructor__(Context context, AttributeSet attributeSet) {
    AndroidGraphics.init();
    AndroidAssets.init(context);
}
 
Example #17
Source File: MapFragment.java    From open with GNU General Public License v3.0 4 votes vote down vote up
public MarkerSymbol getDefaultMarkerSymbol() {
    return AndroidGraphics.makeMarker(getResources().getDrawable(R.drawable.ic_pin),
            MarkerItem.HotspotPlace.BOTTOM_CENTER);
}
 
Example #18
Source File: MapFragment.java    From open with GNU General Public License v3.0 4 votes vote down vote up
public MarkerSymbol getHighlightMarkerSymbol() {
    return AndroidGraphics.makeMarker(getResources().getDrawable(R.drawable.ic_pin_active),
            MarkerItem.HotspotPlace.BOTTOM_CENTER);
}
 
Example #19
Source File: MapView.java    From trekarta with GNU General Public License v3.0 4 votes vote down vote up
public MapView(Context context, AttributeSet attributeSet) {
    super(context, attributeSet);

    if (isInEditMode())
        return;

    init();

    /* Not sure if this makes sense */
    this.setWillNotDraw(true);
    this.setClickable(true);
    this.setFocusable(true);
    this.setFocusableInTouchMode(true);

    /* Setup android backend */
    AndroidGraphics.init();
    AndroidAssets.init(context);
    DateTimeAdapter.init(new DateTime());

    DisplayMetrics metrics = getResources().getDisplayMetrics();
    CanvasAdapter.dpi = (int) (metrics.scaledDensity * CanvasAdapter.DEFAULT_DPI);
    if (!Parameters.CUSTOM_TILE_SIZE)
        Tile.SIZE = Tile.calculateTileSize();

    WindowManager windowManager = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
    Display display = windowManager.getDefaultDisplay();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2)
        display.getSize(mScreenSize);
    else {
        mScreenSize.x = display.getWidth();
        mScreenSize.y = display.getHeight();
    }

    if (!Parameters.CUSTOM_COORD_SCALE) {
        if (Math.min(mScreenSize.x, mScreenSize.y) > 1080)
            MapRenderer.COORD_SCALE = 4.0f;
    }

    /* Initialize the Map */
    mMap = new AndroidMap(this);

    /* Initialize Renderer */
    if (OPENGL_VERSION == 2.0)
        setEGLContextClientVersion(2);
    else {
        // OpenGL ES 3.0 is supported with Android 4.3 (API level 18) and higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            try {
                setEGLContextFactory(new GlContextFactory());
            } catch (Throwable t) {
                log.error("Falling back to GLES 2", t);
                setEGLContextClientVersion(2);
            }
        } else
            setEGLContextClientVersion(2);
    }
    setEGLConfigChooser(new GlConfigChooser());

    if (GLAdapter.debug)
        setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR
                | GLSurfaceView.DEBUG_LOG_GL_CALLS);

    setRenderer(new GLRenderer(mMap));
    setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    mMap.clearMap();
    mMap.updateMap(false);

    if (!Parameters.MAP_EVENT_LAYER2) {
        GestureHandler gestureHandler = new GestureHandler(mMap);
        mGestureDetector = new GestureDetector(context, gestureHandler);
        mGestureDetector.setOnDoubleTapListener(gestureHandler);
    }

    mMotionEvent = new AndroidMotionEvent();
}