通过ValueAnimator实现呼吸灯效果

主要是通过AnimatorUpdateListener获得状态,运行一个不现实的动画,根据获得的运行值自己设置view或者drawable的状态。

代码如下

ValueAnimator alphaAnim = null;

/**

 * 透明渐变的动画 

 * @param animType 动画的类型,循环/单次  0是单次,1是循环,默认0

 * @param drawable 结束之后的前景

 **/

public void startAlphaAnim(final int animType,final int overDrawable){

alphaAnim =ObjectAnimator.ofInt(255,0);

alphaAnim.setDuration(2000);

if(1==animType){

alphaAnim.setRepeatCount(-1);

alphaAnim.setRepeatMode(ValueAnimator.REVERSE);

}

alphaAnim.addUpdateListener(new AnimatorUpdateListener(){

@Override

public void onAnimationUpdate(ValueAnimator animation) {

int frameValue = (Integer) animation.getAnimatedValue();

getBackground().setAlpha(255-frameValue);

getDrawable().setAlpha(frameValue);

}

});

alphaAnim.addListener(new AnimatorListener(){

@Override

public void onAnimationStart(Animator animation) {}

@Override

public void onAnimationEnd(Animator animation) {

//将透明度的恢复放在前面,只有这样才是对同一对象入手

//背景色不需要做处理,因为背景色只有在动画的时候才有用

getDrawable().setAlpha(255);

setBackgroundColor(getResources().getColor(R.color.transparent));

setImageResource(overDrawable);

}

@Override

public void onAnimationCancel(Animator animation) {

//将透明度的恢复放在前面,只有这样才是对同一对象入手

//背景色不需要做处理,因为背景色只有在动画的时候才有用

getDrawable().setAlpha(255);

setBackgroundColor(getResources().getColor(R.color.transparent));

setImageResource(overDrawable);

}

@Override

public void onAnimationRepeat(Animator animation) {}

});

alphaAnim.start();

}

/**

 * 取消渐变动画 

 **/

public void closeAlphaAnim(){

if(alphaAnim==null)

return;

alphaAnim.cancel();

alphaAnim = null;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android实现呼吸灯效果,可以通过使用ValueAnimator实现。具体实现步骤如下: 1. 创建一个View,设置其背景色为需要的颜色。 2. 创建一个ValueAnimator对象,设置动画时长、循环次数、动画插值器等属性。 3. 在ValueAnimator的监听器中,实现对View的背景色进行动态修改,从而实现呼吸灯效果。 以下是示例代码: ``` public class BreathingLightView extends View { private int mColor; private Paint mPaint; private ValueAnimator mAnimator; private int mAlpha; public BreathingLightView(Context context) { this(context, null); } public BreathingLightView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public BreathingLightView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); mPaint = new Paint(); mPaint.setAntiAlias(true); mColor = Color.parseColor("#FF4081"); mAnimator = ValueAnimator.ofInt(0, 255); mAnimator.setDuration(2000); mAnimator.setRepeatCount(ValueAnimator.INFINITE); mAnimator.setRepeatMode(ValueAnimator.REVERSE); mAnimator.setInterpolator(new AccelerateDecelerateInterpolator()); mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { mAlpha = (int) animation.getAnimatedValue(); mPaint.setColor(Color.argb(mAlpha, Color.red(mColor), Color.green(mColor), Color.blue(mColor))); invalidate(); } }); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(Color.WHITE); mPaint.setColor(Color.argb(mAlpha, Color.red(mColor), Color.green(mColor), Color.blue(mColor))); canvas.drawCircle(getWidth() / 2, getHeight() / 2, getWidth() / 2, mPaint); } public void startBreathing() { if (mAnimator != null && !mAnimator.isStarted()) { mAnimator.start(); } } public void stopBreathing() { if (mAnimator != null && mAnimator.isStarted()) { mAnimator.end(); } } } ``` 在Activity中,可以通过以下方式来使用BreathingLightView: ``` public class MainActivity extends AppCompatActivity { private BreathingLightView mBreathingLightView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mBreathingLightView = findViewById(R.id.breathing_light_view); mBreathingLightView.startBreathing(); } @Override protected void onDestroy() { super.onDestroy(); mBreathingLightView.stopBreathing(); } } ``` 其中,activity_main.xml中的布局代码如下: ``` <com.example.myapplication.BreathingLightView android:id="@+id/breathing_light_view" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:background="@android:color/white" /> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值