2.水波纹控件RippleLayout

---------------------更新2017/2/24--------
发现一个更好的类库,https://github.com/traex/RippleEffect
这轮子果然重造了.... ,不过还是学到好多。
本控件的特点:可以对所有的子View添加此特效

----------------------------------以下是历史内容,并非该类库讲解-----------------

该水波纹控件 可以对所有子控件添加水波纹效果支持 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望发一份到739043667@qq.com,在此先谢过
欢迎交流学习,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`3667@qq.com
 */
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);
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 171,050评论 25 707
  • . 现在决定未来,知识改变命运。 2. 小事成就大事,细节成就完美。 3. 态度决定高度,思路决定出路,细节关乎命...
    毛光临阅读 332评论 0 0
  • ZHJunLing阅读 147评论 0 0