触摸时可着色/变暗
我目前正在开发的应用程序使用大量 ImageView 作为按钮。这些按钮上的图形使用 Alpha 通道淡出按钮的边缘,使它们看起来不规则。目前,我们必须为每个按钮生成 2 个图形(1 个用于选定/聚焦/按下状态,另一个用于默认的未选定状态),并为每个按钮使用 XML 文件中定义的 StateListDrawable。
虽然这工作得很好,但似乎非常浪费,因为所有选定的图形都只是未选定按钮的着色版本。这些需要时间来生成(无论多短)并占用最终 APK 的空间。似乎应该有一个简单的方法来自动实现这一点。
完美的解决方案似乎是为每个按钮使用 ImageView,并在其色调属性中指定 ColorStateList。此方法的优点是所有按钮(共享相同色调)只需要一个 XML ColorStateList。然而它不起作用。正如此处所述,当提供给色调的值不是单一颜色时,ImageView 会抛出 NumberFormatException。
我的下一个尝试是对选定的可绘制对象使用 LayerDrawable。在图层列表内,我们将在堆栈底部放置原始图像,并用半透明矩形覆盖。这适用于按钮图形的实体部分。然而,原本应该完全透明的边缘现在与顶层的颜色相同。
有没有人遇到过这个问题并找到合理的解决方案?我想坚持使用 XML 方法,但可能会编写一个简单的 ImageView 子类,该子类将在代码中进行所需的着色。
The app I'm currently working on uses a lot of ImageViews as buttons. The graphics on these buttons use the alpha channel to fade out the edges of the button and make them look irregular. Currently, we have to generate 2 graphics for each button (1 for the selected/focused/pressed state and the other for the default unselected state) and use a StateListDrawable defined in an XML file for each button.
While this works fine it seems extremely wasteful since all of the selected graphics are simply tinted versions of the unselected buttons. These take time to produce (however little) and take up space in the final APK. It seems like there should be an easy way to this automatically.
The perfect solution, it would seem, is to use ImageViews for each button and specify in its tint attribute a ColorStateList. This approach has the advantage that only a single XML ColorStateList is needed for all of the buttons (that share the same tint). However it does not work. As mentioned here, ImageView throws a NumberFormatException when the value provided to tint is anything other than a single color.
My next attempt was to use a LayerDrawable for the selected drawable. Inside the layer list, we would have the original image at the bottom of the stack covered by a semi-transparent rectangle. This worked on the solid parts of the button graphic. However the edges which were supposed to be entirely transparent, were now the same color as the top layer.
Has anyone encountered this issue before and found a reasonable solution? I would like to stick to XML approaches but will probably code up a simple ImageView subclass that will do the required tinting in code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
对于那些遇到类似需求的人来说,用代码解决这个问题是相当干净的。这是一个示例:
它尚未完成,但可以很好地作为概念证明。
For those who have encountered a similar need, solving this in code is fairly clean. Here is a sample:
It's not finished but works well as a proof of concept.
您可以为此组合
StateListDrawable
和LayerDrawable
。我假设我们的 color.xml 看起来像这样
You can combine
StateListDrawable
andLayerDrawable
for this.I assume our colors.xml looks like this
您的回答非常好:)
但是,我没有创建按钮对象,而是仅调用 OnTouchlistener 来更改每个按钮的状态看法。
但给出了相同的结果
Your answer was excellent :)
However instead of creating a button object, I am only calling an OnTouchlistener to change the state of every view.
Gives the same results though
我认为这个解决方案很简单!
步骤 1:创建 res/drawable/dim_image_view.xml
步骤 2:创建 res/drawable/dim_image_view_normal.xml
步骤 3:创建 res/drawable/dim_image_view_pressed.xml
步骤 4:尝试将 xml 布局中的图像设置为:
I think this solution is simple!
Step 1: Create res/drawable/dim_image_view.xml
Step 2: Create res/drawable/dim_image_view_normal.xml
Step 3: Create res/drawable/dim_image_view_pressed.xml
Step 4: Try setting the image in xml layout as:
我也有看起来不规则的按钮,需要色调。最后,我只是采用了状态颜色列表作为我的 ImageButton 背景。给人的印象是按钮颜色在按下时会发生变化,并且非常简单且计算强度较低。我知道这并不完全是一种色调,但它确实在通常转瞬即逝的时刻向用户提供了必要的视觉反馈。
I too have irregular looking buttons and needed the tint. In the end, I just resorted to having a state colorlist as my
ImageButton
background. Gives the impression that the button color is changing on press and is very straightforward and less compute intensive. I know that this isn't exactly a tint, but it does give it does give necessary visual feedback to the user in what usually is a fleeting moment.您可以使用我创建的类 https://github.com/THRESHE/TintableImageButton
不完全是最好的解决方案,但它有效。
You can use a class I've created https://github.com/THRESHE/TintableImageButton
Not exactly the best solution but it works.
你的回答也很好;)
我正在修改一点,它会给你这样的结果:
Your answer was also excellent ;)
I am modifying little bit, it will give you result like this:
我认为这个解决方案很简单:
I think this solution is simple enough: