简体   繁体   中英

Invert text color of TextView depending on background color?

is there a way to invert the color of my text view depending on my background colors?

I am thinking of setting a black to white gradient background and have my TextView scrolled through it. I noticed that I could not have a single text color for my TextView.

For example, If I set the text color to black, it will not be shown when over black background, same goes for white text on white background.

Is there a property or method I can use to quickly show the text in the opposite color of my background?

It's not that simple but yes, technically you can do this.

First you'll need to set up an onScrollListener on your scrollable view (eg. ListView , GridView , ScrollView

in the onScrollListener() , you will need to implement the onScroll() method.

onScroll (AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount)

At this point you know that the use is scrolling. You can then check where on the screen your TextView mTextView is.

int distanceFromTop = mTextView.getTop();

and change you text color depending on the distance from the top.

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