如何使用 Jetpack Compose 在 Android 中添加图像
How to add Image in Android using Jetpack Compose
我正在尝试使用 Android Jetpack Compose 将图像添加到 activity,但出现错误:
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Image(bitmap = imageFromResource(res = resources, resId =R.drawable.ic_launcher_background))
}
}
}
这是解决此问题的另一个替代工作代码:
代码:
Image(
painter = painterResource(R.drawable.happy_meal_small),
contentDescription = null
)
输出:
这两个都可以用来获取图片资源。
使用 painterResource API 加载矢量绘图或 PNG 等光栅化资源格式。您不需要知道可绘制对象的类型,只需使用 painterResource。
import androidx.compose.ui.res.painterResource
Image(painterResource(id = imageResource), contentDescription = contentDescription)
或
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
Image(ImageBitmap.imageResource(id = imageResource), contentDescription = contentDescription)
或
import androidx.compose.ui.res.vectorResource
Image(ImageVector.vectorResource(id = imageResource), contentDescription = contentDescription)
大多数情况下本地图片加载可以使用painterResource在Image
中完成
例如:
Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "")
或者,如果您有兴趣更改图像资源的颜色,请使用 Icon 和 painterResource
Icon(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "", tint = Color.Red)
或者如果你想从 远程加载 URL 然后使用 Coil
添加依赖:
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.6.1"
然后像下面这样使用它:
CoilImage(
data = "https://www.instaily.com/images/android.jpg",
contentDescription = "android",
alignment = Alignment.TopCenter,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(.60f),
contentScale = ContentScale.Crop,
loading = {
Box(
modifier = Modifier.background(
shape = RoundedCornerShape(20.dp),
color = Teal200
)
)
},
error = {
Box(
modifier = Modifier.background(
shape = RoundedCornerShape(20.dp),
color = Teal200
)
)
}
)
我正在尝试使用 Android Jetpack Compose 将图像添加到 activity,但出现错误:
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.Image
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Image(bitmap = imageFromResource(res = resources, resId =R.drawable.ic_launcher_background))
}
}
}
这是解决此问题的另一个替代工作代码:
代码:
Image(
painter = painterResource(R.drawable.happy_meal_small),
contentDescription = null
)
输出:
这两个都可以用来获取图片资源。
使用 painterResource API 加载矢量绘图或 PNG 等光栅化资源格式。您不需要知道可绘制对象的类型,只需使用 painterResource。
import androidx.compose.ui.res.painterResource
Image(painterResource(id = imageResource), contentDescription = contentDescription)
或
import androidx.compose.ui.graphics.ImageBitmap
import androidx.compose.ui.res.imageResource
Image(ImageBitmap.imageResource(id = imageResource), contentDescription = contentDescription)
或
import androidx.compose.ui.res.vectorResource
Image(ImageVector.vectorResource(id = imageResource), contentDescription = contentDescription)
大多数情况下本地图片加载可以使用painterResource在Image
中完成例如:
Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "")
或者,如果您有兴趣更改图像资源的颜色,请使用 Icon 和 painterResource
Icon(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "", tint = Color.Red)
或者如果你想从 远程加载 URL 然后使用 Coil
添加依赖:
implementation "dev.chrisbanes.accompanist:accompanist-coil:0.6.1"
然后像下面这样使用它:
CoilImage(
data = "https://www.instaily.com/images/android.jpg",
contentDescription = "android",
alignment = Alignment.TopCenter,
modifier = Modifier
.fillMaxWidth()
.fillMaxHeight(.60f),
contentScale = ContentScale.Crop,
loading = {
Box(
modifier = Modifier.background(
shape = RoundedCornerShape(20.dp),
color = Teal200
)
)
},
error = {
Box(
modifier = Modifier.background(
shape = RoundedCornerShape(20.dp),
color = Teal200
)
)
}
)