summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobrun <tobrun.van.nuland@gmail.com>2017-10-29 08:01:10 -0700
committerTobrun <tobrun.van.nuland@gmail.com>2017-10-31 15:56:17 -0700
commitf620068b99dc5adcf0538fc6c833df795ef18674 (patch)
treed25eb038be04d46dccdf64317d46d0eebaa0aa95
parent56c2af5670852312f788f83ff86d80447c30b0fd (diff)
downloadqtlocation-mapboxgl-upstream/tvn-logo-resize.tar.gz
[android] - add logo resize to MapSnapshotterupstream/tvn-logo-resize
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnaphotUtil.java29
-rw-r--r--platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java33
2 files changed, 58 insertions, 4 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnaphotUtil.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnaphotUtil.java
new file mode 100644
index 0000000000..da86dc51fb
--- /dev/null
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnaphotUtil.java
@@ -0,0 +1,29 @@
+package com.mapbox.mapboxsdk.snapshotter;
+
+import android.graphics.BitmapFactory;
+
+class MapSnaphotUtil {
+
+ static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
+ // Raw height and width of image
+ final int height = options.outHeight;
+ final int width = options.outWidth;
+ int inSampleSize = 1;
+
+ if (height > reqHeight || width > reqWidth) {
+
+ final int halfHeight = height / 2;
+ final int halfWidth = width / 2;
+
+ // Calculate the largest inSampleSize value that is a power of 2 and keeps both
+ // height and width larger than the requested height and width.
+ while ((halfHeight / inSampleSize) >= reqHeight
+ && (halfWidth / inSampleSize) >= reqWidth) {
+ inSampleSize *= 2;
+ }
+ }
+ return inSampleSize;
+ }
+
+
+}
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java
index 37d05fc328..4fbbc2409c 100644
--- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java
+++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/snapshotter/MapSnapshotter.java
@@ -7,6 +7,7 @@ import android.graphics.Canvas;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.UiThread;
+import android.util.DisplayMetrics;
import com.mapbox.mapboxsdk.R;
import com.mapbox.mapboxsdk.camera.CameraPosition;
@@ -55,7 +56,7 @@ public class MapSnapshotter {
void onError(String error);
}
- private static final int LOGO_MARGIN_PX = 4;
+ private static final int LOGO_MARGIN_DP = 4;
// Holds the pointer to JNI NativeMapView
private long nativePtr = 0;
@@ -252,10 +253,34 @@ public class MapSnapshotter {
}
protected void addOverlay(Bitmap original) {
- float margin = context.getResources().getDisplayMetrics().density * LOGO_MARGIN_PX;
+ DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
+ float margin = displayMetrics.density * LOGO_MARGIN_DP;
Canvas canvas = new Canvas(original);
- Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.mapbox_logo_icon, null);
- canvas.drawBitmap(logo, margin, original.getHeight() - (logo.getHeight() + margin), null);
+
+ // Decode just the boundaries
+ BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
+ bitmapOptions.inJustDecodeBounds = true;
+ BitmapFactory.decodeResource(context.getResources(), R.drawable.mapbox_logo_icon, bitmapOptions);
+ int srcWidth = bitmapOptions.outWidth;
+ int srcHeight = bitmapOptions.outHeight;
+
+ // Ratio, preferred dimensions and resulting sample size
+ float widthRatio = displayMetrics.widthPixels / original.getWidth();
+ float heightRatio = displayMetrics.heightPixels / original.getHeight();
+ float prefWidth = srcWidth / widthRatio;
+ float prefHeight = srcHeight / heightRatio;
+ int sampleSize = MapSnaphotUtil.calculateInSampleSize(bitmapOptions, (int) prefWidth, (int) prefHeight);
+
+ // Scale bitmap
+ bitmapOptions.inJustDecodeBounds = false;
+ bitmapOptions.inScaled = true;
+ bitmapOptions.inSampleSize = sampleSize;
+ bitmapOptions.inDensity = srcWidth;
+ bitmapOptions.inTargetDensity = (int) prefWidth * bitmapOptions.inSampleSize;
+ Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.drawable.mapbox_logo_icon, bitmapOptions);
+
+ float scaledHeight = bitmapOptions.outHeight * heightRatio / bitmapOptions.inSampleSize;
+ canvas.drawBitmap(logo, margin, original.getHeight() - scaledHeight - margin, null);
}
/**