创建 ColorFilter 的自定义子类?
好的,这与我之前关于 ColorMatrixColorFilter 的问题有些相关,但我觉得这是一个明显不同的问题。我想知道是否有办法 - 或者更确切地说,如何扩展 ColorFilter 类来创建我自己的自定义滤色器。对于我需要完成的任务,我需要编写一个自定义过滤器来查询每个像素,将其 RGB 值转换为 HSL 或 LAB,修改色调,将其转换回 RGB,然后将该像素设置为新值。
我想我可以简单地编写一个类来执行此操作,接受一个 Drawable 和一定量的色调偏移来执行,但必须为每个 Drawable 以及每个 Drawable 的每个状态手动调用它,而ColorFilter 似乎可以很好地处理这个问题。鉴于 LightingColorFilter 和 ColorMatrixColorFilter 的存在,似乎可以对它进行子类化,但到目前为止,我寻找任何类型文档的努力都是徒劳的。我似乎找不到这三个(Lighting、ColorMatrix、ColorFilter)中任何一个的源代码;我想它们可能是用本机代码完成的?
我的问题是:如何正确子类化 ColorFilter?如果我找不到一个好的答案,如果有人能够找到源代码(我搜索了 Android 的 git)并发布一个链接,那也会很有帮助。
谢谢!
Okay, so this is somewhat related to my previous question on the ColorMatrixColorFilter, but I feel it's a significantly different question. I'm wondering if there's a way - or rather, how to extend the ColorFilter class to create my own custom color filter. For what I'm needing to accomplish, I need to write a custom filter that will query each pixel, convert its RGB value to HSL or LAB, modify the hue, convert it back to RGB, and set that pixel to the new value.
I'm thinking I could simply write a class that does this, taking in a Drawable and an amount of hue shift to perform, but it would have to be called manually for each and every Drawable, and every state of every Drawable, whereas the ColorFilter seems to handle this nicely. Given the existence of the LightingColorFilter and ColorMatrixColorFilter, it seems like it can be subclassed, but so far my efforts to find any sort of documentation have been futile. I can't seem to find the source code for any of the three (Lighting, ColorMatrix, ColorFilter); I'm thinking they're probably done in native code?
My question is this: How can I properly subclass ColorFilter? If I can't find a good answer for that, if anyone is able to find the source (I've searched Android's git) and post a link to that, that would be helpful as well.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所说, 源代码显示 ColorFilter 使用本机代码,因此您无法真正对其进行子类化。
除了为您想做的事情创建自己的类之外,可能没有其他方法了。
As you said, the source code shows ColorFilter use native code so you cannot really subclass it.
There's probably no other way than creating your own class for what you want to do.
您可以使用它来应用您自己的滤色器技术,不幸的是,它适用于 RGB:
在我的情况下,我需要应用 HSL 效果,如 PhotoShop 中的 colorize,它不是 100% 正确,但这给出了很好的结果:
You may use this to apply your own color filter technique unfortunately works on RGB:
Any way in my case I was needed to apply HSL effect like colorize in PhotoShop it is not 100% correct but this gives nice result :