How to apply a color filter to a view with all children

前端 未结 5 604
误落风尘
误落风尘 2020-12-24 02:45

How do I grey out a view uniformly which contains many different items - ImageViews, TextViews, background image. Do I have to grey out each thing individually? Or is there

5条回答
  •  不知归路
    2020-12-24 02:59

    Unless somebody else has a better answer, my current method is greying out each item separatedly.

    PorterDuffColorFilter greyFilter = new PorterDuffColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
    myLayout.getBackground().setColorFilter(greyFilter);
    myImageView.setColorFilter(greyFilter);
    myTextView.setTextColor(0xff777777);
    

    For more or nested children probably a loop with instanceof would be suitable but I don't need it.

    Edit: This filter isn't actually grey, a better filter is here: Drawable => grayscale Which can be used in the same way.

提交回复
热议问题