我有一个imageview,我已经设置了从url中获取的位图。 在imageview上,我设置了一个打开对话框的onClickListener。Android的imageview更改色调模拟按钮点击
我想以某种方式改变颜色(使其更深),当按下图像视图时,提供一种像感觉一样的按钮点击。
你有什么建议?
我有一个imageview,我已经设置了从url中获取的位图。 在imageview上,我设置了一个打开对话框的onClickListener。Android的imageview更改色调模拟按钮点击
我想以某种方式改变颜色(使其更深),当按下图像视图时,提供一种像感觉一样的按钮点击。
你有什么建议?
我不得不对它进行测试,但您应该能够将该行为设置为ImageView drawable,然后将您的位图设置为ImageView背景。
一种方法是使用ColorFilter
和ColorStateList
的组合,其中包含您按下按钮时的色调颜色。在res /彩色目录为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();
}
无论何时按钮的状态发生变化,此代码被调用并会自动设置适当的色调。
对我来说,一个简单的解决方案正在使用setAlpha(180)在的onClick事件使图像变暗,给用户一个反馈,它被点击或触摸。
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的源代码只接受一个整数(纯色)。 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和自定义属性与您当地的命名空间,而不是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>
我想你的解决方案,但仍然得到'java.lang.NumberFormatException:无效的int:“@ 2130837701”'当TintableImageView'内的构造函数调用它的超级...我需要传递'new int [] {R .styleable.TintableImageView_tint}'因为'obtainStyledAttributes'需要一个数组,并声明'
这就是为什么我的答案在attrs.xml中定义了一个自定义tint属性。您必须在您的布局中使用自定义**应用程序:tint **而不是** android:tint **。实质上,您正在创建一个新的属性,该属性包装原生属性并一次为其提供一种颜色。 –
感谢您的回答!我已经更改为'app:tint',并且异常消失了...但是选择器仍然没有被使用,即我的图标始终是黑色的... ...是我在colors.xml中的可绘制声明的正确方法去做吧? –
这段代码为我工作:
porterDuffColorFilter = newPorterDuffColorFilter(getResources().getColor(R.color.cardview_dark_background),PorterDuff.Mode.MULTIPLY);
imgView.getDrawable().setColorFilter(porterDuffColorFilter);
imgView.setBackgroundColor(Color.TRANSPARENT);
我尝试了你的建议。它似乎没有做任何事情。使用调试器,我发现drawableStateChanged()方法在onPress上没有触发,但似乎只在onClick上触发。 – Abhishek
嗯,我在上面的代码中看不到任何错误。根据定义,该方法应该在按下状态改变时调用。我一直在我自己的代码中使用这个构造,它工作正常。作为一个测试,在你的ImageView XML中添加'android:tint =“@ color/pressed _color_”“。这应该为图像添加永久性色调,因为它与上面的代码调用相同的colorfilter设置方法。这样,您至少可以排除您选择颜色的问题。 – happydude
对不起,我很笨。你可以直接将你的颜色状态列表直接添加到你的'ImageView' XML文件中。见上面的编辑。 – happydude