Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
[android] - add logo resize to MapSnapshotter
Browse files Browse the repository at this point in the history
  • Loading branch information
tobrun committed Nov 1, 2017
1 parent c61ccb3 commit 53e61fa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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;
Expand Down Expand Up @@ -55,7 +56,7 @@ public interface ErrorHandler {
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;
Expand Down Expand Up @@ -262,10 +263,34 @@ public void cancel() {
}

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);
}

/**
Expand Down

0 comments on commit 53e61fa

Please sign in to comment.