背景颜色呼吸效果

该博客演示了一个使用CSS实现的背景动画效果,通过`linear-gradient`和`background-position`配合`@keyframes`动画,创建了一个从左到右的背景颜色平移效果。代码中展示了如何设置背景颜色、大小、重复方式以及动画的定义,适用于网页动态背景设计。
摘要由CSDN通过智能技术生成

1.演示地址

https://animista.net/play/background/bg-pan

2.代码

.bg-pan-left {
    background-image: linear-gradient( 
90deg,#3e91cc,#2dcca7);
    background-size: 200%;
    background-repeat: repeat;
    animation: bg-pan-right 1.2s infinite both;
    animation-direction: alternate;
}
@keyframes bg-pan-left {
  0% {
    background-position: 100% 50%;
  }
  100% {
    background-position: 0% 50%;
  }
}
在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、付费专栏及课程。

余额充值