importandroidx.compose.foundation.AndroidExternalSurfaceimportandroidx.compose.foundation.layout.fillMaxWidthimportandroidx.compose.foundation.layout.heightimportandroidx.compose.runtime.withFrameNanosimportandroidx.compose.ui.Modifierimportandroidx.compose.ui.graphics.Colorimportandroidx.compose.ui.graphics.lerpimportandroidx.compose.ui.graphics.toArgbimportandroidx.compose.ui.unit.dpAndroidExternalSurface(modifier=Modifier.fillMaxWidth().height(400.dp)){// Resources can be initialized/cached here// A surface is available, we can start renderingonSurface{surface,width,height->
varw=widthvarh=height// Initial draw to avoid a black framesurface.lockCanvas(Rect(0,0,w,h)).apply{drawColor(Color.Blue.toArgb())surface.unlockCanvasAndPost(this)}// React to surface dimension changessurface.onChanged{newWidth,newHeight->
w=newWidthh=newHeight}// Cleanup if neededsurface.onDestroyed{}// Render loop, automatically cancelled on surface destructionwhile(true){withFrameNanos{time->
surface.lockCanvas(Rect(0,0,w,h)).apply{valtimeMs=time/1_000_000Lvalt=0.5f+0.5f*sin(timeMs/1_000.0f)drawColor(lerp(Color.Blue,Color.Green,t).toArgb())surface.unlockCanvasAndPost(this)}}}}}
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2026-06-24 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2026-06-24 UTC."],[],[]]