简体   繁体   English

如何在 Jetpack Compose 的 Surface 中显示图像?

[英]How to show an image in Surface in Jetpack Compose?

Do anyone have a solution for showing an xml shape as a background by using the Jetpack Compose widget Surface?有没有人有使用 Jetpack Compose 小部件 Surface 将 xml 形状显示为背景的解决方案?

Do I need to use DrawImage, or is there a simpler approach?我需要使用 DrawImage,还是有更简单的方法?

In the example below I want to exchange the Color.Blue with a xml file called side_nav_bar.xml which is actually a gradient created with <shape> </shape>在下面的示例中,我想将Color.Blue与名为side_nav_bar.xml的 xml 文件交换,该文件实际上是使用<shape> </shape>创建的渐变

@Composable
private fun AppDrawer(
    currentScreen: Screen,
    closeDrawer: () -> Unit
)
{
val topImage = +imageResource(R.mipmap.empty_1024)


Column(
    crossAxisSize = LayoutSize.Expand,
    mainAxisSize = LayoutSize.Expand
) {

    Surface(color = Color.Blue)
    {
        Column(
            mainAxisSize = LayoutSize.Expand,
            crossAxisSize = LayoutSize.Expand)
        {
            Padding(8.dp) {
                Container(expanded = true, height = 100.dp, width = 100.dp){
                    Clip(shape = RoundedCornerShape(4.dp)) {
                        DrawImage(image = topImage)
                    }
                }
            }

            Padding(8.dp){
                Text(text = "Fuel Empty")
            }

            Padding(8.dp){
                Text(text = "https://www.fuelpump.no")
            }


        }

    }

    Divider()

    DrawerButton(
        icon = R.drawable.ic_baseline_store_24,
        label = "Stations",
        isSelected = currentScreen == Screen.Stations
    ){
        navigateTo(Screen.Stations)
        closeDrawer()
    }

    DrawerButton(
        icon = R.drawable.ic_baseline_map_24,
        label = "Overview",
        isSelected = currentScreen == Screen.Overview
    ){
        navigateTo(Screen.Overview)
        closeDrawer()
    }

    DrawerButton(
        icon = R.drawable.ic_baseline_format_list_numbered_24,
        label = "Statistics",
        isSelected = currentScreen == Screen.Statistics
    ){
        navigateTo(Screen.Statistics)
        closeDrawer()
    }

    Divider()

    DrawerButton(
        icon = R.drawable.ic_baseline_settings_applications_24,
        label = "Settings",
        isSelected = currentScreen == Screen.Settings
    ){
        closeDrawer()
    }

    DrawerButton(
        icon = R.drawable.ic_baseline_build_24,
        label = "Tools",
        isSelected = currentScreen == Screen.Tools
    ){
        closeDrawer()
    }

    DrawerButton(
        icon = R.drawable.ic_baseline_help_24,
        label = "Help",
        isSelected = currentScreen == Screen.Help
    ){
        closeDrawer()
    }



}
}

xml I want to use as background: xml 我想用作背景:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
   android:shape="rectangle">
<gradient
        android:angle="135"
        android:centerColor="@color/colorPrimaryDark"
        android:endColor="@color/ic_black_background"
        android:startColor="@color/colorPrimary"
        android:type="linear"/>
</shape>

RG RG

try this function.试试这个 function。 I fixed my problem.我解决了我的问题。

inline fun Image(
  asset:        VectorAsset,
  modifier:     Modifier     = Modifier,
  alignment:    Alignment    = Alignment.Center,
  contentScale: ContentScale = ContentScale.Inside,
  alpha:        Float        = DefaultAlpha,
  colorFilter:  ColorFilter? = null
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM