简体   繁体   中英

Android How to change text color according to background image

On Android, I have a header with a background image (Random image according to API). On this header I have texts with some data. My text is every time black but sometimes image is black too. So, we can't see the text.

I'm looking for library or snippet for resolve this problem. Thanks.

I encoutered this problem months ago and was not really sure how to approach it. First of all you need to use a Layout where you can put View over View for example Relative or Frame. After that you need to make the ImageView thats behind the TextView to be a little bit Lighter or Darker (like a shade) at the place of your TextView so you can choose a color for your text which will always be readable since the shade will be in contrast with the text. What you can do is put something behind the TextView and the ImageView which will be Light or Dark and make the ImageView a little bit transparent using set.alpha(int) if i remember correctly. So at this point you will have transparent image with a light or dark rectangle behind it. It will be visible that the part where you have the rectangle is darker/lighter. Then you put your TextView there with contrast color to the Rectangle and you will always be able to see it. It is kind of complicated, but it will work. Hope it helps.

I think palatte is not available for android. So glide will be a better option. Link

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM