Pulsating button animation in Android

后端 未结 1 2018
面向向阳花
面向向阳花 2020-12-10 17:42

I\'m working with a Voice recognition application and I want to make my play/stop button \"pulse\" when it\'s recording. Something like this:

I have tried t

相关标签:
1条回答
  • 2020-12-10 18:17

    Finally I found the solution! I overrided the onDraw method and draw a first circle for my button and, when my boolean is true. I draw a second circle with an alpha as a background. Making a pulsating effect:

     @Override
    protected void onDraw(Canvas canvas) {
    
        int w = getMeasuredWidth();
        int h = getMeasuredHeight();
        mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mCirclePaint.setColor(mColor);
        mBackgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mBackgroundPaint.setColor(Util.adjustAlpha(mColor, 0.4f));
        //Draw circle
        canvas.drawCircle(w/2, h/2, MIN_RADIUS_VALUE , mCirclePaint);        
        if (mAnimationOn) {
            if (mRadius >= MAX_RADIUS_VALUE)
                mPaintGoBack = true;
            else if(mRadius <= MIN_RADIUS_VALUE)
                mPaintGoBack = false;
            //Draw pulsating shadow
            canvas.drawCircle(w / 2, h / 2, mRadius, mBackgroundPaint);
            mRadius = mPaintGoBack ? (mRadius - 0.5f) : (mRadius + 0.5f);
            invalidate();
        }
    
        super.onDraw(canvas);
    }
     public void animateButton(boolean animate){
        if (!animate)
            mRadius = MIN_RADIUS_VALUE;
        mAnimationOn = animate;
        invalidate();
    }
    
    0 讨论(0)
提交回复
热议问题