Skip to content

Commit

Permalink
feat: android expose image asset width and height
Browse files Browse the repository at this point in the history
Request from a customer.

This unlocks the ability to resize images at runtime to fit the appropriate `ImageAsset` dimensions.

Diffs=
a7421a4bd feat: android expose image asset width and height (#8167)

Co-authored-by: Gordon <[email protected]>
  • Loading branch information
HayesGordon and HayesGordon committed Sep 19, 2024
1 parent 5dacf18 commit facbcc3
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .rive_head
Original file line number Diff line number Diff line change
@@ -1 +1 @@
adc2405543ea447b83fda212a0cd7cc4eafd4de9
a7421a4bd4c3c6082742e7eadf4ab13f6b5abe23
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,34 @@ class RiveAssetsTest {
renderImage.release()
}

@Test
fun imageAssetWidthAndHeightValidation() {
var imageAsset: ImageAsset? = null
val myLoader = object : ContextAssetLoader(appContext) {
override fun loadContents(asset: FileAsset, inBandBytes: ByteArray): Boolean {
if (asset is ImageAsset) {
imageAsset = asset
return true
}
return false
}

}
val file = File(
appContext.resources.openRawResource(R.raw.asset_load_check).readBytes(),
fileAssetLoader = myLoader,
rendererType = RendererType.Skia,
)
assertEquals(1, file.firstArtboard.animationCount)
assertNotNull(imageAsset)
assert(imageAsset!!.width == 1280.0f)
assert(imageAsset!!.height == 720.0f)

/* Clean things up */
myLoader.release()
file.release()
}

@Test
fun makeFont() {
val font = RiveFont.make(fontBytes)
Expand Down
18 changes: 18 additions & 0 deletions kotlin/src/main/cpp/src/bindings/bindings_file_asset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,24 @@ extern "C"
return reinterpret_cast<jlong>(imageAsset->renderImage());
}

JNIEXPORT jfloat JNICALL
Java_app_rive_runtime_kotlin_core_ImageAsset_cppImageAssetWidth(JNIEnv* env,
jobject thisObj,
jlong address)
{
auto image = reinterpret_cast<rive::ImageAsset*>(address);
return image->width();
}

JNIEXPORT jfloat JNICALL
Java_app_rive_runtime_kotlin_core_ImageAsset_cppImageAssetHeight(JNIEnv* env,
jobject thisObj,
jlong address)
{
auto image = reinterpret_cast<rive::ImageAsset*>(address);
return image->height();
}

JNIEXPORT jlong JNICALL
Java_app_rive_runtime_kotlin_core_RiveRenderImage_00024Companion_cppMakeImage(
JNIEnv* env,
Expand Down
13 changes: 13 additions & 0 deletions kotlin/src/main/java/app/rive/runtime/kotlin/core/FileAsset.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ sealed class FileAsset(address: Long, rendererTypeIdx: Int) : NativeObject(addre
class ImageAsset(address: Long, rendererTypeIdx: Int) : FileAsset(address, rendererTypeIdx) {
private external fun cppSetRenderImage(cppAsset: Long, cppRenderImage: Long)
private external fun cppGetRenderImage(cppAsset: Long): Long
private external fun cppImageAssetWidth(cppPointer: Long): Float
private external fun cppImageAssetHeight(cppPointer: Long): Float

/**
* The [RiveRenderImage] object associated with this [ImageAsset].
Expand All @@ -42,6 +44,17 @@ class ImageAsset(address: Long, rendererTypeIdx: Int) : FileAsset(address, rende
@VisibleForTesting
get() = RiveRenderImage(cppGetRenderImage(cppPointer))

/**
* The width of the image in pixels.
*/
val width: Float
get() = cppImageAssetWidth(cppPointer)

/**
* The height of the image in pixels.
*/
val height: Float
get() = cppImageAssetHeight(cppPointer)
}

/**
Expand Down

0 comments on commit facbcc3

Please sign in to comment.