compose for desktop 加载图片
To load an image on a desktop using the Compose framework, you can use the following code:
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.res.imageResource
@Composable
fun LoadImage() {
val image = imageResource("path_to_image.jpg") // Replace "path_to_image.jpg" with the actual path to your image file
val bitmap = image.asImageBitmap()
Image(
bitmap = bitmap,
contentDescription = null, // Provide a suitable description for accessibility
modifier = Modifier.fillMaxSize()
)
}
In the above code, you need to replace "path_to_image.jpg" with the actual path to your image file. The imageResource function is used to load the image from the resources folder. Make sure to place your image file in the appropriate folder (e.g., res/drawable) for it to be found by the imageResource function.
Once you have defined the LoadImage composable function, you can use it in your desktop app's UI by calling LoadImage() inside a Column or any other appropriate container composable
原文地址: https://www.cveoy.top/t/topic/ivT9 著作权归作者所有。请勿转载和采集!