Android中的脉动按钮动画

时间:2016-05-16 21:09:52

标签: android button android-animation android-imageview scaletransform

我正在使用语音识别应用程序,我想在录制时将播放/停止按钮设置为“脉冲”。像这样:

enter image description here

我试图制作一个ScaleAnimation,按钮增长,但当然,它会使所有按钮增长。

   public static ObjectAnimator pulseAnimation(ImageView target){

    ObjectAnimator scaleDown = ObjectAnimator.ofPropertyValuesHolder(target,
            PropertyValuesHolder.ofFloat("scaleX", 1.1f),
            PropertyValuesHolder.ofFloat("scaleY", 1.1f));
    scaleDown.setDuration(310);
    scaleDown.setRepeatCount(ObjectAnimator.INFINITE);
    scaleDown.setRepeatMode(ObjectAnimator.REVERSE);

    return scaleDown;
}

所以这个想法是实现类似的东西,但只是在实际按钮后面有一个alpha。我想知道是否可以使用alpha动画或其他东西在我的按钮后面添加第二个“Alpha Button”以使其增长并实现此效果。

1 个答案:

答案 0 :(得分:7)

最后我找到了解决方案!我重写了onDraw方法并为我的按钮画了第一个圆圈,当我的布尔值为真时。我用alpha作为背景画第二个圆圈。产生脉动效果:

 @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();
}