会View设置点击效果

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

public static View.OnTouchListener buildLayTouchListener(final int colorRes, final Context context) { return new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    setSelected(view, true);
                    break;
                default:
                    break;
            }
            return false;
        }

        private void setSelected(final View view, boolean isSelected) {
            if (isSelected) {
                view.getBackground().setColorFilter(context.getResources().getColor(colorRes),
                        PorterDuff.Mode.MULTIPLY);
                view.invalidate();
                view.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        setSelected(view, false);
                    }
                }, 100);
            } else {
                view.getBackground().clearColorFilter();
                view.invalidate();
            }
        }
    };
}

转载于:https://my.oschina.net/u/698243/blog/169213

你可能感兴趣的:(会View设置点击效果)