如何在JetPack组成中将图像从一个合成功能传递到另一个函数?

发布于 2025-01-23 11:47:09 字数 124 浏览 2 评论 0原文

我正在开发一个带有文本和图像的应用程序。图像在一个可复合的(活动)上与文本旁边看起来很小,例如缩略图。这个想法是:当用户触摸(单击)图像时,导航组件将用户带到另一个合并(活动)中的图像的全屏幕版本。有可能吗?如果是,怎么样?提前致谢。

I am developing an app with texts and images. The images appear small, like thumbnails, on one composable (activity), alongside the text. The idea is: when the user touches (clicks) the image, the navigation component takes the user to a full screen version of this image in another composable (activity). Is it at al possible? If yes, how? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明媚殇 2025-01-30 11:47:09

您应该传递某些内容以识别图像,而不是传递图像本身。例如:

  • 如果您是从资源文件夹加载图像,则应传递资源ID(即r.drawable.your_image);
  • 如果您使用资产文件夹或某些远程URL,则应通过Image uri(即/path_in_assets/your_image> https://foo.bar/your_image)。

然后,从“ ListActivity”中,您可以使用Intent Extras将图像ID传递给下一个活动。

startActivity(
    Intent(context, DetailsActivity::class.java).apply {
        putExtra("imageId", yourImageId) // resource ID or image URL
    }
)

在“详细信息”中,在ongreate方法的内部,您将获得图像ID并正确加载它...

class DetailsActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // for uri ID
        val imageId = intent.getStringExtra("imageId")
        // for resource ID
        val imageId = intent.getIntExtra("imageId", -1)

        setContent {
            YourAppTheme {
                YourScreen(imageId)
            }
        }
    }
}

最后,在合并中,您可以使用以下方式加载基于资源ID的图像:

Image(painter = painterResource(id = imageId), contentDescription = null)

或使用< a href =“ https://coil-kt.github.io/coil/compose/”

Image(painter = rememberAsyncImagePainter(imageId), contentDescription = null)

Instead of passing the image itself, you should pass something to identify the image. For instance:

  • If you're loading the image from the resource folder, you should pass the resource ID (i.e. R.drawable.your_image);
  • If you're using the assets folder or some remote URL, you should pass the image Uri (i.e. /path_in_assets/your_image or https://foo.bar/your_image).

Then from the "ListActivity" you can pass the image ID to next activity using the Intent extras.

startActivity(
    Intent(context, DetailsActivity::class.java).apply {
        putExtra("imageId", yourImageId) // resource ID or image URL
    }
)

in "DetailsActivity", inside of the onCreate method, you will get image ID and load it properly...

class DetailsActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // for uri ID
        val imageId = intent.getStringExtra("imageId")
        // for resource ID
        val imageId = intent.getIntExtra("imageId", -1)

        setContent {
            YourAppTheme {
                YourScreen(imageId)
            }
        }
    }
}

finally, in your composable, you can load an image based on resource ID using:

Image(painter = painterResource(id = imageId), contentDescription = null)

or using Coil for URL resource:

Image(painter = rememberAsyncImagePainter(imageId), contentDescription = null)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文