Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 441 Vote(s) - 3.54 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Set Drawable instance to Image in Jetpack Compose

#1
In Jetpack Compose, who can tell me is there a way to assign a Drawable Object to the Image compose view?

I took the apps installed on an Android device. I get an icon with the type that is Drawable and I want to use it in Image

val icon: Drawable = packageInfor.applicationInfo.loadIcon(packageManager)

I found there are 3 functions that can assign images


> - From Painter (IdRes)
> - From ImageBitmap
> - From ImageVector

[![enter image description here][1]][1]

From all that I don't know how to assign a Drawable instance.


[1]:
Reply

#2
You can do like that

val backgroundImage = painterResource(R.drawable.your_image)

and then pass this to your image like that

Image(painter = backgroundImage, contentDescription = null)


That will work.

Reply

#3
If you're using the [Coil library](

[To see links please register here]

), `rememberImagePainter` will accept an `Any?` as its data argument, which includes support for `Drawable` instances. I'm using this as an all-in-one solution for my images rather than importing Accompanist.

```
Image(
painter = rememberImagePainter(data = myDrawableInstance)
)
```
Reply

#4
**Edit**: You should definitely use the [accompanist library][1] as recommended in the Wilson Tran answer. Since it provides support for several kinds of drawables.

If you're not using the accompanist library, you can do the following...
```
ContextCompat.getDrawable(LocalContext.current, R.mipmap.ic_launcher)?.let {
Image(bitmap = it.toBitmap().asImageBitmap(), contentDescription = null)
}
```
You can set the drawable size in `toBitmap` function if you want...


[1]:

[To see links please register here]

Reply

#5
After all, I found a simple solution with Accompanist. Thanks to @Sinner of the System for suggesting to me.

Adding this dependency to the app gradle.

implementation "com.google.accompanist:accompanist-drawablepainter:<version>" //0.28.0

Using:

Image(
painter = rememberDrawablePainter(drawable = drawable),
contentDescription = "content description",
)

Check out: [Drawable Painter¶][1]


[1]:

[To see links please register here]

Reply

#6
We can simply put drawable resource like below:

Icon(painter = painterResource(id = R.drawable.icon_half_rect_croner),contentDescription = null, tint = Color.Red)
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through