import android.content.Context;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class MyImageView extends ImageView{
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
this.setOnTouchListener(VIEW_TOUCH_DARK);
}
public static final OnTouchListener VIEW_TOUCH_DARK = new OnTouchListener() {
//变暗(三个-50,值越大则效果越深)
public final float[] BT_SELECTED_DARK = new float[] { 1, 0, 0, 0, -50, 0, 1,
0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };
/*
//变亮
public final float[] BT_SELECTED_LIGHT = new float[] { 1, 0, 0, 0, 50, 0, 1,
0, 0, 50, 0, 0, 1, 0, 50, 0, 0, 0, 1, 0 };
//恢复
public final float[] BT_NOT_SELECTED = new float[] { 1, 0, 0, 0, 0, 0,
1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };
*/
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ImageView iv = (ImageView) v;
iv.setColorFilter(new ColorMatrixColorFilter(BT_SELECTED_DARK));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
ImageView iv = (ImageView) v;
iv.clearColorFilter();
}
return false; //如为false,执行ACTION_DOWN后不再往下执行
}
};
}
点击可变暗的ImageView
最新推荐文章于 2021-05-26 13:30:27 发布
本文介绍了一个自定义的Android ImageView组件,该组件在被触摸时会显示为变暗的效果,并在触摸结束时恢复原状。通过使用ColorMatrixColorFilter实现颜色过滤,此效果可以增强用户交互体验。
1878

被折叠的 条评论
为什么被折叠?



