Android:使用选择器更新自定义颜色属性

时间:2017-01-04 14:05:23

标签: java android xml android-selector

我想根据自定义状态定义在绘制到画布时应使用的颜色。这是我得到了多远:

在res / layout / content.xml中:

<com.example.package.MyView
    app:primary_color="@drawable/my_selector"
/>

primary_color是res / values / attrs.xml中定义的自定义属性:

<resource>
    <declare-styleable name="MyView">
        <attr name="primary_color" format="reference"/>
    </declare-styleable>
</resource>

my_selector在res / drawable / my_selector.xml

中定义
<selector xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res/com.example.package">
    <item
        app:state_a="true"
        android:drawable="@drawable/red" />
    <item
        app:state_b="true"
        android:drawable="@drawable/orange" />
    <item
        app:state_c="true"
        android:drawable="@drawable/red" />
</selector>

红色,橙色和红色在res / values / colordrawable.xml中定义:

<resources>
    <drawable name="red">#f00</drawable>
    <drawable name="orange">#fb0</drawable>
    <drawable name="green">#0f0</drawable>
</resources>

在MyView中我可以得到这个可绘制的内容:

StateListDrawable primaryColor;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);

    try{

        primaryColor = (StateListDrawable) a.getDrawable(
            R.styleable.MyView_primary_color);
    }finally {
        a.recycle();
    }
}

primaryColor使用不同的状态正确更新,我可以通过调用:

来测试它
setBackground(primaryColor);

但我想在Paint上使用这种颜色,如下所示:

paint.setColor(primaryColor);

但显然不允许这样做。我尝试将primaryColor转换为具有方法getColor()的ColorDrawable,但如果可能的话,我无法弄清楚如何执行此操作。

有关如何从选择器获取可在视图中使用的颜色的任何建议都会令人惊叹。

1 个答案:

答案 0 :(得分:0)

我发现ColorStateList结果正是我所需要的。以下是我当前实现的简化版本,以防其他人和我一样进入同样的道路。

在res / color / my_selector.xml

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

  <item app:state_weak="true" android:color="#F00" />
  <item app:state_average="true" android:color="#0F0" />
  <item app:state_strong="true" android:color="#00F" />
  <item android:color="#FA0" />
</selector>

在res / layout / content.xml中(这周围有另一个布局,但这不相关)

<com.example.package.MyView
    android:id="@+id/strMeter"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:primary_color="@color/my_selector"
    />

primary_color被定义为res / values / attrs.xml

中的引用
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="MyView">
        <attr name="primary_color" format="reference"/>
    </declare-styleable>
</resources>

我在MyView的构造函数中获得了对ColorStateList的引用:

ColorStateList primaryColor;

public PasswordStrengthBar(Context context, AttributeSet attrs) {
    super(context, attrs);

    try{
        primaryColor = a.getColorStateList(
            R.styleable.MyView_primary_color);
    }finally {
        a.recycle();
    }
}

当我想获得当前状态的颜色时:

int color = secondaryColor.getColorForState(
              getDrawableState(), primaryColor.getDefaultColor());

如果您像我一样实现自定义状态,那么您还必须覆盖onCreateDrawableState以实际更新状态,但是有大量文档/帖子可以覆盖它。

相关问题