如何使用选择器在android中着色imageview

时间:2013-10-21 16:36:25

标签: android android-imageview android-selector

我想使用xml为我的tabhost图标着色,而不是以编程方式进行(我无论如何都无法做到这一点)...所以我在SO上找到了这个帖子:Android imageview change tint to simulate button click

这似乎是一个非常好的解决方案,但我无法在我的项目中正确调整它...我做了以下更改:

public class TintableImageView extends ImageView {
private ColorStateList tint;

public TintableImageView(Context context) {
    super(context);
}

//this is the constructor that causes the exception
public TintableImageView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs, 0);
}

public TintableImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init(context, attrs, defStyle);
}

//here, obtainStyledAttributes was asking for an array
private void init(Context context, AttributeSet attrs, int defStyle) {
    TypedArray a = context.obtainStyledAttributes(attrs, new int[]{R.styleable.TintableImageView_tint}, defStyle, 0);
    tint = a.getColorStateList(R.styleable.TintableImageView_tint);
    a.recycle();
}

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (tint != null && tint.isStateful())
        updateTintColor();
}

public void setColorFilter(ColorStateList tint) {
    this.tint = tint;
    super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}

private void updateTintColor() {
    int color = tint.getColorForState(getDrawableState(), 0);
    setColorFilter(color);
}

}

我也无法在@drawable/selector.xml引用android:tint,所以我在colors.xml上执行了此操作:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="azulPadrao">#2e7cb4</color>
<drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable>
</resources>

我的选择器:

<?xml version="1.0" encoding="utf-8"?>

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:tint="#007AFF" />
<item android:state_focused="true" android:tint="#007AFF" />
<item android:state_pressed="true" android:tint="#007AFF" />
<item android:tint="#929292" />
</selector>

我的标签布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:id="@+id/TabLayout"
          android:layout_width="fill_parent" android:layout_height="fill_parent"
          android:gravity="center" android:background="@drawable/tab_bg_selector">

<com.myapp.TintableImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView" android:layout_gravity="center" android:tint="@drawable/tab_icon_selector"/>
<TextView android:id="@+id/TabTextView" android:text="Text"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content" android:textColor="@drawable/tab_text_selector"
          android:textSize="10dip"
          android:textStyle="bold" android:layout_marginTop="2dip"/>

</LinearLayout>

有什么建议吗?提前致谢

[编辑]我使用NumberFormatException获得了android:tint,当正确为app:tint时(设置xmlns:app="http://schemas.android.com/apk/res/com.myapp"后)...但现在我想我我以错误的方式使用我的选择器,因为图标都是黑色的,无论状态如何...... 我已尝试在colors.xml中设置<drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable>,但无法正常工作

[/编辑]

6 个答案:

答案 0 :(得分:30)

如果您使用的是API 21+,则可以使用选择器和tint在XML中轻松完成此操作:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_activated="true">
        <bitmap android:src="@drawable/ic_settings_grey"
                android:tint="@color/primary" />
    </item>

    <item android:drawable="@drawable/ic_settings_grey"/>
</selector>

答案 1 :(得分:19)

我是使用Android support-v4库中的DrawableCompat实现的。

使用常规ImageButton(子类ImageView,因此此信息也适用于ImageView s),使用material icons collection中的黑色图标:

<ImageButton
  android:id="@+id/button_add"
  android:src="@drawable/ic_add_black_36dp"
  android:background="?attr/selectableItemBackgroundBorderless"
  android:contentDescription="@string/title_add_item" />

这是我创建的实用方法:

public static void tintButton(@NonNull ImageButton button) {
    ColorStateList colours = button.getResources()
            .getColorStateList(R.color.button_colour);
    Drawable d = DrawableCompat.wrap(button.getDrawable());
    DrawableCompat.setTintList(d, colours);
    button.setImageDrawable(d);
}

res/color/button_colour.xml是一个选择器,当按下按钮时,它会将图标颜色从红色变为半透明红色:

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

    <item
      android:state_pressed="false"
      android:color="@color/red" />

    <item
      android:color="@color/red_alpha_50pc" />

</selector>

在我的活动的ImageButton方法中onCreate()被夸大后,我只需为每个按钮调用tintButton(...)辅助方法一次。

我已在Android 4.1(我的minSdkVersion)和5.0设备上对此进行了测试,但DrawableCompat应该可以恢复到Android 1.6。

答案 2 :(得分:11)

在我https://stackoverflow.com/a/18724834/2136792的解决方案中,有一些你不知道的事情:

<强> TintableImageView.java

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (tint != null && tint.isStateful())
        updateTintColor();
}

public void setColorFilter(ColorStateList tint) {
    this.tint = tint;
    super.setColorFilter(tint.getColorForState(getDrawableState(), 0));
}

private void updateTintColor() {
    int color = tint.getColorForState(getDrawableState(), 0);
    setColorFilter(color);
}
必须覆盖

drawableStateChanged(),以便在元素状态发生变化时更新色调。

我不确定引用drawable中的drawable是否会导致问题,但您只需将selector.xml移动到文件夹“/ res / color”中即可使用“@ color / selector.xml”引用它(aapt合并/res/values/colors.xml和/ res / color文件夹)。

答案 3 :(得分:10)

使用支持库22.1,我们可以使用DrawableCompat来绘制可绘制的API级别4 +

  

DrawableCompat.wrap(Drawable)和setTint(),setTintList()和setTintMode()将起作用:不需要创建和维护单独的drawable只支持多种颜色!

答案 4 :(得分:7)

我同意Code中的@Dreaming,我会举一个例子。

ic_up_small

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:color="@color/comment_count_selected_color" android:state_selected="true" />
    <item android:color="@color/comment_count_text_color"/>

</selector>

布局/ item_post_count_info.xml

<android.support.v7.widget.AppCompatImageView
    android:id="@+id/post_upvote_icon"
    android:layout_width="14dp"
    android:layout_height="14dp"
    android:layout_marginLeft="17dp"
    app:srcCompat="@drawable/ic_up_small"
    app:tint="@color/post_up_color"/>

注意:我们应该使用 app:tint 而不是 android:tint

我的支持库版本是26.0.2。

应用程序/的build.gradle

implementation 'com.android.support:appcompat-v7:26.0.2'
implementation 'com.android.support:support-core-utils:26.0.2'
implementation 'com.android.support:support-annotations:26.0.2'
implementation 'com.android.support:support-v4:26.0.2'
implementation 'com.android.support:design:26.0.2'

如果我们使用android:tint,它会崩溃并且日志是这样的:

  

E / AndroidRuntime:致命异常:主要                                                                     android.view.InflateException:二进制XML文件行#0:错误   膨胀班                                                                         在android.view.LayoutInflater.createView(LayoutInflater.java:613)                                                                         在   android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)                                                                         在android.view.LayoutInflater.rInflate(LayoutInflater.java:746)                                                                         在android.view.LayoutInflater.inflate(LayoutInflater.java:489)                                                                         在android.view.LayoutInflater.inflate(LayoutInflater.java:396)                                                                         在   com.opera.six.viewholder.post.PostCountInfoViewHolder $ 1。创建(PostCountInfoViewHolder.java:29)                                                                         在   com.opera.six.viewholder.post.PostCountInfoViewHolder $ 1。创建(PostCountInfoViewHolder.java:25)                                                                         在   com.opera.six.collection.CollectionAdapter.onCreateViewHolder(CollectionAdapter.java:39)                                                                         在   com.opera.six.collection.CollectionAdapter.onCreateViewHolder(CollectionAdapter.java:19)                                                                         在   android.support.v7.widget.RecyclerView $ Adapter.createViewHolder(RecyclerView.java:6493)                                                                         在   android.support.v7.widget.RecyclerView $ Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5680)                                                                         在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5563)                                                                         在   android.support.v7.widget.RecyclerView $ Recycler.getViewForPosition(RecyclerView.java:5559)                                                                         在   android.support.v7.widget.LinearLayoutManager $ LayoutState.next(LinearLayoutManager.java:2229)                                                                         在   android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1556)                                                                         在   android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1516)                                                                         在   android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:608)                                                                         在   android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3693)                                                                         在   android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3410)                                                                         在   android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3962)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在   android.support.v4.widget.SwipeRefreshLayout.onLayout(SwipeRefreshLayout.java:610)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在   android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:132)                                                                         在   android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)                                                                         在   android.support.design.widget.AppBarLayout $ ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1361)                                                                         在   android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:869)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)                                                                         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)                                                                         在android.widget.LinearLayout.onLayout(LinearLayout.java:1420)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)                                                                         在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)                                                                         在android.widget.LinearLayout.onLayout(LinearLayout.java:1420)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(ViewGroup.java:4364)                                                                         在android.widget.FrameLayout.onLayout(FrameLayout.java:448)                                                                         在android.view.View.layout(View.java:13754)                                                                         在android.view.ViewGroup.layout(Vi

答案 5 :(得分:3)

使用当前的AppCompat支持库,您可以在app:tint标记上使用ImageView,该标记将被充实为AppCompatImageView并正确处理状态更改。

AppCompatImageView中,您可以看到状态更改已通知mImageHelper

@Override
protected void drawableStateChanged() {
    super.drawableStateChanged();
    if (mBackgroundTintHelper != null) {
        mBackgroundTintHelper.applySupportBackgroundTint();
    }
    if (mImageHelper != null) {
        mImageHelper.applySupportImageTint();
    }
}

Android Studio目前会对此发出警告,但您可以放心地禁止它。