Mad*_*ddy 26 android image androidx android-jetpack-compose
我试过下面的代码,但它在 UI 中没有反映任何东西,我在这里遗漏了什么?
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
loadUi()
}
}
@Composable
fun loadUi() {
CraneWrapper {
MaterialTheme {
Image(
(ResourcesCompat.getDrawable(
resources,
R.mipmap.ic_launcher,
null
) as BitmapDrawable).bitmap
)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Gab*_*tti 31
有了1.0.x 可以使用painterResource的功能:
Image(painterResource(R.drawable.ic_xxxx),"content description")
Run Code Online (Sandbox Code Playgroud)
具有给定 id 的资源必须指向完全光栅化的图像(例如 PNG 或 JPG 文件)或VectorDrawablexml 资产。
这意味着,该方法可以装载的一个实例BitmapPainter或VectorPainter为ImageBitmap分别基于资产或基于向量的资产。
例子:
Image(painterResource(R.drawable.ic_xxxx),"content description")
Run Code Online (Sandbox Code Playgroud)
Cri*_*tan 20
从版本开始1.0.0-beta01:
Image(
painter = painterResource(R.drawable.your_drawable),
contentDescription = "Content description for visually impaired"
)
Run Code Online (Sandbox Code Playgroud)
Tan*_*med 12
尝试这个,但如果你复制代码然后粘贴它,我不知道为什么,但它不起作用,所以只需按原样输入它并替换图像 ID
Image(
painter = painterResource(id = R.drawable.tanjim),
contentDescription = null,
)
Run Code Online (Sandbox Code Playgroud)
在工作 0.1.0-dev14
在 Image 中加载 drawable 可以通过以下方式实现:
Image(
imageResource(id = R.drawable.scene_01),
modifier = Modifier.preferredHeightIn(160.dp, 260.dp)
.fillMaxWidth(),
contentScale = ContentScale.Crop
)
Run Code Online (Sandbox Code Playgroud)
现在,我正在尝试在 Circle Image 中上传 drawable,这听起来很棘手,但在JetPack Compose 中太容易了。您可以通过这种方式实现:
Image(
asset = imageResource(id = R.drawable.scene_01),
modifier = Modifier.drawBackground(
color = Color.Black,
style = Stroke(4f),
shape = CircleShape
).preferredSize(120.dp)
.gravity(Alignment.CenterHorizontally)
.clip(CircleShape),
contentScale = ContentScale.FillHeight
)
Run Code Online (Sandbox Code Playgroud)
输出:
由于imageResource不再可用,解决方案painterResource确实是正确的,例如
Image(painter = painterResource(R.drawable.ic_heart), contentDescription = "content description")
Run Code Online (Sandbox Code Playgroud)
但如果你需要的话,你实际上仍然可以使用 Bitmap 而不是可绘制的:
Image(bitmap = bitmap.asImageBitmap())
Run Code Online (Sandbox Code Playgroud)
.asImageBitmap()是 compose 提供的 Bitmap 的扩展,它从给定的 Bitmap 创建一个 ImageBitmap。
| 归档时间: |
|
| 查看次数: |
19535 次 |
| 最近记录: |