扇叶对比
简单说下这个的实现,其实实现虚线画圆真心简单两行代码解决。 // kotlin版 java也是一样就是设置一下绘制效果就可以绘制虚线圆 val pathEffect = DashPathEffect(floatArrayOf(mPaint.strokeWidth*0.4f,mPaint.strokeWidth),0f) mPaint.pathEffect = pathEffect canvas.drawCircle(dashedRingCx,dashedRingCy,dashedRingRadius,mPaint) 但是 渐变的怎么绘制呢?上面的方法就不能用了,因为Android绘制渐变的我想到的只有LinearGradient,我说的是一个个绘制渐变,先说下我的方法,LinearGradient需要传入好几个参数其中这里比较重要的就是x0,y0,x1,y1,就是你需要从那个点渐变到那个点,直接再上个图标就是求出图中两个就可以绘制一个渐变的小圆环了,然后怎么绘制一个圆弧了。 // kotlin版 java也是一样 //实现渐变扇叶 while (curAngle < 360 - mEachPanAngle) { val x0 = measuredWidth/2f + (Math.cos((curAngle)*Math.PI/180)*(dashedRingRadius-dashedRadiusDiff*0.5)).toFloat() val y0 = measuredHeight/2f + (Math.sin((curAngle)*Math.PI/180)*(dashedRingRadius-dashedRadiusDiff*0.5)).toFloat() val x1 = measuredWidth/2f + (Math.cos((curAngle+mEachPanAngle)*Math.PI/180)*(dashedRingRadius+dashedRadiusDiff*0.5)).toFloat() val y1 = measuredHeight/2f + (Math.sin((curAngle+mEachPanAngle)*Math.PI/180)*(dashedRingRadius+dashedRadiusDiff*0.5)).toFloat() val shader = LinearGradient(x0, y0, x1, y1, Color.parseColor("#22ffffff"), Color.parseColor("#ffffffff"), Shader.TileMode.CLAMP) mPaint.shader = shader canvas?.drawArc(rectF, curAngle, mEachPanAngle, false, mPaint) curAngle = curAngle + mEachPanAngle + mEachPanAngleGap } mPaint.shader = null //记得清除这个的实现方法就很多了可以直接属性动画一个起始值一个结束值,设置animator.setEvaluator(ArgbEvaluator()),还有一个就是使用Hsv 使颜色渐变更适合人类观感,公式就不写,网上有源码里也有。
这个动画还是使用属性动画,但注意每次开启和关闭时传入的值,当前值为起始值,结束值为你要到的值,代码其实很简单。 “` java
private fun onFanAnim(isOpenFan: Boolean) { if (mJumpAnimator != null && mJumpAnimator!!.isRunning) { mJumpAnimator!!.cancel() } if (isOpenFan) { mJumpAnimator = ObjectAnimator.ofFloat(this,”dashedRadiusDiff”,dashedRadiusDiff,mDashedRingWidth) mJumpAnimator!!.interpolator = DecelerateInterpolator() mJumpAnimator!!.addListener(object : Animator.AnimatorListener{ override fun onAnimationRepeat(p0: Animator?) { } override fun onAnimationEnd(p0: Animator?) { if (!isCancelJumpAnim) { mPanListener?.onHasOpen() onRotateAnim() } isCancelJumpAnim = false } override fun onAnimationCancel(p0: Animator?) { isCancelJumpAnim = true } override fun onAnimationStart(p0: Animator?) {
} }) mJumpAnimator!!.duration = 2000 } else { mJumpAnimator = ObjectAnimator.ofFloat(this,"dashedRadiusDiff",dashedRadiusDiff,0f) mJumpAnimator!!.interpolator = AccelerateInterpolator() mJumpAnimator!!.addListener(object : Animator.AnimatorListener{ override fun onAnimationRepeat(p0: Animator?) { } override fun onAnimationEnd(p0: Animator?) { if (dashedRadiusDiff == 0f) { mPanListener?.onHasClose() } } override fun onAnimationCancel(p0: Animator?) { } override fun onAnimationStart(p0: Animator?) { } }) mJumpAnimator!!.duration = 1200 } mJumpAnimator!!.start() }“`
这个实现方法非常多我实现方法应该是比较简单的,但效果不是特别符合空气效果,颗粒物实现了两个效果一个随机流动还有一个随着扇叶渐变到,1.无方向浮动是每次刷新都进行加一个随机 -0.5 ~ 0.5数字 2.被设备吸入的效果使用角度增加,半径减小是不是很简单。
这个实现过程更简单了每次改变动画的时间但要记住起点是上一次动画结束点,终点是结束点+360