实现点击图片增加蒙层
增加蒙层
drawable.mutate().setColorFilter(Color.GRAY,
PorterDuff.Mode.MULTIPLY);
当ImageView使用在ListView中,为避免滑动过程中touch事件执行,造成图片蒙层闪现的情况,需要增加点击延时,实现如下:
继承ImageView,重写onTouch事件
public class CustomImageView extends ImageView {
public CustomImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomImageView(Context context) {
super(context);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean bool = super.onTouchEvent(event);
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mPendingCheckForTap == null) {
mPendingCheckForTap = new CheckForTap();
}
postDelayed(mPendingCheckForTap, ViewConfiguration.getTapTimeout());
break;
case MotionEvent.ACTION_MOVE:
removeTapCallback();
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
removeTapCallback();
if (mUnsetPressedState == null) {
mUnsetPressedState = new UnsetPressedState();
}
if (isPressed()) {
Drawable drawable=getDrawable();
if(drawable!=null) {
drawable.mutate().setColorFilter(Color.GRAY,
PorterDuff.Mode.MULTIPLY);
}
postDelayed(mUnsetPressedState, ViewConfiguration.getPressedStateDuration());
} else {
mUnsetPressedState.run();
}
default:break;
}
return bool;
}
}