Android中的脉动按钮动画 [英] Pulsating button animation in Android

查看:226
本文介绍了Android中的脉动按钮动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

我试图制作一个ScaleAnimation,使按钮变大,但是,当然,它使所有按钮变大.

I have tried to make a ScaleAnimation, doing the button grow, but of course, it makes grow all the button.

   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按钮"以使其生长并实现此效果.

So the idea is achieve something like that but just with a alpha behind the actual button. I want to know if it's possible to do this with an alpha animation or something before add a second "Alpha Button" behind my button to make it grow and achieve this effect.

推荐答案

最后,我找到了解决方案!当我的布尔值为true时,我重写了onDraw方法并为按钮绘制了第一个圆.我绘制一个以alpha为背景的第二个圆.产生脉动效果:

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

这篇关于Android中的脉动按钮动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆