happydude jest najbardziej elegancki sposób, aby sobie z tym poradzić, ale niestety (jak wskazano w komentarzach) kod źródłowy ImageView akceptuje tylko liczbę całkowitą (Solid Color). Issue 18220 już od paru lat adresowania tego, jakie napisali obejście tam, że będę streszczać tutaj:
Extend ImageView i owinąć drawableStateChanged() z kodem, który ustawia odcień na podstawie nowego stanu:
TintableImageView.java
package com.example.widgets;
import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.support.v7.widget.AppCompatImageView;
import com.example.R;
public class TintableImageView extends AppCompatImageView {
private ColorStateList tint;
public TintableImageView(Context context) {
super(context);
}
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);
}
private void init(Context context, AttributeSet attrs, int defStyle) {
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintableImageView, defStyle, 0);
tint = a.getColorStateList(R.styleable.TintableImageView_tintColorStateList);
a.recycle();
}
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
if (tint != null && tint.isStateful())
updateTintColor();
}
private void updateTintColor() {
int color = tint.getColorForState(getDrawableState(), 0);
setColorFilter(color);
}
}
zdefiniować atrybut niestandardowy:
attrs.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="TintableImageView">
<attr name="tintColorStateList" format="reference|color" />
</declare-styleable>
</resources>
Użyj widget i atrybut niestandardowy z lokalnej przestrzeni nazw zamiast Androida:
example_layout.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.example.widgets.TintableImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/example"
android:clickable="true"
app:tintColorStateList="@color/color_selector"/>
</LinearLayout>
Następnie można użyć selektora kolorów jak happydude zasugerował:
color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="@color/pressed_color"/>
<item android:color="#00000000"/>
</selector>
I wypróbowałem twoją sugestię. Wydaje się, że nic nie robi. Używając debuggera zauważyłem, że metoda drawableStateChanged() nie jest wyzwalana w onPress, ale wydaje się, że wyzwala tylko onClick. – Abhishek
Hmm, nie widzę żadnych błędów w powyższym kodzie. Z definicji metoda powinna zostać wywołana, gdy wciśnięty stan ulegnie zmianie. Używam tej konstrukcji w moim własnym kodzie i działa dobrze. Jako test, w swoim ImageView XML dodaj 'android: tint =" @ color/pressed_color "'. Powinno to dodać trwały odcień do obrazu, ponieważ wywołuje tę samą metodę ustawiania filtra koloru jak powyższy kod. W ten sposób możesz przynajmniej wykluczyć problem z wybranym kolorem. – happydude
Przepraszam, jestem głupi. Możesz bezpośrednio dodać swoją listę stanów kolorów bezpośrednio do pliku XML 'ImageView' bezpośrednio. Zobacz edycję powyżej. – happydude