通过改变View透明度来给view增加点击效果,可以不用再写selector
/**
 * 通过改变View透明度来给view增加点击效果,可以不用再写selector
 * @param view
 */
public static void setAlphaSelector(View view) {
    view.setAlpha(1f);
    view.setOnTouchListener(new View.OnTouchListener() {
        float lastPosX = -1;
        float lastPosY = -1;
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            float posX = motionEvent.getX();
            float posY = motionEvent.getY();
            if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                lastPosX = posX;
                lastPosY = posY;
                if (view.isClickable()){
                    view.setAlpha(0.5f);
                }
            } else if (motionEvent.getAction() == MotionEvent.ACTION_MOVE) {
                if (lastPosY == posY && lastPosX == posX) {
                    view.setAlpha(0.5f);
                } else {
                    view.setAlpha(1f);
                }
            } else {
                view.setAlpha(1f);
            }
            return false;
        }
    });
}



注意:本文归作者所有,未经作者允许,不得转载
2017,颜值新招数