@@ -17,11 +17,12 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
1717 private val particles = mutableListOf<Particle >()
1818 private var surfaceViewThread: SurfaceViewThread ? = null
1919
20- private var count = 20
20+ private var particleCount = 20
2121 private var minRadius = 5
2222 private var maxRadius = 10
2323 private var isLinesEnabled = true
2424 private var hasSurface: Boolean = false
25+ private var hasSetup = false
2526
2627 private var background = Color .BLACK
2728 private var colorParticles = Color .WHITE
@@ -45,7 +46,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
4546 val a = context.obtainStyledAttributes(attrs, R .styleable.ParticleView , 0 , 0 )
4647
4748 isLinesEnabled = a.getBoolean(R .styleable.ParticleView_lines , isLinesEnabled)
48- count = a.getInt(R .styleable.ParticleView_particleCount , count )
49+ particleCount = a.getInt(R .styleable.ParticleView_particleCount , particleCount )
4950 minRadius = a.getInt(R .styleable.ParticleView_minParticleRadius , minRadius)
5051 maxRadius = a.getInt(R .styleable.ParticleView_maxParticleRadius , maxRadius)
5152 colorParticles = a.getColor(R .styleable.ParticleView_particleColor , colorParticles)
@@ -56,7 +57,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
5657 paintParticles.color = colorParticles
5758 paintLines.color = colorLines
5859
59- if (count > 50 ) count = 50
60+ if (particleCount > 50 ) particleCount = 50
6061 if (minRadius <= 0 ) minRadius = 1
6162 if (maxRadius <= minRadius) maxRadius = minRadius + 1
6263
@@ -94,49 +95,44 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
9495
9596 override fun surfaceDestroyed (holder : SurfaceHolder ) {
9697 hasSurface = false
97-
98- if (surfaceViewThread != null ) {
99- surfaceViewThread!! .requestExitAndWait()
100- surfaceViewThread = null
101- }
98+ surfaceViewThread?.requestExitAndWait()
99+ surfaceViewThread = null
102100 }
103101
104102 override fun surfaceChanged (holder : SurfaceHolder , format : Int , w : Int , h : Int ) {
105103 // Ignore
106104 }
107105
108- override fun onSizeChanged (w : Int , h : Int , oldw : Int , oldh : Int ) {
109- super .onSizeChanged(w, h, oldw, oldh)
110- if (particles.size == 0 ) {
111- for (i in 0 until count) {
112- particles.add(
113- Particle (
114- Random .nextInt(minRadius, maxRadius).toFloat(),
115- Random .nextInt(0 , width).toFloat(),
116- Random .nextInt(0 , height).toFloat(),
117- Random .nextInt(- 2 , 2 ),
118- Random .nextInt(- 2 , 2 ),
119- Random .nextInt(150 , 255 )
120- )
121- )
122- }
123- }
124- }
125-
126106 private inner class SurfaceViewThread : Thread () {
127107
128108 private var running = true
129109 private var canvas: Canvas ? = null
130110
131111 override fun run () {
112+ if (! hasSetup) {
113+ hasSetup = true
114+ for (i in 0 until particleCount) {
115+ particles.add(
116+ Particle (
117+ Random .nextInt(minRadius, maxRadius).toFloat(),
118+ Random .nextInt(0 , width).toFloat(),
119+ Random .nextInt(0 , height).toFloat(),
120+ Random .nextInt(- 2 , 2 ),
121+ Random .nextInt(- 2 , 2 ),
122+ Random .nextInt(150 , 255 )
123+ )
124+ )
125+ }
126+ }
127+
132128 while (running) {
133129 try {
134130 canvas = holder.lockCanvas()
135131
136132 synchronized (holder) {
137133 canvas?.drawColor(background)
138134
139- for (i in 0 until count ) {
135+ for (i in 0 until particleCount ) {
140136 particles[i].x + = particles[i].vx
141137 particles[i].y + = particles[i].vy
142138
@@ -154,7 +150,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
154150
155151 canvas?.let {
156152 if (isLinesEnabled) {
157- for (j in 0 until count ) {
153+ for (j in 0 until particleCount ) {
158154 if (i != j) {
159155 linkParticles(it, particles[i], particles[j])
160156 }
0 commit comments