Skip to content
Permalink
Browse files

Android samples: simplify bitmap reading, #1138

  • Loading branch information
devemux86 committed Nov 30, 2019
1 parent c213132 commit edea280e690c18d4340118e6374fba651c89b5ba
@@ -1,6 +1,7 @@
/*
* Copyright 2014 Martin Vennekamp
* Copyright 2015 mapsforge.org
* Copyright 2019 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -15,24 +16,18 @@
*/
package org.mapsforge.samples.android;

import android.graphics.drawable.Drawable;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.widget.Toast;

import org.mapsforge.core.graphics.Align;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Color;
import org.mapsforge.core.graphics.FontFamily;
import org.mapsforge.core.graphics.FontStyle;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.graphics.*;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.samples.android.cluster.ClusterManager;
import org.mapsforge.samples.android.cluster.GeoItem;
@@ -63,13 +58,10 @@
private List<MarkerBitmap> getMarkerBitmap() {
List<MarkerBitmap> markerBitmaps = new ArrayList<MarkerBitmap>();
// prepare for marker icons.
Drawable balloon;
// small icon for maximum single item
balloon = getResources().getDrawable(R.drawable.marker_green);
Bitmap bitmapClimbingPeak = AndroidGraphicFactory.convertToBitmap(balloon);
Bitmap bitmapClimbingPeak = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_green));
bitmapClimbingPeak.incrementRefCount();
balloon = getResources().getDrawable(R.drawable.marker_red);
Bitmap markerRedS = AndroidGraphicFactory.convertToBitmap(balloon);
Bitmap markerRedS = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_red));
markerRedS.incrementRefCount();
Paint paint1;
paint1 = AndroidGraphicFactory.INSTANCE.createPaint();
@@ -82,12 +74,9 @@
markerBitmaps.add(new MarkerBitmap(this.getApplicationContext(), bitmapClimbingPeak, markerRedS,
new Point(0, 0), 10f, 1, paint1));
// small icon. for 10 or less items.
balloon = getResources().getDrawable(R.drawable.balloon_s_n);
Bitmap bitmapBalloonSN = AndroidGraphicFactory
.convertToBitmap(balloon);
Bitmap bitmapBalloonSN = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.balloon_s_n));
bitmapBalloonSN.incrementRefCount();
balloon = getResources().getDrawable(R.drawable.balloon_s_s);
Bitmap bitmapBalloonSS = AndroidGraphicFactory.convertToBitmap(balloon);
Bitmap bitmapBalloonSS = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.balloon_s_s));
bitmapBalloonSS.incrementRefCount();
Paint paint2;
paint2 = AndroidGraphicFactory.INSTANCE.createPaint();
@@ -100,12 +89,9 @@
markerBitmaps.add(new MarkerBitmap(this.getApplicationContext(), bitmapBalloonSN,
bitmapBalloonSS, new Point(0, 0), 9f, 10, paint2));
// large icon. 100 will be ignored.
balloon = getResources().getDrawable(R.drawable.balloon_m_n);
Bitmap bitmapBalloonMN = AndroidGraphicFactory.convertToBitmap(balloon);
Bitmap bitmapBalloonMN = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.balloon_m_n));
bitmapBalloonMN.incrementRefCount();
balloon = getResources().getDrawable(R.drawable.balloon_m_s);
Bitmap bitmapBalloonMS = AndroidGraphicFactory
.convertToBitmap(balloon);
Bitmap bitmapBalloonMS = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.balloon_m_s));
bitmapBalloonMS.incrementRefCount();
Paint paint3;
paint3 = AndroidGraphicFactory.INSTANCE.createPaint();
@@ -1,6 +1,6 @@
/*
* Copyright 2016 mapicke
* Copyright 2016 devemux86
* Copyright 2016-2019 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -15,11 +15,13 @@
*/
package org.mapsforge.samples.android;

import android.graphics.BitmapFactory;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Color;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.samples.android.group.ChildMarker;
import org.mapsforge.samples.android.group.GroupMarker;
@@ -39,10 +41,9 @@ protected void createLayers() {
addGroupMarker();
}

@SuppressWarnings("deprecation")
private void addGroupMarker() {
LatLong latLong = new LatLong(52.525582, 13.370061);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(getResources().getDrawable(R.drawable.marker_green));
Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_green));
GroupMarker groupMarker = new GroupMarker(latLong, bitmap, 0, -bitmap.getHeight() / 2, mapView.getLayerManager().getLayers(), BLACK);
for (int i = 0; i < 10.; i++) {
groupMarker.getChildren().add(new ChildMarker(latLong, bitmap, 0, 0, BLACK));
@@ -17,18 +17,19 @@
package org.mapsforge.samples.android;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.graphics.BitmapFactory;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.android.layers.MyLocationOverlay;
import org.mapsforge.map.layer.overlay.Circle;
@@ -52,15 +53,13 @@ private static Paint getPaint(int color, int strokeWidth, Style style) {
private LocationManager locationManager;
private MyLocationOverlay myLocationOverlay;

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
protected void createLayers() {
super.createLayers();

// marker to show at the location
Drawable drawable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDrawable(R.drawable.ic_maps_indicator_current_position) : getResources().getDrawable(R.drawable.ic_maps_indicator_current_position);
Marker marker = new Marker(null, AndroidGraphicFactory.convertToBitmap(drawable), 0, 0);
Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.ic_maps_indicator_current_position));
Marker marker = new Marker(null, bitmap, 0, 0);

// circle to show the location accuracy (optional)
Circle circle = new Circle(null, 0,
@@ -1,6 +1,6 @@
/*
* Copyright 2013-2014 Ludwig M Brinckmann
* Copyright 2015-2018 devemux86
* Copyright 2015-2019 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -15,22 +15,17 @@
*/
package org.mapsforge.samples.android;

import android.annotation.TargetApi;
import android.os.Build;
import android.graphics.BitmapFactory;
import android.widget.Toast;

import org.mapsforge.core.graphics.Color;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.layer.Layers;
import org.mapsforge.map.layer.overlay.Circle;
import org.mapsforge.map.layer.overlay.FixedPixelCircle;
import org.mapsforge.map.layer.overlay.Marker;
import org.mapsforge.map.layer.overlay.Polygon;
import org.mapsforge.map.layer.overlay.Polyline;
import org.mapsforge.map.layer.overlay.*;

import java.util.ArrayList;
import java.util.List;
@@ -57,9 +52,6 @@
protected LatLong latLong14 = new LatLong(52.516, 13.4245);
protected LatLong latLong15 = new LatLong(52.526, 13.4345);


@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
protected void addOverlayLayers(Layers layers) {

Polyline polyline = new Polyline(Utils.createPaint(
@@ -75,7 +67,7 @@ protected void addOverlayLayers(Layers layers) {
// this illustrates that bitmap shaders can be used on a path, but then any dash effect
// will not be applied.
Paint shaderPaint = Utils.createPaint(AndroidGraphicFactory.INSTANCE.createColor(Color.GREEN), 90, Style.STROKE);
shaderPaint.setBitmapShader(AndroidGraphicFactory.convertToBitmap(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDrawable(R.drawable.marker_green) : getResources().getDrawable(R.drawable.marker_green)));
shaderPaint.setBitmapShader(new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_green)));

Polyline polylineWithShader = new Polyline(shaderPaint, AndroidGraphicFactory.INSTANCE, true) {
@Override
@@ -120,7 +112,7 @@ public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
Paint paintFill2 = Utils.createPaint(
AndroidGraphicFactory.INSTANCE.createColor(Color.GREEN), 2,
Style.FILL);
paintFill2.setBitmapShader(AndroidGraphicFactory.convertToBitmap(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDrawable(R.drawable.marker_green) : getResources().getDrawable(R.drawable.marker_green)));
paintFill2.setBitmapShader(new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_green)));

Paint paintStroke2 = Utils.createPaint(
AndroidGraphicFactory.INSTANCE.createColor(Color.BLACK), 2,
@@ -137,7 +129,7 @@ public boolean onTap(LatLong tapLatLong, Point layerXY, Point tapXY) {
Paint paintFill3 = Utils.createPaint(
AndroidGraphicFactory.INSTANCE.createColor(Color.RED), 2,
Style.FILL);
paintFill3.setBitmapShader(AndroidGraphicFactory.convertToBitmap(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? getDrawable(R.drawable.marker_red) : getResources().getDrawable(R.drawable.marker_red)));
paintFill3.setBitmapShader(new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_red)));

Paint paintStroke3 = Utils.createPaint(
AndroidGraphicFactory.INSTANCE.createColor(Color.BLACK), 2,
@@ -14,15 +14,16 @@
*/
package org.mapsforge.samples.android;

import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.model.BoundingBox;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.layer.GroupLayer;
import org.mapsforge.map.layer.overlay.Marker;
@@ -104,7 +105,6 @@ private PoiSearchTask(PoiSearchViewer activity, String category) {
return null;
}

@SuppressWarnings("deprecation")
@Override
protected void onPostExecute(Collection<PointOfInterest> pointOfInterests) {
final PoiSearchViewer activity = weakActivity.get();
@@ -118,7 +118,7 @@ protected void onPostExecute(Collection<PointOfInterest> pointOfInterests) {

// Overlay POI
groupLayer = new GroupLayer();
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? activity.getDrawable(R.drawable.marker_green) : activity.getResources().getDrawable(R.drawable.marker_green));
Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.marker_green));
for (final PointOfInterest pointOfInterest : pointOfInterests) {
Marker marker = new MarkerImpl(pointOfInterest.getLatLong(), bitmap, 0, -bitmap.getHeight() / 2, pointOfInterest);
groupLayer.layers.add(marker);
@@ -1,6 +1,6 @@
/*
* Copyright 2013-2014 Ludwig M Brinckmann
* Copyright 2014, 2015 devemux86
* Copyright 2014-2019 devemux86
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -18,18 +18,19 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;
import android.view.View.MeasureSpec;
import android.widget.Toast;

import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Paint;
import org.mapsforge.core.graphics.Style;
import org.mapsforge.core.model.LatLong;
import org.mapsforge.core.model.Point;
import org.mapsforge.map.android.graphics.AndroidBitmap;
import org.mapsforge.map.android.graphics.AndroidGraphicFactory;
import org.mapsforge.map.layer.overlay.Marker;

@@ -67,12 +68,9 @@ public static void setBackground(View view, Drawable background) {
}
}

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static Marker createMarker(Context c, int resourceIdentifier,
LatLong latLong) {
Drawable drawable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? c.getDrawable(resourceIdentifier) : c.getResources().getDrawable(resourceIdentifier);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2);
}

@@ -84,12 +82,9 @@ static Paint createPaint(int color, int strokeWidth, Style style) {
return paint;
}

@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
static Marker createTappableMarker(final Context c, int resourceIdentifier,
LatLong latLong) {
Drawable drawable = Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP ? c.getDrawable(resourceIdentifier) : c.getResources().getDrawable(resourceIdentifier);
Bitmap bitmap = AndroidGraphicFactory.convertToBitmap(drawable);
Bitmap bitmap = new AndroidBitmap(BitmapFactory.decodeResource(c.getResources(), resourceIdentifier));
bitmap.incrementRefCount();
return new Marker(latLong, bitmap, 0, -bitmap.getHeight() / 2) {
@Override

0 comments on commit edea280

Please sign in to comment.
You can’t perform that action at this time.