我有一个imageview,我已经在其中设置了从网址获取的位图。 在imageview上,我设置了一个打开对话框的onClickListener。
我想在按下imageview时以某种方式改变色调(使其变暗),以提供一种按钮点击感觉。
你有什么建议?
答案 0 :(得分:99)
happydude的答案是处理这个问题的最优雅的方法,但不幸的是(正如评论中所指出的)ImageView的源代码只接受整数(纯色)。 Issue 18220已经解决了这个问题几年了,我在那里发布了一个解决方法,我将在这里总结一下:
扩展ImageView并使用基于新状态设置色调的代码包装drawableStateChanged():
<强> 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);
}
}
定义自定义属性:
<强> attrs.xml 强>
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<declare-styleable name="TintableImageView">
<attr name="tintColorStateList" format="reference|color" />
</declare-styleable>
</resources>
将widget和custom属性与您的本地命名空间一起使用,而不是Android的:
<强> 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>
然后你可以使用像happydude建议的颜色选择器:
<强> 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>
答案 1 :(得分:6)
一种方法是使用ColorFilter
和ColorStateList
的组合,其中包含按下按钮时的色调颜色。 res / color目录中ColorStateList
的xml如下所示:
button_pressed.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>
其中@color/pressed_color
是您的色调(应该是部分透明的)。然后在ImageView
子类中,通过覆盖drawableStateChanged()
来应用颜色。
@Override
protected void drawableStateChanged() {
super.drawableStateChanged();
ColorStateList list = getResources().getColorStateList(R.color.button_pressed);
int color = list.getColorForState(getDrawableState(), Color.TRANSPARENT);
setColorFilter(color);
invalidate();
}
只要按钮的状态发生变化,就会调用此代码并自动设置适当的色调。
答案 2 :(得分:0)
我必须测试它,但是您应该能够将该行为设置为ImageView drawable,然后将您的位图设置为ImageView背景。
答案 3 :(得分:0)
对我来说,一个简单的解决方案是工作,在 onClick 事件中使用 setAlpha(180)使图像变暗,为用户提供点击或触摸的反馈。
final ImageView myImage = (ImageView) findViewById(R.id.ivDocument);
myImage.setImage...(... your image ...); // load your ImageView
myImage.setClickable(true);
myImage.setFocusable(true);
myImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
myImage.setAlpha(180);
doWhateverYouWantHere(v);
}
});
关于您的XML布局,没什么特别的。
答案 4 :(得分:-1)
此代码段对我有用:
porterDuffColorFilter = newPorterDuffColorFilter(getResources().getColor(R.color.cardview_dark_background),PorterDuff.Mode.MULTIPLY);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);