Skip to content

Commit

Permalink
optimize cloud effect
Browse files Browse the repository at this point in the history
  • Loading branch information
wsj1024 committed Aug 2, 2021
1 parent c3269d6 commit 82dbddf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
11 changes: 9 additions & 2 deletions lib/src/main/java/me/wsj/lib/EffectUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class EffectUtil {
2 -> {
if (isDay) {
EffectCloudDrawable(
1,
arrayOf(
context.resources.getDrawable(R.drawable.cloudy_day_1),
context.resources.getDrawable(R.drawable.cloudy_day_2),
Expand All @@ -32,13 +31,21 @@ class EffectUtil {
)
} else
EffectCloudDrawable(
1,
arrayOf(
context.resources.getDrawable(R.drawable.cloudy_day_1),
context.resources.getDrawable(R.drawable.cloudy_night1),
context.resources.getDrawable(R.drawable.cloudy_night2)
)
)
}
3->{
EffectCloudDrawable(
arrayOf(
context.resources.getDrawable(R.drawable.fog_cloud_1),
context.resources.getDrawable(R.drawable.fog_cloud_2)
)
)
}
4 -> { // 中雨
EffectRainDrawable(
1,
Expand Down
18 changes: 12 additions & 6 deletions lib/src/main/java/me/wsj/lib/specialeffects/EffectCloudDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.animation.LinearInterpolator
import androidx.collection.ArraySet
import me.wsj.lib.specialeffects.entity.Cloud
import me.wsj.lib.specialeffects.entity.Rain
import per.wsj.commonlib.utils.LogUtil
Expand All @@ -18,7 +19,7 @@ import kotlin.math.sin
* create by shiju.wang
* cloud
*/
class EffectCloudDrawable(val type: Int, val clouds: Array<Drawable>) :
class EffectCloudDrawable(val clouds: Array<Drawable>) :
Drawable(), ICancelable {

val random = Random()
Expand All @@ -36,30 +37,36 @@ class EffectCloudDrawable(val type: Int, val clouds: Array<Drawable>) :

mWidth = bounds.right - bounds.left

for (i in 0 until 3) {
for (i in clouds.indices) {
val nextX = random.nextInt(mWidth)
val nextY = random.nextInt((mWidth * 0.7).toInt())

cloudList.add(
Cloud(
nextX.toFloat(),
nextY.toFloat(),
random.nextFloat() * speed + speed,
random.nextInt(clouds.size)
i
)
)
}

startAnim()
}

var counter = 0

private fun startAnim() {
if (mAnimator == null) {
mAnimator = ValueAnimator.ofFloat(0f, 1f)
mAnimator?.repeatCount = ValueAnimator.INFINITE
mAnimator?.duration = 3000
mAnimator?.duration = 6000
mAnimator?.interpolator = LinearInterpolator()
mAnimator?.addUpdateListener {
updatePosition()
counter++
if (counter % 2 == 0) {
updatePosition()
}
}
mAnimator?.start()
}
Expand All @@ -73,7 +80,6 @@ class EffectCloudDrawable(val type: Int, val clouds: Array<Drawable>) :
it.x = 0f
it.y = random.nextInt((mWidth * 0.7).toInt()).toFloat()
it.speed = random.nextFloat() * speed + speed
it.type = random.nextInt(clouds.size)
}
}
invalidateSelf()
Expand Down

0 comments on commit 82dbddf

Please sign in to comment.