基本信息
源码名称:点击实现水波纹效果
源码大小:1.45M
文件格式:.rar
开发语言:Java
更新时间:2016-05-17
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

核心代码:


public class RevealLayout extends LinearLayout implements Runnable{
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    
    private float mCenterX,mCenterY;
    
    private int[] mLocation = new int[2];
    
    private int INVALIDATE_DURATION = 40;
    private int mTargetHeight,mTargetWidth;
    private int mRevealRadius = 0,mRevealRadiusGap,mMaxRadius;
    private int mMinBetweenWidthAndHeight,mMaxBetweenWidthAndHeight;
    
    private boolean mIsPressed;
    private boolean mShouldDoAnimation;
    
    private View mTargetView;
    private DispatchUpTouchEventRunnable mDispatchUpTouchEventRunnable = new DispatchUpTouchEventRunnable();
    
    public RevealLayout(Context context) {
        super(context);
        init();
    }
    
    public RevealLayout(Context context, AttributeSet attrs){
        super(context,attrs);
        init();
    }
    
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public RevealLayout(Context context, AttributeSet attrs, int defStyleAttr){
        super(context,attrs,defStyleAttr);
        init();
    }

    public void init(){
        setWillNotDraw(false);
        mPaint.setColor(getResources().getColor(R.color.reveal_color));
    }
    
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        this.getLocationOnScreen(mLocation);
    }
    
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        
        if(mTargetView == null || !mShouldDoAnimation || mTargetWidth <= 0)
            return;
        
        if(mRevealRadius > mMinBetweenWidthAndHeight / 2)
            mRevealRadius = mRevealRadiusGap * 4;
        else
            mRevealRadius = mRevealRadiusGap;
        
        int[] location = new int[2];
        this.getLocationOnScreen(mLocation);
        mTargetView.getLocationOnScreen(location);

        int top = location[1] - mLocation[1];
        int left = location[0] - mLocation[0];
        int right = left mTargetView.getMeasuredWidth();
        int bottom = top mTargetView.getMeasuredHeight();
        
        canvas.save();
        canvas.clipRect(left, top, right, bottom);
        canvas.drawCircle(mCenterX, mCenterY, mRevealRadius, mPaint);
        canvas.restore();
        
        if(mRevealRadius <= mMaxRadius)
            postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
        else if(!mIsPressed){
            mShouldDoAnimation = false;
            postInvalidateDelayed(INVALIDATE_DURATION, left, top, right, bottom);
        }
    }
    
    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int x = (int)event.getRawX();
        int y = (int)event.getRawY();
        int action = event.getAction();
        
        switch(action){
            case MotionEvent.ACTION_DOWN:
                View targetView = getTargetView(this,x,y);
                
                if(targetView != null && targetView.isEnabled()){
                    mTargetView = targetView;
                    initParametersForChild(event,targetView);
                    postInvalidateDelayed(INVALIDATE_DURATION);
                }
                break;
                
            case MotionEvent.ACTION_UP:
                mIsPressed = false;
                postInvalidateDelayed(INVALIDATE_DURATION);
                mDispatchUpTouchEventRunnable.event = event;
                postDelayed(mDispatchUpTouchEventRunnable, 40);
                break;
                
            case MotionEvent.ACTION_CANCEL:
                mIsPressed = false;
                postInvalidateDelayed(INVALIDATE_DURATION);
                break;
        }        
        return super.dispatchTouchEvent(event);
    }
    
    public View getTargetView(View view,int x,int y){
        View target = null;
        ArrayList<View> views = view.getTouchables();
        
        for(View child : views)
            if(isTouchPointInView(child,x,y)){
                target = child;
                break;
            }
        return target;
    }
    
    public boolean isTouchPointInView(View child,int x,int y){
        int[] location = new int[2];
        child.getLocationOnScreen(location);

        int top = location[1];
        int left = location[0];
        int right = left child.getMeasuredWidth();
        int bottom = top child.getMeasuredHeight();
        
        if(child.isClickable() && y>=top && y<= bottom && x >= left && x<= right)
            return true;
        else
            return false;
    }
    
    public void initParametersForChild(MotionEvent event,View view){
        mCenterX = event.getX();
        mCenterY = event.getY();
        mTargetWidth = view.getMeasuredWidth();
        mTargetHeight = view.getMeasuredHeight();
        mMinBetweenWidthAndHeight = Math.min(mTargetWidth, mTargetHeight);
        mMaxBetweenWidthAndHeight = Math.max(mTargetWidth, mTargetHeight);

        mRevealRadius = 0;
        mRevealRadiusGap = mMinBetweenWidthAndHeight / 8;

        mIsPressed = true;
        mShouldDoAnimation = true;
        
        int[] location = new int[2];
        view.getLocationOnScreen(location);
        
        int left = location[0] - mLocation[0];
        int mTransformedCenterX = (int)mCenterX - left;
        mMaxRadius = Math.max(mTransformedCenterX, mTargetWidth - mTransformedCenterX);
    }
    
    @Override
    public void run() {
        super.performClick();
    }

    @Override
    public boolean performClick() {
        postDelayed(this,40);
        return true;
    }
    private class DispatchUpTouchEventRunnable implements Runnable{
        public MotionEvent event;
        
        @Override
        public void run() {
            if(mTargetView.isEnabled() && mTargetView.isClickable())
                return;
            
            if(isTouchPointInView(mTargetView, (int)event.getRawX(), (int)event.getRawX()))
                mTargetView.performClick();
        }
    }
}