From 6e364dc8eb3d851ea06001b4d3dce011a9aab55d Mon Sep 17 00:00:00 2001 From: b1uebyte Date: Mon, 26 Dec 2016 05:36:33 +0300 Subject: [PATCH] Fixed API 15 support; fixed lags --- CHANGELOG.md | 6 +++++ README.md | 2 +- .../jetradarmobile/snowfall/SnowfallView.kt | 22 +++++++++++-------- 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c049551..8d11ed4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,12 @@ Change Log ========== +## Version 1.1.1 + +_2016-12-26_ + + * Fixed lags. + * Fixed API 15 support. ## Version 1.1.0 diff --git a/README.md b/README.md index 1688aaa..a8c50d1 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ Add the dependency ```groovy dependencies { - compile 'com.github.jetradarmobile:android-snowfall:1.1.0' + compile 'com.github.jetradarmobile:android-snowfall:1.1.1' } ``` diff --git a/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt b/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt index 700ecf0..7f340a1 100644 --- a/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt +++ b/snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt @@ -19,6 +19,9 @@ package com.jetradarmobile.snowfall import android.content.Context import android.graphics.Bitmap import android.graphics.Canvas +import android.os.Handler +import android.os.HandlerThread +import android.support.v4.view.ViewCompat import android.util.AttributeSet import android.view.View import java.util.ArrayList @@ -65,7 +68,11 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) snowflakes = ArrayList(snowflakesNum) updateSnowflakesThread = UpdateSnowflakesThread() - updateSnowflakesThread.start() + } + + private fun updateSnowflakes() { + updateSnowflakesThread.handler.post { snowflakes.forEach { it.update() } } + ViewCompat.postInvalidateOnAnimation(this) } private fun dpToPx(dp: Int): Int { @@ -87,17 +94,14 @@ class SnowfallView(context: Context, attrs: AttributeSet) : View(context, attrs) override fun onDraw(canvas: Canvas) { super.onDraw(canvas) snowflakes.forEach { it.draw(canvas) } + updateSnowflakes() } - private inner class UpdateSnowflakesThread : Thread() { - private val FPS = 10L + private inner class UpdateSnowflakesThread() : HandlerThread("SnowflakesComputations") { + val handler by lazy { Handler(looper) } - override fun run() { - while (true) { - try { Thread.sleep(FPS) } catch (ignored: InterruptedException) {} - snowflakes.forEach { it.update() } - postInvalidateOnAnimation() - } + init { + start() } } } \ No newline at end of file