Android的imageview改变色调模拟button点击

我有一个imageview,我已经设置了从url获取的位图。 在imageview上,我设置了一个onClickListener,打开一个对话框。

我想以某种方式改变颜色(使其更深),当按下图像视图提供一种button点击感觉。

你有什么build议?

happydude的答案是最优雅的方式来处理这个,但不幸的是(正如在评论中指出的)ImageView的源代码只接受一个整数(纯色)。 问题18220已经出现几年了,我已经发布了一个解决方法,我将在这里总结一下:

扩展ImageView并将drawableStateChanged()与包含基于新状态的tint设置的代码包装在一起:

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.widget.ImageView; import com.example.R; public class TintableImageView extends ImageView { 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_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); } } 

定义一个自定义属性:

attrs.xml

 <?xml version="1.0" encoding="UTF-8"?> <resources> <declare-styleable name="TintableImageView"> <attr name="tint" format="reference|color" /> </declare-styleable> </resources> 

使用你的本地命名空间,而不是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" app:tint="@color/color_selector"/> </LinearLayout> 

然后你可以使用像happydude这样的颜色select器:

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> 

一种方法是使用一个ColorFilter和一个ColorStateList的组合,当button被按下时,它包含你的色调颜色。 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(); } 

任何时候button的状态改变,这个代码被调用,并会自动设置适当的色调。

我不得不testing一下,但是你应该能够把这个行为设置为ImageView的drawable,然后把你的位图设置为ImageView的背景。

对我来说,一个简单的解决scheme正在工作,在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布局,没有什么特别的。

这个代码片段为我工作:

 porterDuffColorFilter = newPorterDuffColorFilter(getResources().getColor(R.color.cardview_dark_background),PorterDuff.Mode.MULTIPLY); imgView.getDrawable().setColorFilter(porterDuffColorFilter); imgView.setBackgroundColor(Color.TRANSPARENT);