有点击效果的控件

public class RipperImageView extends ImageView {
    private static final int INVALIDATE_DURATION = 20; //每次刷新的时间间隔
    private static int DIFFUSE_GAP = 5;               //扩散半径增量
    private static int TAP_TIMEOUT;                   //判断点击和长按的时间

    private int viewWidth;                            //控件宽度和高度
    private int viewHeight;
    //private int pointX;                               //控件原点坐标(左上角)
    //private int pointY;
    private int maxRadio;                             //扩散的最大半径
    private int shaderRadio=10;

    private Paint bottomPaint;                        //画笔
    private Paint colorPaint;

    private boolean isPushButton;                     //记录是否按钮被按下
    private int eventX;
    private int eventY;
    private long downTime = 0;

    private boolean useRipper = true;
    private boolean islittle;                       //由于在小的情况的效果不好,所以在小的情况下自动进行一些调整

    public RipperImageView(Context context){
        super(context);
        initPaint();
    }
    public RipperImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    public void useRipper(boolean b) {
        useRipper = b;
    }

    /**
     * 初始化画笔资源
     */
    private void initPaint() {
        colorPaint = new Paint();
        bottomPaint = new Paint();
        //int color = getDrawingCacheBackgroundColor();
        colorPaint.setColor(0x22000000);//getResources().getColor(R.color.reveal_color)
        bottomPaint.setColor(0x22000000);//getResources().getColor(R.color.bottom_color)
        colorPaint.setAntiAlias(true);
        bottomPaint.setAntiAlias(true);
        TAP_TIMEOUT = ViewConfiguration.getLongPressTimeout();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.viewWidth = w;
        this.viewHeight = h;
        if(w >= h && w < 150) islittle=true;
        else if(h > w && h<150) islittle=true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (useRipper) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //只需要取一次时间
                    if (downTime == 0) {
                        downTime = SystemClock.elapsedRealtime();
                    }
                    if (islittle) {
                        eventX = viewWidth / 2;
                        eventY = viewHeight / 2;
                    } else {
                        eventX = (int) event.getX();
                        eventY = (int) event.getY();
                    }
                    //计算最大半径
                    countMaxRadio();
                    isPushButton = true;
                    postInvalidateDelayed(INVALIDATE_DURATION);
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (SystemClock.elapsedRealtime() - downTime < TAP_TIMEOUT) {
                        DIFFUSE_GAP = 30;
                        postInvalidate();
                    } else {
                        clearData();
                    }
                    break;
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 计算此时的最大半径
     */
    private void countMaxRadio() {
        if (viewWidth > viewHeight) {
            DIFFUSE_GAP = viewWidth/5;
            if (eventX < viewWidth / 2) {
                maxRadio = viewWidth - eventX;
            } else {
                maxRadio = viewWidth / 2 + eventX;
            }
            if(islittle) {
                maxRadio = viewWidth/3;
            }
        } else {
            DIFFUSE_GAP = viewHeight/5;
            if (eventY < viewHeight / 2) {
                maxRadio = viewHeight - eventY;
            } else {
                maxRadio = viewHeight / 2 + eventY;
            }
            if(islittle){
                maxRadio = viewHeight/3;
            }
        }
    }

    /**
     * 清理改变的数据(初始化数据)
     */
    private void clearData() {
        downTime = 0;
        DIFFUSE_GAP = 10;
        isPushButton = false;
        shaderRadio = 10;
        postInvalidate();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (!isPushButton || !useRipper) return; //如果按钮没有被按下则返回

        //绘制按下后的整个背景
        int pointX=0;
        int pointY=0;

        if(!islittle)
            canvas.drawRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight, bottomPaint);
        else
            canvas.drawCircle(pointX+viewWidth/2, pointY + viewHeight/2, viewWidth/2,  bottomPaint);
        canvas.save();
        //绘制扩散圆形背景
        canvas.clipRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight);
        canvas.drawCircle(eventX, eventY, shaderRadio, colorPaint);
        canvas.restore();
        //直到半径等于最大半径
        if (shaderRadio < maxRadio) {
            postInvalidateDelayed(INVALIDATE_DURATION,
                    pointX, pointY, pointX + viewWidth, pointY + viewHeight);
            shaderRadio += DIFFUSE_GAP;
        } else {
            clearData();
        }
    }
}

public class RipperLinearLayout extends LinearLayout {
    private static final int INVALIDATE_DURATION = 20; //每次刷新的时间间隔
    private static int DIFFUSE_GAP = 10;               //扩散半径增量
    private static int TAP_TIMEOUT;                   //判断点击和长按的时间

    private int viewWidth;                            //控件宽度和高度
    private int viewHeight;
    //private int pointX;                               //控件原点坐标(左上角)
    //private int pointY;
    private int maxRadio;                             //扩散的最大半径
    private int shaderRadio;

    private Paint bottomPaint;                        //画笔
    private Paint colorPaint;

    private boolean isPushButton;                     //记录是否按钮被按下
    private int eventX;
    private int eventY;
    private long downTime = 0;

    private boolean islittle;

    public RipperLinearLayout(Context context){
        super(context);
        initPaint();
    }
    public RipperLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    /**
     * 初始化画笔资源
     */
    private void initPaint() {
        colorPaint = new Paint();
        bottomPaint = new Paint();
        //int color = getDrawingCacheBackgroundColor();
        colorPaint.setColor(0x33000000);//getResources().getColor(R.color.reveal_color)
        bottomPaint.setColor(0x22000000);//getResources().getColor(R.color.bottom_color)
        TAP_TIMEOUT = ViewConfiguration.getLongPressTimeout();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.viewWidth = w;
        this.viewHeight = h;
        if(w >= h && w < 150) islittle=true;
        else if(h > w && h<150) islittle=true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //只需要取一次时间
                if (downTime == 0) {
                    downTime = SystemClock.elapsedRealtime();
                }
                if(islittle){
                    eventX=viewWidth/2;
                    eventY=viewHeight/2;
                }else {
                    eventX = (int) event.getX();
                    eventY = (int) event.getY();
                }
                //计算最大半径
                countMaxRadio();
                isPushButton = true;
                postInvalidateDelayed(INVALIDATE_DURATION);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (SystemClock.elapsedRealtime() - downTime < TAP_TIMEOUT) {
                    DIFFUSE_GAP = 30;
                    postInvalidate();
                } else {
                    clearData();
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 计算此时的最大半径
     */
    private void countMaxRadio() {
        if (viewWidth > viewHeight) {
            DIFFUSE_GAP = viewWidth/5;
            if (eventX < viewWidth / 2) {
                maxRadio = viewWidth - eventX;
            } else {
                maxRadio = viewWidth / 2 + eventX;
            }
            if(islittle) {
                maxRadio = viewWidth/3;
            }
        } else {
            DIFFUSE_GAP = viewHeight/5;
            if (eventY < viewHeight / 2) {
                maxRadio = viewHeight - eventY;
            } else {
                maxRadio = viewHeight / 2 + eventY;
            }
            if(islittle){
                maxRadio = viewHeight/3;
            }
        }
    }

    /**
     * 清理改变的数据(初始化数据)
     */
    private void clearData() {
        downTime = 0;
        DIFFUSE_GAP = 10;
        isPushButton = false;
        shaderRadio = 0;
        postInvalidate();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (!isPushButton) return; //如果按钮没有被按下则返回

        //绘制按下后的整个背景
        int pointX=0;
        int pointY=0;
        if(!islittle)
            canvas.drawRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight, bottomPaint);
        else
            canvas.drawCircle(pointX+viewWidth/2, pointY + viewHeight/2, viewWidth/2,  bottomPaint);

        canvas.save();
        //绘制扩散圆形背景
        canvas.clipRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight);
        canvas.drawCircle(eventX, eventY, shaderRadio, colorPaint);
        canvas.restore();
        //直到半径等于最大半径
        if (shaderRadio < maxRadio) {
            postInvalidateDelayed(INVALIDATE_DURATION,
                    pointX, pointY, pointX + viewWidth, pointY + viewHeight);
            shaderRadio += DIFFUSE_GAP;
        } else {
            clearData();
        }
    }
}


public class RipperTextView extends TextView {
    private static final int INVALIDATE_DURATION = 20; //每次刷新的时间间隔
    private static int DIFFUSE_GAP = 10;               //扩散半径增量
    private static int TAP_TIMEOUT;                   //判断点击和长按的时间

    private int viewWidth;                            //控件宽度和高度
    private int viewHeight;
    //private int pointX;                               //控件原点坐标(左上角)
    //private int pointY;
    private int maxRadio;                             //扩散的最大半径
    private int shaderRadio;

    private Paint bottomPaint;                        //画笔
    private Paint colorPaint;

    private boolean isPushButton;                     //记录是否按钮被按下
    private int eventX;
    private int eventY;
    private long downTime = 0;
    private boolean islittle;

    public RipperTextView(Context context){
        super(context);
        initPaint();
    }
    public RipperTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint();
    }

    /**
     * 初始化画笔资源
     */
    private void initPaint() {
        colorPaint = new Paint();
        bottomPaint = new Paint();
        //int color = getDrawingCacheBackgroundColor();
        colorPaint.setColor(0x33000000);//getResources().getColor(R.color.reveal_color)
        bottomPaint.setColor(0x22000000);//getResources().getColor(R.color.bottom_color)
        TAP_TIMEOUT = ViewConfiguration.getLongPressTimeout();
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        this.viewWidth = w;
        this.viewHeight = h;
        if(w >= h && w < 150) islittle=true;
        else if(h > w && h<150) islittle=true;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                //只需要取一次时间
                if (downTime == 0) {
                    downTime = SystemClock.elapsedRealtime();
                }
                if(islittle){
                    eventX=viewWidth/2;
                    eventY=viewHeight/2;
                }else {
                    eventX = (int) event.getX();
                    eventY = (int) event.getY();
                }
                //计算最大半径
                countMaxRadio();
                isPushButton = true;
                postInvalidateDelayed(INVALIDATE_DURATION);
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                if (SystemClock.elapsedRealtime() - downTime < TAP_TIMEOUT) {
                    DIFFUSE_GAP = 30;
                    postInvalidate();
                } else {
                    clearData();
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 计算此时的最大半径
     */
    private void countMaxRadio() {
        if (viewWidth > viewHeight) {
            DIFFUSE_GAP = viewWidth/5;
            if (eventX < viewWidth / 2) {
                maxRadio = viewWidth - eventX;
            } else {
                maxRadio = viewWidth / 2 + eventX;
            }
            if(islittle) {
                maxRadio = viewWidth/3;
            }
        } else {
            DIFFUSE_GAP = viewHeight/5;
            if (eventY < viewHeight / 2) {
                maxRadio = viewHeight - eventY;
            } else {
                maxRadio = viewHeight / 2 + eventY;
            }
            if(islittle){
                maxRadio = viewHeight/3;
            }
        }
    }

    /**
     * 清理改变的数据(初始化数据)
     */
    private void clearData() {
        downTime = 0;
        DIFFUSE_GAP = 10;
        isPushButton = false;
        shaderRadio = 0;
        postInvalidate();
    }

    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        if (!isPushButton) return; //如果按钮没有被按下则返回

        //绘制按下后的整个背景
        int pointX=0;
        int pointY=0;

        if(!islittle)
            canvas.drawRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight, bottomPaint);
        else
            canvas.drawCircle(pointX+viewWidth/2, pointY + viewHeight/2, viewWidth/2,  bottomPaint);

        canvas.save();
        //绘制扩散圆形背景
        canvas.clipRect(pointX, pointY, pointX + viewWidth, pointY + viewHeight);
        canvas.drawCircle(eventX, eventY, shaderRadio, colorPaint);
        canvas.restore();
        //直到半径等于最大半径
        if (shaderRadio < maxRadio) {
            postInvalidateDelayed(INVALIDATE_DURATION,
                    pointX, pointY, pointX + viewWidth, pointY + viewHeight);
            shaderRadio += DIFFUSE_GAP;
        } else {
            clearData();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值