Android imageview更改色调以模拟按钮单击

Android imageview更改色调以模拟按钮单击,android,android-imageview,android-ui,Android,Android Imageview,Android Ui,我有一个imageview,我在上面设置了一个从url获取的位图。 在imageview上,我设置了一个onClickListener,它打开了一个对话框 我想在按下imageview时以某种方式改变色调(使其更暗),以提供一种按钮点击式的感觉 你有什么建议 我必须对其进行测试,但您应该能够将具有该行为的xml设置为ImageView drawable,然后将位图设置为ImageView背景。一种方法是使用颜色过滤器和颜色状态列表的组合,其中包含按下按钮时的着色颜色。res/color目录中的C

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

我想在按下imageview时以某种方式改变色调(使其更暗),以提供一种按钮点击式的感觉


你有什么建议

我必须对其进行测试,但您应该能够将具有该行为的xml设置为ImageView drawable,然后将位图设置为ImageView背景。

一种方法是使用
颜色过滤器
颜色状态列表
的组合,其中包含按下按钮时的着色颜色。res/color目录中的
ColorStateList
的xml如下所示:

按钮按下。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>

任何时候按钮的状态发生变化,都会调用此代码,并根据需要自动设置色调。

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

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

使用基于新状态设置色调的代码扩展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>
<?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>
<?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>

将小部件和自定义属性与本地名称空间(而不是Android名称空间)一起使用:

示例_layout.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>

    <declare-styleable name="TintableImageView">
        <attr name="tintColorStateList" format="reference|color" />
    </declare-styleable>

</resources>
<?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>
<?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>

然后,您可以使用颜色选择器,如happydude建议的:

color\u selector.xml

<?xml version="1.0" encoding="UTF-8"?>
<resources>

    <declare-styleable name="TintableImageView">
        <attr name="tintColorStateList" format="reference|color" />
    </declare-styleable>

</resources>
<?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>
<?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>

这个代码片段对我很有用:

porterDuffColorFilter = newPorterDuffColorFilter(getResources().getColor(R.color.cardview_dark_background),PorterDuff.Mode.MULTIPLY);

imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);

我试过你的建议。它似乎没什么作用。使用调试器,我注意到drawableStateChanged()方法不会在onPress上触发,但似乎只在onClick上触发。嗯,我在上面的代码中看不到任何错误。根据定义,当按下的状态更改时,应该调用该方法。我一直在我自己的代码中使用这个结构,它工作正常。作为测试,在ImageView XML中添加
android:tint=“@color/pressed\u color”
。这将为图像添加永久色调,因为它调用与上面代码相同的颜色过滤器设置方法。这样你至少可以排除你选择的颜色有问题。对不起,我很笨。您可以直接将颜色状态列表添加到
ImageView
XML文件中。请参见上面的编辑。确定。这就是当我设置android:tint=“@drawable/button_pressed”时出现的错误,我在编译时得到这个错误,06-19 20:22:54.222:E/AndroidRuntime(2338):由:java.lang.NumberFormatException:无法将'res/drawable/button_pressed.xml'解析为整数06-19 20:22:54.222:E/AndroidRuntime(2338):在java.lang.integer.parse(Integer.java:383)06-19 20:22:54.222:E/AndroidRuntime(2338):在java.lang.Integer.parseInt(Integer.java:372)06-19 20:22:54.222:E/AndroidRuntime(2338):在com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:12s)首先,请忽略“编辑”我浏览了源代码,确信
tint
只接受纯色。很抱歉让您误入歧途。其次,我在我创建的自定义
ImageView
中尝试了上面原始帖子中的代码,它对我有效。我通过使用
setImageResource(int)设置图像来测试它
,但我不明白为什么其他图像设置方法不起作用。最后,请确保在XML定义中添加
android:clickable=“true”
。如果没有这一行,它将不起作用。我正在尝试您的解决方案,但仍然得到
java.lang.NumberFormatException:Invalid int:@213083701
TintableImageView
中的构造函数调用其super…我需要传递
newint[]{R.styleable.TintableImageView\u tint}
因为
ActainStyledAttributes
要求数组,并在colors.xml中声明
@drawable/tab_icon_selector
以便能够从android:tint引用它。这就是为什么我的答案在attrs.xml中定义了自定义tint属性。您必须在布局中使用自定义应用程序:tint,而不是android:tint.E基本上,您正在创建一个新属性,该属性包装本机属性并一次提供一种颜色。感谢您的回答!我已更改为
app:tint
,例外情况消失了…但选择器仍然没有被使用,即,我的图标一直是黑色的…我在colors.xml中的可绘制声明正确吗?请e回答我的问题,这样我就可以接受了。我粘贴了你的代码,检查了所有的东西,但它就是不起作用。