I'm trying to set a color as an image to an ImageView that exists inside a custom notification. I can give it predefined colors by writing this line:
remoteViews.setImageViewResource(R.id.imageView, R.color.Green);
But i want to be able to give it hex color values, and set that as the color. I tried setting the color as the background of the ImageView, but i couldn't get access to it, and the only options i have to access the ImageView are these:
remoteViews
.setImageViewResource()
.setImageViewBitmap()
.setImageViewIcon()
.setImageViewUri()
Is there a way to pass in a color by parsing the hex value to any of these? I think if I was able to create a bitmap and set the bitmap to the image view, it could be done, so i wrote the code bellow to create the bitmap:
int[] colors = {Color.parseColor("#64DD17"), Color.parseColor("#D32F2F")};
Bitmap bitmap = Bitmap.createBitmap(colors, 10, 10, Bitmap.Config.RGB_565);
, but i get a java.lang.ArrayIndexOutOfBoundsException . I tried with one element in the colors
array too, but same error. What is my error here?
The int[]
is your image; so you create an image 2px
wide, since that's the length of your array. However, you call createBitmap
and specify that the image is in fact 10px
x 10px
; this is why the ArrayIndexOutOfBoundsException
is thrown, as Android starts looking for pixels that aren't there.
Using Bitmap bitmap = Bitmap.createBitmap(colors, 2, 1, Bitmap.Config.RGB_565);
would solve this problem, albeit generate a very tiny image!
Simple solution: generate png images(or use svg) to put inside the image view, instead of the color you want.
but, if u want to use that approach, you can create a colored bitmap using something like that:
Bitmap image = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
image.eraseColor(android.graphics.Color.GREEN);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.