点击可变暗的ImageView

来源:互联网 发布:文件档案管理系统软件 编辑:程序博客网 时间:2024/05/17 23:59
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后不再往下执行        }      };  }
0 0