自定义view实现android5.0 ripple效果

思路:

      重写Button在onTouchEvent中监听action_down事件,然后播放一个属性动画,动态的改变圆圈的半径,这样就产生了波纹效果      

       1.首先是RippleButton直接继承自Button

       2.在attrs.xml中自定义两个属性一个是波纹的颜色rb_rippleColor,一个是波纹的透明度rb_alphaFactor;

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="RippleView">
        <attr name="rb_rippleColor" format="color" />
        <attr name="rb_alphaFactor" format="float" />
    </declare-styleable>
</resources>

       3.在构造函数中初始化这两个属性

public RippleButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setAlpha(100);
        mPath = new Path();

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RippleView);
        mRippleColor = a.getColor(R.styleable.RippleView_rb_rippleColor, mRippleColor);
        mAlphaFactor = a.getFloat(R.styleable.RippleView_rb_alphaFactor, mAlphaFactor);
        if (mAlphaFactor > 0 && mAlphaFactor <= 1) {
            mPaint.setAlpha((int) Math.floor(255 * mAlphaFactor));
        }
        a.recycle();
    }

        4.重写onTouchEvent

    

 public boolean onTouchEvent(MotionEvent event) {
        boolean superResult = super.onTouchEvent(event);
        mDownX = event.getX();
        mDownY = event.getY();
        if (isEnabled() && !isAnimatorPlaying && event.getActionMasked() == MotionEvent.ACTION_DOWN) {
            //calculate ripple's radius
            float offsetX = Math.max(mDownX - getLeft(), getRight() - mDownX);
            float offsetY = Math.max(mDownY - getTop(), getBottom() - mDownY);
            mMaxRadius = (float) Math.sqrt(Math.pow(offsetX, 2) + Math.pow(offsetY, 2));
            ObjectAnimator mScaleAnimator = ObjectAnimator.ofFloat(this, "radius", 0, mMaxRadius);
            mScaleAnimator.addListener(new Animator.AnimatorListener() {
                @Override
                public void onAnimationStart(Animator animation) {
                    isAnimatorPlaying = true;
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    setRadius(0);
                    isAnimatorPlaying = false;
                    if (onRippleFinished != null) {
                        onRippleFinished.onRippleFinished(RippleButton.this);
                    }
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    isAnimatorPlaying = false;
                }

                @Override
                public void onAnimationRepeat(Animator animation) {

                }
            });
            mScaleAnimator.setInterpolator(new AccelerateInterpolator());
            mScaleAnimator.setDuration(500);
            mScaleAnimator.start();
        }
        return superResult;

    }
这里边主要是对手指按下这个事件作出了相应,给一个属性动画,也就是对半径作出改变,下面一个重要的方法就是setRadius(float radius)

        5.实现setRadius方法

 protected void setRadius(float radius) {
        mMaxRadius = radius;
        if (radius > 0) {
            RadialGradient mRadialGradient = new RadialGradient(mDownX, mDownY, radius, adjustAlpha(mRippleColor, mAlphaFactor), mRippleColor, Shader.TileMode.MIRROR);
            mPaint.setShader(mRadialGradient);
        }
        invalidate();
    }
这里面是根据属性动画里不断传递过来的半径,对RadialGradient不断的改变其大小,然后重新绘制真个view


         6.更新view

protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isInEditMode()) {
            return;
        }

        canvas.save(Canvas.CLIP_SAVE_FLAG);

        mPath.reset();
        mPath.addCircle(mDownX, mDownY, mMaxRadius, Path.Direction.CW);
        canvas.clipPath(mPath);

        canvas.restore();
        canvas.drawCircle(mDownX, mDownY, mMaxRadius, mPaint);
    }
这里面主要就是根据panint画出一个圆,这样就实现了ripple效果啦

      

         7.最后是给动画结束添加一个回调函数

  

public interface OnRippleFinished {
        void onRippleFinished(View rippleButton);
    }
public void setOnRippleFinished(OnRippleFinished onRippleFinished) {
        this.onRippleFinished = onRippleFinished;
    }



源码地址:https://github.com/zlidentify/RippleButton

参考实现:https://github.com/siriscac/RippleView

         

        

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值