———————更新2017/2/24——–
發現一個更好的類庫,https://github.com/traex/RippleEffect
這輪子果然重造了…. ,不過還是學到好多。

———————————-以下是歷史內容,並非該類庫講解—————–

該水波紋控件 可以對所有子控件添加水波紋效果支持 api14(最新代碼在最下方)

先看效果:

RippleLayout.gif
說明:

未修復bug說明 :

  • RippleManager動態添加效果不佳,部分機型不能實現(本人測試機型:vivo xplay510w, android 4.4-api19,ROM:Funtouch OS_2.0),原因未知,其他手機可以適配,樣本數4。
    動態添加原理: 在parentView中remove先前ViewGroup,再直接動態添加該控件,再添加remove的ViewGroup,實現動態添加效果

如嫌棄此bug,請忽略RippleManager內部類,使用xml添加效果就好。詳見使用方法

如果有大神能解決此bug望發一份到[email protected],在此先謝過
歡迎交流學習,qq:73904`3667
*

更新日誌:

———-2016/12————-
1.修復Touch事件cancle無法執行的問題(點擊后滑動到控件之外,事件仍觸發的問題)
2.添加isDrawParent屬性 false:最外層控件不會繪製水波紋;

TODO:

1.RippleManager動態添加bug
2.自定義控件屬性實現
3、clickable設置失效問題

使用:
  1. 使用xml進行引用添加
  2. 使用RippleManager進行動態添加
代碼:
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Build;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import java.util.ArrayList;

/**
 * 水波紋控件布局:
 * 子控件點擊後會繪製一個水波紋動畫
 * 配置:
 * isDrawParent:是否給最外層控件繪製水波紋
 * <p>
 * <p>
 * Bug統計:
 * 1.當出現控件重疊時水波紋會出現在底層(xml中先繪製)的布局中;需求往往不是; //已解決
 * 2、onTouch中的Cancle事件判斷有誤:當觸摸點滑動到觸發範圍外後點擊事件依然觸發 //已解決
 * <p>
 * 注意事項:
 * 1.在繪製中如果不想觸發事件可設置enable|clickable=false
 * 2.使用RippleManager動態添加效果
 *
 * @author zlw 73904`[email protected]
 */
public class RippleLayout extends RelativeLayout {
    public boolean isDrawParent = true;   //是否繪製最外層控件
    private static final String TAG = RippleLayout.class.getSimpleName();

    private Paint mPaint; 
    private int clickedViewWidth, clickedViewHeight;
    private int mMaxRippleRadius; 
    private int mRippleRadiusGap;
    private int mRippleRadius = 0;
    private float mCenterX;
    private float mCenterY;
    private int[] mLocationInScreen = new int[2];
    private boolean shouldDrawRipple = false;
    private boolean isPressed = false;
    private int INVALIDATE_DURATION = 20;
    private View clickedView;

    private boolean isClickedParent = false;

    public RippleLayout(Context context) {
        super(context);
        init();
    }

    public RippleLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RippleLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        setWillNotDraw(false);
        mPaint.setColor(Color.GRAY);    //水波紋顏色
        mPaint.setAlpha(50);                    //水波紋透明度
    }

    public void setRippleColor(int color) {
        mPaint.setColor(color);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.getLocationOnScreen(mLocationInScreen);
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        int action = event.getAction();
        if (action == MotionEvent.ACTION_DOWN) {
            View clickedView = getTouchTarget(this, x, y);
            if (clickedView != null && clickedView.isClickable() && clickedView.isEnabled()) {
                this.clickedView = clickedView;
                isClickedParent = false;
                initParamForRipple(event, clickedView, false);
                postInvalidateDelayed(INVALIDATE_DURATION);
            } else {
                this.clickedView = this;
                isClickedParent = true;
                RippleLayout.this.setClickable(true);
                initParamForRipple(event, clickedView, true);
                postInvalidateDelayed(INVALIDATE_DURATION);
            }

        } else if (action == MotionEvent.ACTION_UP) {
            isPressed = false;
            RippleLayout.this.setClickable(false);
            postInvalidateDelayed(INVALIDATE_DURATION);
//            clickedView.performClick();
//            return true;
        } else if (action == MotionEvent.ACTION_CANCEL) {
            isPressed = false;
            postInvalidateDelayed(INVALIDATE_DURATION);
        }

        return super.dispatchTouchEvent(event);
    }

    protected void dispatchDraw(Canvas canvas) {


        super.dispatchDraw(canvas);
        if (!shouldDrawRipple) {
            return;
        }
        if (clickedView == null) {
            return;
        }
        if (!isDrawParent && clickedView instanceof ViewGroup) {
            View parentView = (View) clickedView.getParent();
            if (parentView != null) {
                if (clickedView.getHeight() >= parentView.getHeight() - parentView.getPaddingBottom() - parentView.getPaddingTop()) {
                    return;
                }
            }
        }

        if (mRippleRadius > mMaxRippleRadius / 2) {
            mRippleRadius += mRippleRadiusGap * 4;
        } else {
            mRippleRadius += mRippleRadiusGap;
        }

        this.getLocationOnScreen(mLocationInScreen);
        int[] location = new int[2];
        clickedView.getLocationOnScreen(location);
        int left = location[0] - mLocationInScreen[0];
        int top = location[1] - mLocationInScreen[1];
        int right = left + clickedView.getMeasuredWidth();
        int bottom = top + clickedView.getMeasuredHeight();

        canvas.save();
        if (!isClickedParent) {
            canvas.clipRect(left, top, right, bottom);
        }
        canvas.drawCircle(mCenterX, mCenterY, mRippleRadius, mPaint);
        canvas.restore();

        if (mRippleRadius <= mMaxRippleRadius) {
            if (isClickedParent) {
                postInvalidateDelayed(INVALIDATE_DURATION);
            } else {
                postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
            }

        } else if (!isPressed) {
            shouldDrawRipple = false;
            if (isClickedParent) {
                postInvalidateDelayed(INVALIDATE_DURATION);
            } else {
                postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
            }
        }
    }

    private void initParamForRipple(MotionEvent event, View view, boolean isClickedParent) {
        mCenterX = event.getX();
        mCenterY = event.getY();
        if (isClickedParent) {
            clickedViewWidth = this.getMeasuredWidth();
            clickedViewHeight = this.getMeasuredHeight();
        } else {
            clickedViewWidth = view.getMeasuredWidth();
            clickedViewHeight = view.getMeasuredHeight();
        }
        mMaxRippleRadius = (int) Math
                .sqrt((double) (clickedViewWidth * clickedViewWidth + clickedViewHeight * clickedViewHeight));
        mRippleRadius = 0;
        shouldDrawRipple = true;
        isPressed = true;
        mRippleRadiusGap = mMaxRippleRadius / 20;
    }

    private View getTouchTarget(View view, int x, int y) {
        View target = null;
        ArrayList<View> touchableViews = view.getTouchables();

        for (int i = touchableViews.size() - 1; i >= 0; i--) {
            View child = touchableViews.get(i);
            if (isTouchPointInView(child, x, y) && child != RippleLayout.this) {
                return child;
            }
        }

        return target;
    }

    private boolean isTouchPointInView(View view, int x, int y) {
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        int left = location[0];
        int top = location[1];
        int right = left + view.getMeasuredWidth();
        int bottom = top + view.getMeasuredHeight();
        if (view.isClickable() && y >= top && y <= bottom && x >= left && x <= right) {
            return true;
        }
        return false;
    }


    public static class RippleManager {

        public static void addRipple(View child) {
            addRipple(child, true);
        }

        public static void addRipple(View child, boolean isParentRip) {
            View oldScreen = child;
            ViewGroup parentView = (ViewGroup) oldScreen.getParent();
            parentView.removeView(oldScreen);

            RippleLayout rippleLayout = new RippleLayout(child.getContext());
            rippleLayout.isDrawParent = isParentRip;
            rippleLayout.addView(oldScreen);
            parentView.addView(rippleLayout);
        }

        public static void addRipple(Activity activity, int res) {

            View oldScreen = activity.findViewById(res);
            ViewGroup parentView = (ViewGroup) oldScreen.getParent();
            parentView.removeView(oldScreen);

            RippleLayout rippleLayout = new RippleLayout(activity);
            rippleLayout.addView(oldScreen);
            parentView.addView(rippleLayout);
        }

        public static void addRipple(Activity activity) {
            addRipple(activity, true);
        }


        /**
         * 對整個activity的布局添加水波紋
         *
         * @param activity
         * @param isParentRip 對全局是否設置水波紋
         */
        public static void addRipple(Activity activity, boolean isParentRip) {
            ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
            View oldScreen = decorView.getChildAt(0);
            decorView.removeViewAt(0);

            RippleLayout rippleLayout = new RippleLayout(activity);
            rippleLayout.isDrawParent = isParentRip;
            rippleLayout.addView(oldScreen);

            decorView.addView(rippleLayout);
        }
    }
}