diff options
author | Tobrun <tobrun@mapbox.com> | 2017-12-19 19:38:11 +0100 |
---|---|---|
committer | Łukasz Paczos <lukasz.paczos@mapbox.com> | 2017-12-19 19:38:11 +0100 |
commit | af52538a81768977648c9d95190ba8c12e28f78a (patch) | |
tree | a2a4eaed0d7e0704b76e6a33694d0a2a1ebbe2b3 /platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils | |
parent | f242227a677880298429b26b1e6fc3a2d33a046f (diff) | |
download | qtlocation-mapboxgl-af52538a81768977648c9d95190ba8c12e28f78a.tar.gz |
Saving/restoring MyLocationViewSettings (#10746) (#10748)
[android] saving/restoring MyLocationViewSettings, updated BitmapUtils
Diffstat (limited to 'platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils')
-rw-r--r-- | platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java index af3a79539f..765ca431dd 100644 --- a/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java +++ b/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/BitmapUtils.java @@ -1,10 +1,16 @@ package com.mapbox.mapboxsdk.utils; +import android.content.Context; import android.graphics.Bitmap; +import android.graphics.BitmapFactory; import android.graphics.Canvas; +import android.graphics.drawable.BitmapDrawable; +import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; import android.view.View; +import java.io.ByteArrayOutputStream; + /** * Utility class for creating bitmaps */ @@ -46,4 +52,51 @@ public class BitmapUtils { return result; } + /** + * Extract an underlying bitmap from a drawable + * @param drawable The source drawable + * @return The underlying bitmap + */ + public static Bitmap getBitmapFromDrawable(Drawable drawable) { + if (drawable instanceof BitmapDrawable) { + return ((BitmapDrawable) drawable).getBitmap(); + } else { + Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), + Bitmap.Config.ARGB_8888); + Canvas canvas = new Canvas(bitmap); + drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); + drawable.draw(canvas); + return bitmap; + } + } + + /** + * Create a byte array out of drawable + * @param drawable The source drawable + * @return The byte array of source drawable + */ + public static byte[] getByteArrayFromDrawable(Drawable drawable) { + if (drawable == null) { + return null; + } + Bitmap bitmap = getBitmapFromDrawable(drawable); + ByteArrayOutputStream stream = new ByteArrayOutputStream(); + bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); + return stream.toByteArray(); + } + + /** + * Decode byte array to drawable object + * @param context Context to obtain {@link android.content.res.Resources} + * @param array The source byte array + * @return The drawable created from source byte array + */ + public static Drawable getDrawableFromByteArray(Context context, byte[] array) { + if (array == null) { + return null; + } + Bitmap compass = BitmapFactory.decodeByteArray(array, 0, array.length); + return new BitmapDrawable(context.getResources(), compass); + } + } |