AdRenderer

interface AdRenderer

Ad Renderer interface for creating renderers in Compose.

You can create custom views to display advertisements. To do so, your must follow this interface. Example of creating a custom renderer:

internal class MyRenderer: AdRenderer {
private lateinit var adState: AdStateInterface

private var tapURL: String? = null
private var imageBitmap by mutableStateOf<ImageBitmap?>(null)
// ...

override fun prepare(adState: AdStateInterface) {
this.adState = adState
// ...
// Decode and process data somehow
// ...
adState.state = AdState.State.Caching
scope.launch {
val imageBitmapDeferred = async { loadBitmap(adState, imageUrl) }
imageBitmap = imageBitmapDeferred.await()

if (imageBitmap != null) {
adState.state = AdState.State.AdReadyToDisplay
}
}
}

@Composable
override fun RenderAd(modifier: Modifier) {
val imageBitmap = rememberUpdatedState(this.imageBitmap)

if (imageBitmap.value != null && clickUrl != null) {
Image(
bitmap = imageBitmap.value!!,
contentDescription = null,
modifier = modifier.clickable { adState.triggerAdClick(clickUrl) }
)
}
}

private suspend fun loadBitmap(adState: AdStateInterface, url: String?): ImageBitmap? = supervisorScope {
try {
BitmapLoader.loadBitmap(url, adState)
} catch (exception: Exception) {
adState.state = AdState.State.Error(
AdException(
AdError.DECODING,
exception = Exception("MyRenderer: " + exception.message)
)
)
null
}
}
}

Functions

Link copied to clipboard
abstract fun prepare(adState: AdStateInterface)

Passes AdStateInterface with the new data received from the server to the renderer.

Link copied to clipboard
open fun release()
Link copied to clipboard
abstract fun RenderAd(modifier: Modifier)

Render ad composable.