【问题标题】:How to add Image in Android using Jetpack Compose [duplicate]如何使用 Jetpack Compose 在 Android 中添加图像 [重复]
【发布时间】:2021-03-08 00:28:44
【问题描述】:

我正在尝试使用 Android Jetpack Compose 将图像添加到活动中,但它给出了错误:

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))
        }
    }
}


【问题讨论】:

    标签: android android-jetpack android-jetpack-compose


    【解决方案1】:

    本地图片加载的大部分情况都可以使用Image中的painterResource来完成

    例如:

    Image(painter = painterResource(id = R.drawable.ic_launcher_background), contentDescription = "")
    

    或者,如果您有兴趣更改图像资源的颜色,则使用 IconpainterResource

    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
                            )
                        )
                    }
                )
    

    【讨论】:

      【解决方案2】:

      其中任何一个都可以用来获取图片资源。

      使用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)
      

      【讨论】:

        【解决方案3】:

        这是解决此问题的另一个替代工作代码:

        代码:

             Image(
                  painter = painterResource(R.drawable.happy_meal_small),
                  contentDescription = null
              )
        

        输出:

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-07-01
          • 1970-01-01
          • 2023-04-02
          • 2022-10-25
          • 1970-01-01
          • 2019-11-08
          • 2021-11-08
          • 2020-11-06
          相关资源
          最近更新 更多