最佳的按钮着色方法是什么?

3
我正在开发一款Android应用程序,其中我使用透明的PNG作为用户界面的按钮。

这些按键看起来有点像这样:

enter image description here

当用户按下按钮时,我希望自动将图像中不透明的像素着色为较暗的颜色。

目前,我使用带有不同绘制状态的XML选择器。但很明显,这种方法无法很好地扩展,因为我需要在Photoshop中制作每个图像的几个版本。

有什么解决方案吗?我听说可以使用ImageView的setColorFilter方法来实现这一点,但是能否提供详细说明呢?

谢谢!

1个回答

2

将该图像设置为ImageButton的源,并使用xml状态列表设置ImageButton的状态(背景),类似于以下内容:

<?xml version="1.0" encoding="utf-8"?>
<selector
    xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true">
        <shape
            >

            <gradient
                android:startColor="@android:color/background_light"
                android:endColor="@android:color/darker_gray"
                android:angle="90"/>
             <stroke
                android:width="1dp"
                android:color="#999999" />
            <corners
                android:radius="9dp"/>
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"/>

        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <gradient
                android:endColor="@android:color/transparent"
                android:startColor="@android:color/transparent"
                android:angle="270"/>
            <stroke
                android:width="1dp"
                android:color="#989797" />
            <corners
                android:radius="9dp"/>
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"/>
        </shape>
    </item>

    <item>        
        <shape>
            <gradient
                android:endColor="@android:color/transparent"
                android:startColor="@android:color/transparent"
                android:angle="90"/>
             <stroke
                android:width="1dp"
                android:color="#999999" />
            <corners
                android:radius="9dp"/>
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp"/>
        </shape>
    </item>
</selector>

我得说,那看起来非常不错,但不是我想要的东西。这将背景填充为灰色,我希望图像中不透明的绿色像素变暗。 - monoceres
@monoceres,这真的很难...我认为用Photoshop创建图像是一种方法。不知道是否还有其他方法。 - ngesh
@monoceres,请看一下这两个答案:https://dev59.com/w3M_5IYBdhLWcg3wmkUK 和 http://stackoverflow.com/questions/5702521/android-button-setcolorfilter-behaviour。 - ngesh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接