Skip to content

Commit

Permalink
rain effect
Browse files Browse the repository at this point in the history
  • Loading branch information
wsj1024 committed Jul 30, 2021
1 parent c2ded2d commit 0c28c37
Show file tree
Hide file tree
Showing 12 changed files with 95 additions and 54 deletions.
9 changes: 6 additions & 3 deletions app/src/main/res/layout/layout_weather_notify.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/ivWeather"
android:text="浦东新区"
android:textColor="@color/color_666" />
android:textColor="@color/color_666"
android:textSize="14sp" />

<TextView
android:id="@+id/tvWeather"
Expand All @@ -29,7 +30,8 @@
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="@+id/ivWeather"
android:text="" />
android:text=""
android:textSize="14sp" />

<TextView
android:id="@+id/tvTemp"
Expand All @@ -39,7 +41,8 @@
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:layout_toRightOf="@+id/tvWeather"
android:text="25°C" />
android:text="25°C"
android:textSize="14sp" />

<ImageView
android:layout_width="45dp"
Expand Down
1 change: 1 addition & 0 deletions bg/src/main/java/me/wsj/bg/ShowActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import me.wsj.lib.EffectUtil
import me.wsj.lib.specialeffects.ICancelable
import me.wsj.lib.utils.IconUtils

class ShowActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down
1 change: 1 addition & 0 deletions bg/src/main/java/me/wsj/bg/adapter/AllAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import me.wsj.bg.R
import me.wsj.bg.bean.WeatherBean
import me.wsj.lib.utils.IconUtils

class AllAdapter(
val context: Context,
Expand Down
Binary file added img/effect.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/widget.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 12 additions & 5 deletions lib/src/main/java/me/wsj/lib/EffectUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,21 @@ class EffectUtil {
2 -> {
if (isDay) {
EffectCloudDrawable(
context.resources.getDrawable(R.drawable.cloudy_day_2),
context.resources.getDrawable(R.drawable.cloudy_day_1),
context.resources.getDrawable(R.drawable.cloudy_day_3)
1,
arrayOf(
context.resources.getDrawable(R.drawable.cloudy_day_1),
context.resources.getDrawable(R.drawable.cloudy_day_2),
context.resources.getDrawable(R.drawable.cloudy_day_3),
context.resources.getDrawable(R.drawable.cloudy_day_4)
)
)
} else
EffectCloudDrawable(
context.resources.getDrawable(R.drawable.cloudy_day_2),
context.resources.getDrawable(R.drawable.cloudy_day_1)
1,
arrayOf(
context.resources.getDrawable(R.drawable.cloudy_night1),
context.resources.getDrawable(R.drawable.cloudy_night2)
)
)
}
4 -> { // 中雨
Expand Down
82 changes: 48 additions & 34 deletions lib/src/main/java/me/wsj/lib/specialeffects/EffectCloudDrawable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,89 @@ import android.graphics.PixelFormat
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.view.animation.LinearInterpolator
import me.wsj.lib.specialeffects.entity.Cloud
import me.wsj.lib.specialeffects.entity.Rain
import per.wsj.commonlib.utils.LogUtil
import java.util.*
import kotlin.math.PI
import kotlin.math.sin

/**
* create by shiju.wang
* cloud
*/
class EffectCloudDrawable(val cloud1: Drawable, val cloud2: Drawable, var cloud3: Drawable? = null) :
class EffectCloudDrawable(val type: Int, val clouds: Array<Drawable>) :
Drawable(), ICancelable {

private var mAnimator: ValueAnimator? = null

private var mWidth = 0
val random = Random()

private var rate1 = 0f
private var y1 = 0
val speed = 1

private var y2 = 0
private var mAnimator: ValueAnimator? = null

private var y3 = 0
private var mWidth = 0

init {
startAnim()
}
val cloudList = ArrayList<Cloud>()

override fun onBoundsChange(bounds: Rect) {
super.onBoundsChange(bounds)

mWidth = bounds.right - bounds.left

y1 = (mWidth * 0.05).toInt()
y2 = (mWidth * 0.3).toInt()
y3 = (mWidth * 0.54).toInt()
for (i in 0 until 3) {
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)
)
)
}

startAnim()
}

private fun startAnim() {
if (mAnimator == null) {
mAnimator = ValueAnimator.ofFloat(0f, 1f)
mAnimator?.repeatCount = ValueAnimator.INFINITE
mAnimator?.duration = 30000
mAnimator?.duration = 3000
mAnimator?.interpolator = LinearInterpolator()
mAnimator?.addUpdateListener {
rate1 = it.animatedValue as Float

invalidateSelf()
updatePosition()
}
mAnimator?.start()
}
}

private fun updatePosition() {
cloudList.forEach {
it.x += it.speed
val drawable = clouds[it.type]
if (it.x > mWidth + drawable.intrinsicWidth) {
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()
}

override fun draw(canvas: Canvas) {
val total1 = cloud1.intrinsicWidth + mWidth
val curX1 = (-cloud1.intrinsicWidth + total1 * rate1).toInt()

cloud1.setBounds(curX1, y1, curX1 + cloud1.intrinsicWidth, y1 + cloud1.intrinsicHeight)
cloud1.draw(canvas)

val total2 = cloud2.intrinsicWidth * 4 + mWidth
val curX2 = (-cloud2.intrinsicWidth * 2 + total2 * rate1).toInt()
cloud2.setBounds(curX2, y2, curX2 + cloud2.intrinsicWidth, y2 + cloud2.intrinsicHeight)
cloud2.draw(canvas)

cloud3?.let {
val total3 = it.intrinsicWidth * 3 + mWidth
val curX3 = (-it.intrinsicWidth * 2 + total3 * rate1).toInt()
it.setBounds(curX3, y3, curX3 + it.intrinsicWidth, y3 + it.intrinsicHeight)
it.draw(canvas)
for (cloud in cloudList) {
val drawable = clouds[cloud.type]
drawable.setBounds(
(cloud.x - drawable.intrinsicWidth).toInt(),
(cloud.y).toInt(),
cloud.x.toInt(),
(cloud.y + drawable.intrinsicHeight).toInt()
)

drawable.draw(canvas)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.animation.ValueAnimator
import android.graphics.*
import android.graphics.drawable.Drawable
import android.view.animation.AccelerateInterpolator
import me.wsj.lib.specialeffects.rain.Rain
import me.wsj.lib.specialeffects.entity.Rain
import per.wsj.commonlib.utils.LogUtil
import java.util.*
import kotlin.collections.ArrayList
Expand Down Expand Up @@ -52,17 +52,17 @@ class EffectRainDrawable(val type: Int, val rains: Array<Drawable>) :
animator.repeatCount = -1
animator.interpolator = AccelerateInterpolator()
animator.addUpdateListener {
updateParticle()
updatePosition()
}
}

private fun updateParticle() {
private fun updatePosition() {
rainList.forEach {
it.y += it.speed
val drawable = rains[it.type]
if (it.y > mHeight + drawable.intrinsicHeight) {
it.y = 0f
it.speed = random.nextInt(speed) + speed
it.speed = random.nextInt(speed + 1) + speed
it.type = random.nextInt(rains.size)
}
}
Expand All @@ -78,7 +78,7 @@ class EffectRainDrawable(val type: Int, val rains: Array<Drawable>) :
val nextY = random.nextInt(mHeight)
rainList.add(
Rain(
nextX.toFloat(), nextY.toFloat(), random.nextInt(speed) + speed,
nextX.toFloat(), nextY.toFloat(), random.nextInt(speed + 1) + speed,
random.nextInt(rains.size)
)
)
Expand Down Expand Up @@ -115,6 +115,7 @@ class EffectRainDrawable(val type: Int, val rains: Array<Drawable>) :
}

override fun cancel() {
animator.removeAllListeners()
animator.cancel()
LogUtil.d("Effect4Drawable cancel ---------------------------> ")
}
Expand Down
8 changes: 8 additions & 0 deletions lib/src/main/java/me/wsj/lib/specialeffects/entity/Cloud.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.wsj.lib.specialeffects.entity

class Cloud(
var x: Float,
var y: Float,
var speed: Float,
var type: Int = 0
)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package me.wsj.lib.specialeffects.rain
package me.wsj.lib.specialeffects.entity

class Rain(
var x: Float,
Expand Down
9 changes: 6 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@
- 天气特效

## 预览
![weather_info](/img/home.gif) ![hourly](/img/hourly.gif)
![weather_info](img/home.gif) ![effect](img/effect.gif)

![15dforecast](/img/15d.gif) ![sun_moon](/img/sun.gif)
![hourly](img/hourly.gif) ![15dforecast](img/15d.gif)

![sun_moon](img/sun.gif) ![widget](img/widget.gif)

![add_city](img/add_city.png) ![settings](img/settings.png)

![add_city](/img/add_city.png) ![settings](/img/settings.png)


## TODO
Expand Down
9 changes: 6 additions & 3 deletions readme_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@

## Preview

![weather_info](/img/home.gif) ![hourly](/img/hourly.gif)
![weather_info](img/home.gif) ![effect](img/effect.gif)

![15dforecast](/img/15d.gif) ![sun_moon](/img/sun.gif)
![hourly](img/hourly.gif) ![15dforecast](img/15d.gif)

![sun_moon](img/sun.gif) ![widget](img/widget.gif)

![add_city](img/add_city.png) ![settings](img/settings.png)

![add_city](/img/add_city.png) ![settings](/img/settings.png)



Expand Down

0 comments on commit 0c28c37

Please sign in to comment.