Skip to content

Commit

Permalink
Allow vector drawables as native images (rnmapbox#3206)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamuelBrucksch authored Nov 28, 2023
1 parent 4ad96e9 commit 73272d4
Showing 1 changed file with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.rnmapbox.rnmbx.components.images

import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.view.View
import com.facebook.react.bridge.*
import com.facebook.react.common.MapBuilder
Expand All @@ -11,7 +14,6 @@ import com.mapbox.maps.ImageContent
import com.mapbox.maps.ImageStretches
import com.rnmapbox.rnmbx.components.AbstractEventEmitter
import com.rnmapbox.rnmbx.events.constants.EventKeys
import com.rnmapbox.rnmbx.events.constants.eventMapOf
import com.rnmapbox.rnmbx.utils.ImageEntry
import com.rnmapbox.rnmbx.utils.Logger
import com.rnmapbox.rnmbx.utils.ResourceUtils
Expand Down Expand Up @@ -134,12 +136,33 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
images.setHasOnImageMissing(value.asBoolean())
}

private fun convertDrawableToBitmap(sourceDrawable: Drawable?): BitmapDrawable? {
if (sourceDrawable == null) {
return null
}
return if (sourceDrawable is BitmapDrawable) {
sourceDrawable
} else {
// copying drawable object to not manipulate on the same reference
val constantState = sourceDrawable.constantState ?: return null
val drawable = constantState.newDrawable().mutate()
val bitmap: Bitmap = Bitmap.createBitmap(
drawable.intrinsicWidth, drawable.intrinsicHeight,
Bitmap.Config.ARGB_8888
)
val canvas = Canvas(bitmap)
drawable.setBounds(0, 0, canvas.width, canvas.height)
drawable.draw(canvas)
BitmapDrawable(mContext.resources, bitmap)
}
}

fun toNativeImage(dynamic: Dynamic): NativeImage? {
when (dynamic.type) {
ReadableType.String -> {
val resourceName = dynamic.asString();
val drawable =
ResourceUtils.getDrawableByName(mContext, resourceName) as BitmapDrawable?
convertDrawableToBitmap(ResourceUtils.getDrawableByName(mContext, resourceName))
if (drawable != null) {
return NativeImage(ImageInfo(name=resourceName), drawable)
} else {
Expand All @@ -151,7 +174,7 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
val map = dynamic.asMap()
val resourceName = map.getString("name")
val drawable =
ResourceUtils.getDrawableByName(mContext, resourceName) as BitmapDrawable?
convertDrawableToBitmap(ResourceUtils.getDrawableByName(mContext, resourceName))
if (drawable != null && resourceName != null) {
return NativeImage(imageInfo(resourceName, map), drawable)
} else {
Expand Down Expand Up @@ -267,4 +290,4 @@ class RNMBXImagesManager(private val mContext: ReactApplicationContext) :
return ImageContent(result[0].toFloat(), result[1].toFloat() ,result[2].toFloat(), result[3].toFloat())
}
}
}
}

0 comments on commit 73272d4

Please sign in to comment.