Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed May 29, 2020
1 parent 52db9ec commit c1bd3c2
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 78 deletions.
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ allprojects {
}
}
dependencies {
implementation 'com.github.ibrahimsn98:android-particles:1.8'
implementation 'com.github.ibrahimsn98:android-particles:1.9'
}
```

# Attributions
```xml
<me.ibrahimsn.particle.ParticleView
android:id="@+id/particleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:particleCount="20"
app:minParticleRadius="5"
app:maxParticleRadius="12"
app:particleColor="@android:color/white"
app:backgroundColor="@android:color/holo_red_light" />
android:id="@+id/particleView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:particleCount="60"
app:particleMinRadius="3"
app:particleMaxRadius="10"
app:particlesBackgroundColor="#23262a"
app:particleColor="@android:color/holo_green_dark"
app:particleLineColor="@android:color/holo_green_dark"
app:particleLinesEnabled="true" />
```

# Usage
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
app:particleCount="60"
app:linesColor="@android:color/holo_green_dark"
app:particleMinRadius="3"
app:particleMaxRadius="10"
app:particlesBackgroundColor="#23262a"
app:particleColor="@android:color/holo_green_dark"
app:backgroundColor="#23262a"
app:minParticleRadius="3"
app:maxParticleRadius="10"/>
app:particleLineColor="@android:color/holo_green_dark"
app:particleLinesEnabled="true" />

</FrameLayout>
4 changes: 2 additions & 2 deletions particle/src/main/java/me/ibrahimsn/particle/Particle.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package me.ibrahimsn.particle

data class Particle constructor(
data class Particle (
var radius: Float,
var x: Float,
var y: Float,
var vx: Int,
var vy: Int,
var alpha: Int
)
)
221 changes: 164 additions & 57 deletions particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
Original file line number Diff line number Diff line change
@@ -1,34 +1,106 @@
package me.ibrahimsn.particle

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.graphics.*
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.annotation.ColorInt
import androidx.annotation.Dimension
import kotlin.math.min
import kotlin.math.sqrt
import kotlin.random.Random

class ParticleView : SurfaceView, SurfaceHolder.Callback {
class ParticleView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet,
defStyleAttr: Int = R.attr.ParticleViewStyle
) : SurfaceView(context, attrs, defStyleAttr), SurfaceHolder.Callback {

private val particles = mutableListOf<Particle>()
private var surfaceViewThread: SurfaceViewThread? = null

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
private var colorLines = Color.WHITE
private val path = Path()

// Attribute Defaults
private var _particleCount = 20

@Dimension
private var _particleMinRadius = 5

@Dimension
private var _particleMaxRadius = 10

@ColorInt
private var _particlesBackgroundColor = Color.BLACK

@ColorInt
private var _particleColor = Color.WHITE

@ColorInt
private var _particleLineColor = Color.WHITE

private var _particleLinesEnabled = true

// Core Attributes
var particleCount: Int
get() = _particleCount
set(value) {
_particleCount = when {
value > 50 -> 50
value < 0 -> 0
else -> value
}
}

var particleMinRadius: Int
@Dimension get() = _particleMinRadius
set(@Dimension value) {
_particleMinRadius = when {
value <= 0 -> 1
value >= particleMaxRadius -> 1
else -> value
}
}

var particleMaxRadius: Int
@Dimension get() = _particleMaxRadius
set(@Dimension value) {
_particleMaxRadius = when {
value <= particleMinRadius -> particleMinRadius + 1
else -> value
}
}

var particlesBackgroundColor: Int
@ColorInt get() = _particlesBackgroundColor
set(@ColorInt value) {
_particlesBackgroundColor = value
}

var particleColor: Int
@ColorInt get() = _particleColor
set(@ColorInt value) {
_particleColor = value
paintParticles.color = value
}

var particleLineColor: Int
@ColorInt get() = _particleLineColor
set(@ColorInt value) {
_particleLineColor = value
paintLines.color = value
}

var particleLinesEnabled: Boolean
get() = _particleLinesEnabled
set(value) {
_particleLinesEnabled = value
}

// Paints
private val paintParticles: Paint = Paint().apply {
isAntiAlias = true
style = Paint.Style.FILL
Expand All @@ -37,35 +109,64 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {

private val paintLines: Paint = Paint().apply {
isAntiAlias = true
style = Paint.Style.STROKE
style = Paint.Style.FILL_AND_STROKE
strokeWidth = 2F
}

constructor(context: Context) : super(context)
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
val a = context.obtainStyledAttributes(attrs, R.styleable.ParticleView, 0, 0)

isLinesEnabled = a.getBoolean(R.styleable.ParticleView_lines, isLinesEnabled)
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)
colorLines = a.getColor(R.styleable.ParticleView_linesColor, colorLines)
background = a.getColor(R.styleable.ParticleView_backgroundColor, background)
a.recycle()

paintParticles.color = colorParticles
paintLines.color = colorLines

if (particleCount > 50) particleCount = 50
if (minRadius <= 0) minRadius = 1
if (maxRadius <= minRadius) maxRadius = minRadius + 1
init {
obtainStyledAttributes(attrs, defStyleAttr)
if (holder != null) holder.addCallback(this)
hasSurface = false
}

if (holder != null) {
holder.addCallback(this)
private fun obtainStyledAttributes(attrs: AttributeSet, defStyleAttr: Int) {
val typedArray = context.obtainStyledAttributes(
attrs,
R.styleable.ParticleView,
defStyleAttr,
0
)

try {
particleCount = typedArray.getInt(
R.styleable.ParticleView_particleCount,
particleCount
)

particleMinRadius = typedArray.getInt(
R.styleable.ParticleView_particleMinRadius,
particleMinRadius
)

particleMaxRadius = typedArray.getInt(
R.styleable.ParticleView_particleMaxRadius,
particleMaxRadius
)

particlesBackgroundColor = typedArray.getColor(
R.styleable.ParticleView_particlesBackgroundColor,
particlesBackgroundColor
)

particleColor = typedArray.getColor(
R.styleable.ParticleView_particleColor,
particleColor
)

particleLineColor = typedArray.getColor(
R.styleable.ParticleView_particleLineColor,
particleLineColor
)

particleLinesEnabled = typedArray.getBoolean(
R.styleable.ParticleView_particleLinesEnabled,
particleLinesEnabled
)
} catch (e: Exception) {
e.printStackTrace()
} finally {
typedArray.recycle()
}

hasSurface = false
}

override fun surfaceCreated(holder: SurfaceHolder) {
Expand Down Expand Up @@ -100,7 +201,26 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
}

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

private fun setupParticles() {
if (!hasSetup) {
hasSetup = true
particles.clear()
for (i in 0 until particleCount) {
particles.add(
Particle(
Random.nextInt(particleMinRadius, particleMaxRadius).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() {
Expand All @@ -109,28 +229,15 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
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)
)
)
}
}
setupParticles()

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

synchronized (holder) {
canvas?.drawColor(background)
// Clear screen every frame
canvas?.drawColor(particlesBackgroundColor, PorterDuff.Mode.CLEAR)

for (i in 0 until particleCount) {
particles[i].x += particles[i].vx
Expand All @@ -149,7 +256,7 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {
}

canvas?.let {
if (isLinesEnabled) {
if (particleLinesEnabled) {
for (j in 0 until particleCount) {
if (i != j) {
linkParticles(it, particles[i], particles[j])
Expand Down Expand Up @@ -178,8 +285,8 @@ class ParticleView : SurfaceView, SurfaceHolder.Callback {

try {
join()
} catch (ignored: InterruptedException) {

} catch (e: InterruptedException) {
// ignored
}
}
}
Expand Down
14 changes: 8 additions & 6 deletions particle/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ParticleView">
<attr name="lines" format="boolean" />
<attr name="backgroundColor" format="color" />
<attr name="particleColor" format="color" />
<attr name="linesColor" format="color" />
<attr name="particleCount" format="integer" />
<attr name="minParticleRadius" format="integer" />
<attr name="maxParticleRadius" format="integer" />
<attr name="particleMinRadius" format="integer" />
<attr name="particleMaxRadius" format="integer" />
<attr name="particlesBackgroundColor" format="color" />
<attr name="particleColor" format="color" />
<attr name="particleLineColor" format="color" />
<attr name="particleLinesEnabled" format="boolean" />
</declare-styleable>

<attr name="ParticleViewStyle" format="reference" />
</resources>

0 comments on commit c1bd3c2

Please sign in to comment.