如何将颜色过滤器应用于所有孩子的视图 [英] How to apply a color filter to a view with all children
问题描述
如何使包含许多不同项目的视图统一变灰 - ImageViews、TextViews、背景图像.我是否必须单独将每件事变灰?或者有没有办法对所有人应用相同的颜色过滤器?
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 a way to apply the same color filter to all?
推荐答案
除非其他人有更好的答案,否则我目前的方法是将每个项目单独灰显.
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);
对于更多或嵌套的孩子,可能适合使用 instanceof 的循环,但我不需要它.
For more or nested children probably a loop with instanceof would be suitable but I don't need it.
这个过滤器实际上不是灰色的,更好的过滤器在这里:Drawable =>灰度可以以同样的方式使用.
This filter isn't actually grey, a better filter is here: Drawable => grayscale Which can be used in the same way.
这篇关于如何将颜色过滤器应用于所有孩子的视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!