Skip to content

Commit

Permalink
optimize setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Apr 19, 2020
1 parent ed503c3 commit 52db9ec
Showing 1 changed file with 24 additions and 28 deletions.
52 changes: 24 additions & 28 deletions particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
private val particles = mutableListOf<Particle>()
private var surfaceViewThread: SurfaceViewThread? = null

private var count = 20
private var particleCount = 20
private var minRadius = 5
private var maxRadius = 10
private var isLinesEnabled = true
private var hasSurface: Boolean = false
private var hasSetup = false

private var background = Color.BLACK
private var colorParticles = Color.WHITE
Expand All @@ -45,7 +46,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
val a = context.obtainStyledAttributes(attrs, R.styleable.ParticleView, 0, 0)

isLinesEnabled = a.getBoolean(R.styleable.ParticleView_lines, isLinesEnabled)
count = a.getInt(R.styleable.ParticleView_particleCount, count)
particleCount = a.getInt(R.styleable.ParticleView_particleCount, particleCount)
minRadius = a.getInt(R.styleable.ParticleView_minParticleRadius, minRadius)
maxRadius = a.getInt(R.styleable.ParticleView_maxParticleRadius, maxRadius)
colorParticles = a.getColor(R.styleable.ParticleView_particleColor, colorParticles)
Expand All @@ -56,7 +57,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
paintParticles.color = colorParticles
paintLines.color = colorLines

if (count > 50) count = 50
if (particleCount > 50) particleCount = 50
if (minRadius <= 0) minRadius = 1
if (maxRadius <= minRadius) maxRadius = minRadius + 1

Expand Down Expand Up @@ -94,49 +95,44 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {

override fun surfaceDestroyed(holder: SurfaceHolder) {
hasSurface = false

if (surfaceViewThread != null) {
surfaceViewThread!!.requestExitAndWait()
surfaceViewThread = null
}
surfaceViewThread?.requestExitAndWait()
surfaceViewThread = null
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, w: Int, h: Int) {
// Ignore
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
super.onSizeChanged(w, h, oldw, oldh)
if (particles.size == 0) {
for (i in 0 until count) {
particles.add(
Particle(
Random.nextInt(minRadius, maxRadius).toFloat(),
Random.nextInt(0, width).toFloat(),
Random.nextInt(0, height).toFloat(),
Random.nextInt(-2, 2),
Random.nextInt(-2, 2),
Random.nextInt(150, 255)
)
)
}
}
}

private inner class SurfaceViewThread : Thread() {

private var running = true
private var canvas: Canvas? = null

override fun run() {
if (!hasSetup) {
hasSetup = true
for (i in 0 until particleCount) {
particles.add(
Particle(
Random.nextInt(minRadius, maxRadius).toFloat(),
Random.nextInt(0, width).toFloat(),
Random.nextInt(0, height).toFloat(),
Random.nextInt(-2, 2),
Random.nextInt(-2, 2),
Random.nextInt(150, 255)
)
)
}
}

while (running) {
try {
canvas = holder.lockCanvas()

synchronized (holder) {
canvas?.drawColor(background)

for (i in 0 until count) {
for (i in 0 until particleCount) {
particles[i].x += particles[i].vx
particles[i].y += particles[i].vy

Expand All @@ -154,7 +150,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {

canvas?.let {
if (isLinesEnabled) {
for (j in 0 until count) {
for (j in 0 until particleCount) {
if (i != j) {
linkParticles(it, particles[i], particles[j])
}
Expand Down

0 comments on commit 52db9ec

Please sign in to comment.