During development, we may add a gradient effect to an image, which can be achieved by adding a Filter to the ImageView.
First, define a color array and add the following code to arrays. xml:
- <array name="filter_colors">
- <item>#96eeeeee</item>
- <item>#86eeeeee</item>
- <item>#76eeeeee</item>
- <item>#56eeeeee</item>
- <item>#46eeeeee</item>
- <item>#36eeeeee</item>
- <item>#26eeeeee</item>
- <item>#16eeeeee</item>
- <item>#06eeeeee</item>
- </array>
Then define a Handler and add a filter to the ImageView in Handler:
- Handler mHandler = new Handler() {
- public void handleMessage(Message msg) {
- switch (msg.what) {
- case 0:
- mHandler.sendEmptyMessageDelayed(0,
- 1000);
- if(mProgress%10==1){
- int index=mProgress/10;
- int color = mFilterColors.getColor(index,0);
- PorterDuffColorFilter colorFilter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
- mImg.setColorFilter(colorFilter);
- mProgress++;
- }
- }
- }
- }