diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d11ed4..169f3cd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,9 +5,12 @@ Change Log _2016-12-26_ - * Fixed lags. + * Support different start position on y-axis for snowflakes (attr `snowflakesAlreadyFalling`). + * Rename attr `snowflakeFadingEnabled` to `snowflakesFadingEnabled`. + * Fixed animation lags. * Fixed API 15 support. + ## Version 1.1.0 _2016-12-25_ diff --git a/README.md b/README.md index a8c50d1..54d8ba7 100644 --- a/README.md +++ b/README.md @@ -55,14 +55,15 @@ Fully customized implementation: android:layout_width="match_parent" android:layout_height="match_parent" app:snowflakesNum="250" - app:snowflakeAlphaMin="200" - app:snowflakeAlphaMax="250" + app:snowflakeAlphaMin="150" + app:snowflakeAlphaMax="255" app:snowflakeAngleMax="5" app:snowflakeSizeMin="8dp" app:snowflakeSizeMax="32dp" app:snowflakeSpeedMin="4" app:snowflakeSpeedMax="12" - app:snowflakeFadingEnabled="true" + app:snowflakesFadingEnabled="true" + app:snowflakesAlreadyFalling="false" app:snowflakeImage="@drawable/snowflake"/> ``` diff --git a/snowfall-sample/src/main/res/drawable-hdpi/snowflake.png b/snowfall-sample/src/main/res/drawable-hdpi/snowflake.png index 7a9b05c..f775ffb 100644 Binary files a/snowfall-sample/src/main/res/drawable-hdpi/snowflake.png and b/snowfall-sample/src/main/res/drawable-hdpi/snowflake.png differ diff --git a/snowfall-sample/src/main/res/drawable-mdpi/snowflake.png b/snowfall-sample/src/main/res/drawable-mdpi/snowflake.png index 5bce7d9..89e0812 100644 Binary files a/snowfall-sample/src/main/res/drawable-mdpi/snowflake.png and b/snowfall-sample/src/main/res/drawable-mdpi/snowflake.png differ diff --git a/snowfall-sample/src/main/res/drawable-xhdpi/snowflake.png b/snowfall-sample/src/main/res/drawable-xhdpi/snowflake.png index c14246f..56b9082 100644 Binary files a/snowfall-sample/src/main/res/drawable-xhdpi/snowflake.png and b/snowfall-sample/src/main/res/drawable-xhdpi/snowflake.png differ diff --git a/snowfall-sample/src/main/res/drawable-xxhdpi/snowflake.png b/snowfall-sample/src/main/res/drawable-xxhdpi/snowflake.png index 3851e9c..87ebfcf 100644 Binary files a/snowfall-sample/src/main/res/drawable-xxhdpi/snowflake.png and b/snowfall-sample/src/main/res/drawable-xxhdpi/snowflake.png differ diff --git a/snowfall-sample/src/main/res/drawable-xxxhdpi/snowflake.png b/snowfall-sample/src/main/res/drawable-xxxhdpi/snowflake.png index 6933d11..db0b977 100644 Binary files a/snowfall-sample/src/main/res/drawable-xxxhdpi/snowflake.png and b/snowfall-sample/src/main/res/drawable-xxxhdpi/snowflake.png differ diff --git a/snowfall-sample/src/main/res/layout/activity_snowfall.xml b/snowfall-sample/src/main/res/layout/activity_snowfall.xml index 5d30849..a18d1c2 100644 --- a/snowfall-sample/src/main/res/layout/activity_snowfall.xml +++ b/snowfall-sample/src/main/res/layout/activity_snowfall.xml @@ -11,14 +11,15 @@ android:layout_width="match_parent" android:layout_height="match_parent" app:snowflakesNum="250" - app:snowflakeAlphaMin="200" - app:snowflakeAlphaMax="250" + app:snowflakeAlphaMin="150" + app:snowflakeAlphaMax="255" app:snowflakeAngleMax="5" app:snowflakeSizeMin="8dp" app:snowflakeSizeMax="32dp" app:snowflakeSpeedMin="4" app:snowflakeSpeedMax="12" - app:snowflakeFadingEnabled="true" + app:snowflakesFadingEnabled="true" + app:snowflakesAlreadyFalling="false" app:snowflakeImage="@drawable/snowflake"/> \ No newline at end of file diff --git a/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt b/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt index 7f340a1..0325513 100644 --- a/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt +++ b/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt @@ -35,7 +35,8 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) private val DEFAULT_SNOWFLAKE_SIZE_MAX_IN_DP = 8 private val DEFAULT_SNOWFLAKE_SPEED_MIN = 2 private val DEFAULT_SNOWFLAKE_SPEED_MAX = 8 - private val DEFAULT_SNOWFLAKE_FADING_ENABLED = false + private val DEFAULT_SNOWFLAKES_FADING_ENABLED = false + private val DEFAULT_SNOWFLAKES_ALREADY_FALLING = false private val snowflakesNum: Int private val snowflakeImage: Bitmap? @@ -46,7 +47,8 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) private val snowflakeSizeMaxInPx: Int private val snowflakeSpeedMin: Int private val snowflakeSpeedMax: Int - private val snowflakeFadingEnabled: Boolean + private val snowflakesFadingEnabled: Boolean + private val snowflakesAlreadyFalling: Boolean private val snowflakes: MutableList private val updateSnowflakesThread: UpdateSnowflakesThread @@ -62,7 +64,8 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) snowflakeSizeMaxInPx = a.getDimensionPixelSize(R.styleable.SnowfallView_snowflakeSizeMax, dpToPx(DEFAULT_SNOWFLAKE_SIZE_MAX_IN_DP)) snowflakeSpeedMin = a.getInt(R.styleable.SnowfallView_snowflakeSpeedMin, DEFAULT_SNOWFLAKE_SPEED_MIN) snowflakeSpeedMax = a.getInt(R.styleable.SnowfallView_snowflakeSpeedMax, DEFAULT_SNOWFLAKE_SPEED_MAX) - snowflakeFadingEnabled = a.getBoolean(R.styleable.SnowfallView_snowflakeFadingEnabled, DEFAULT_SNOWFLAKE_FADING_ENABLED) + snowflakesFadingEnabled = a.getBoolean(R.styleable.SnowfallView_snowflakesFadingEnabled, DEFAULT_SNOWFLAKES_FADING_ENABLED) + snowflakesAlreadyFalling = a.getBoolean(R.styleable.SnowfallView_snowflakesAlreadyFalling, DEFAULT_SNOWFLAKES_ALREADY_FALLING) a.recycle() snowflakes = ArrayList(snowflakesNum) @@ -70,11 +73,6 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) updateSnowflakesThread = UpdateSnowflakesThread() } - private fun updateSnowflakes() { - updateSnowflakesThread.handler.post { snowflakes.forEach { it.update() } } - ViewCompat.postInvalidateOnAnimation(this) - } - private fun dpToPx(dp: Int): Int { return (dp * resources.displayMetrics.density).toInt() } @@ -87,7 +85,7 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) alphaMin = snowflakeAlphaMin, alphaMax = snowflakeAlphaMax, angleMax = snowflakeAngleMax, sizeMinInPx = snowflakeSizeMinInPx, sizeMaxInPx = snowflakeSizeMaxInPx, speedMin = snowflakeSpeedMin, speedMax = snowflakeSpeedMax, - fadingEnabled = snowflakeFadingEnabled) + fadingEnabled = snowflakesFadingEnabled, alreadyFalling = snowflakesAlreadyFalling) snowflakes.addAll(Array(snowflakesNum, { Snowflake(snowflakeParams) })) } @@ -97,6 +95,13 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) updateSnowflakes() } + private fun updateSnowflakes() { + updateSnowflakesThread.handler.post { + snowflakes.forEach { it.update() } + ViewCompat.postInvalidateOnAnimation(this) + } + } + private inner class UpdateSnowflakesThread() : HandlerThread("SnowflakesComputations") { val handler by lazy { Handler(looper) } diff --git a/snowfall/src/main/java/com/jetradarmobile/snowfall/Snowflake.kt b/snowfall/src/main/java/com/jetradarmobile/snowfall/Snowflake.kt index 9f8177f..33e44d2 100644 --- a/snowfall/src/main/java/com/jetradarmobile/snowfall/Snowflake.kt +++ b/snowfall/src/main/java/com/jetradarmobile/snowfall/Snowflake.kt @@ -44,6 +44,9 @@ internal class Snowflake(val params: Params) { init { init() + if (!params.alreadyFalling) { + positionY = positionY - params.parentHeight - size + } } private fun init() { @@ -96,5 +99,6 @@ internal class Snowflake(val params: Params) { val sizeMaxInPx: Int, val speedMin: Int, val speedMax: Int, - val fadingEnabled: Boolean) + val fadingEnabled: Boolean, + val alreadyFalling: Boolean) } \ No newline at end of file diff --git a/snowfall/src/main/res/values/attrs.xml b/snowfall/src/main/res/values/attrs.xml index d8ef1f5..63d3d7e 100644 --- a/snowfall/src/main/res/values/attrs.xml +++ b/snowfall/src/main/res/values/attrs.xml @@ -27,7 +27,8 @@ - + + \ No newline at end of file