【发布时间】:2012-06-21 03:40:40
【问题描述】:
我有一个图像视图,我在其上设置了从 url 获取的位图。 在 imageview 上,我设置了一个 onClickListener,它会打开一个对话框。
我想在按下 imageview 时以某种方式更改色调(使其更暗)以提供一种按钮点击的感觉。
你有什么建议?
【问题讨论】:
标签: android android-imageview android-ui
我有一个图像视图,我在其上设置了从 url 获取的位图。 在 imageview 上,我设置了一个 onClickListener,它会打开一个对话框。
我想在按下 imageview 时以某种方式更改色调(使其更暗)以提供一种按钮点击的感觉。
你有什么建议?
【问题讨论】:
标签: android android-imageview android-ui
happydude 的回答是处理这个问题的最优雅的方式,但不幸的是(正如 cmets 中所指出的)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>
将小部件和自定义属性与您的本地命名空间一起使用,而不是 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>
【讨论】:
TintableImageView 中的构造函数调用它的超级时仍然得到java.lang.NumberFormatException: Invalid int: "@2130837701"...我需要通过new int[]{R.styleable.TintableImageView_tint} 因为obtainStyledAttributes 要求一个数组,并在 colors.xml 中声明 <drawable name="tab_icon_selector">@drawable/tab_icon_selector</drawable> 以便能够从 android:tint 引用它
app:tint 并且异常消失了...但是仍然没有使用选择器,即我的图标一直是黑色的...是我在colors.xml中的可绘制声明正确的方法是吗?
一种方法是使用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();
}
每当按钮的状态发生变化时,都会调用此代码并根据需要自动设置色调。
【讨论】:
android:tint="@color/pressed_color"。这应该为您的图像添加永久色调,因为它调用与上述代码相同的颜色过滤器设置方法。这样你至少可以排除你选择的颜色有问题。
ImageView XML 文件中。请参阅上面的编辑。
tint 只接受纯色。很抱歉让你走错了路。其次,我在我创建的自定义ImageView 中尝试了上面原始帖子中的代码,它对我有用。我通过使用setImageResource(int) 设置图像对其进行了测试,但我不明白为什么其他图像设置方法不起作用。最后,确保在您的 XML 定义中添加android:clickable="true"。如果没有这条线,它将无法工作。
我必须对其进行测试,但您应该能够将具有该行为的 xml 设置为 ImageView 可绘制对象,然后将您的位图设置为 ImageView 背景。
【讨论】:
对我来说,一个简单的解决方案是有效的,在 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 布局,没什么特别的。
【讨论】:
这段代码 sn-p 为我工作:
porterDuffColorFilter = newPorterDuffColorFilter(getResources().getColor(R.color.cardview_dark_background),PorterDuff.Mode.MULTIPLY);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);
【讨论】: