如何在合成图像中加载可绘制的形状 [英] How to load shape drawable in compose image

查看:41
本文介绍了如何在合成图像中加载可绘制的形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 compose 中为图像设置一个可绘制的形状,可绘制看起来像这样curved_rect.xml

<solid android:color="#FBCCBD";/><corners android:radius="8dp";/></形状>

我尝试使用下面的 snipet 加载形状

Image(painter =painterResource(id = R.drawable.cureved_rect),内容描述 = 空,修饰符 = 修饰符.padding(32.dp).fillMaxWidth().height(200.dp).constrainAs(图像){top.linkTo(parent.top)start.linkTo(parent.start)end.linkTo(parent.end)})

但它不起作用,应用程序崩溃并出现异常java.lang.IllegalArgumentException:仅支持 VectorDrawables 和光栅化资产类型,例如 PNG、JPG1 .如何在撰写中加载可绘制的形状2 .加载合成图像中的颜色资源

解决方案

最好的解决方案是使用 Compose 提供的 Shape.
类似的东西:

盒子(修饰符 = Modifier.size(200.dp).background(颜色(0xFFFBCCBD),形状= RoundedCornerShape(16.dp)))

否则,如果您想使用可绘制资源,您可以使用

I want to set a shape drawable to an image in compose, the drawable look like this cureved_rect.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="rectangle">
    
        <solid android:color="#FBCCBD" />
        <corners android:radius="8dp" />
</shape>

I try the below snipet to load the shape

Image(painter = painterResource(id = R.drawable.cureved_rect),
        contentDescription = null,
        modifier =  Modifier
            .padding(32.dp)
            .fillMaxWidth()
            .height(200.dp)
            .constrainAs(image) {
                top.linkTo(parent.top)
                start.linkTo(parent.start)
                end.linkTo(parent.end)
            })

But it is not working, the app crash with an exception java.lang.IllegalArgumentException:Only VectorDrawables and rasterised asset types are supported ex PNG,JPG 1 . How to load shape drawable in compose 2 . Load colour resource in the compose image

解决方案

The best solution is to use the Shape provided by Compose.
Something like:

Box(
    modifier = Modifier.size(200.dp)
        .background(Color(0xFFFBCCBD), shape = RoundedCornerShape(16.dp))
)

Otherwise if you want to use drawable resource you can use the coil-compose version:

Something like:

Image(
    rememberImagePainter( ContextCompat.getDrawable(context,R.drawable.shape)),
    contentDescription = "Background",
    modifier = Modifier.size(200.dp) 
)

这篇关于如何在合成图像中加载可绘制的形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆