Skip to content

Commit

Permalink
Merge pull request #1 from b1uebyte/threading
Browse files Browse the repository at this point in the history
Fixed API 15 support; fixed lags
  • Loading branch information
johnkil authored Dec 26, 2016
2 parents 5d2da6f + 6e364dc commit e55c343
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Change Log
==========

## Version 1.1.1

_2016-12-26_

* Fixed lags.
* Fixed API 15 support.

## Version 1.1.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
22 changes: 13 additions & 9 deletions snowfall/src/main/java/com/jetradarmobile/snowfall/SnowfallView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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()
}
}
}

0 comments on commit e55c343

Please sign in to comment.