diff --git a/app/build.gradle b/app/build.gradle
index f30e821..07ef169 100644
--- a/app/build.gradle
+++ b/app/build.gradle
@@ -4,6 +4,7 @@ apply plugin: 'kotlin-android'
android {
compileSdkVersion 30
+ buildToolsVersion "29.0.3"
defaultConfig {
applicationId "me.ibrahimsn.particles"
minSdkVersion 15
diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml
index 12d069b..23a1bc7 100644
--- a/app/src/main/res/layout/activity_main.xml
+++ b/app/src/main/res/layout/activity_main.xml
@@ -5,7 +5,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
- android:background="@android:color/black"
+ android:background="@android:color/white"
tools:context=".MainActivity">
+ app:particleLinesEnabled="false" />
\ No newline at end of file
diff --git a/build.gradle b/build.gradle
index 73aa31c..65aa408 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,13 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
- ext.kotlin_version = '1.4.10'
+ ext.kotlin_version = '1.4.20'
repositories {
google()
jcenter()
}
dependencies {
- classpath 'com.android.tools.build:gradle:4.2.0-alpha15'
+ classpath 'com.android.tools.build:gradle:4.1.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 2957ada..14d499a 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
-distributionUrl=https\://services.gradle.org/distributions/gradle-6.7-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
diff --git a/particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt b/particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
index 59a58a6..679d544 100644
--- a/particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
+++ b/particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
@@ -1,8 +1,10 @@
package me.ibrahimsn.particle
import android.content.Context
+import android.content.res.TypedArray
import android.graphics.*
import android.util.AttributeSet
+import android.util.Log
import android.view.SurfaceHolder
import android.view.SurfaceView
import androidx.annotation.ColorInt
@@ -11,10 +13,11 @@ import kotlin.math.min
import kotlin.math.sqrt
import kotlin.random.Random
+
class ParticleView @JvmOverloads constructor(
- context: Context,
- attrs: AttributeSet,
- defStyleAttr: Int = R.attr.ParticleViewStyle
+ context: Context,
+ attrs: AttributeSet,
+ defStyleAttr: Int = R.attr.ParticleViewStyle
) : SurfaceView(context, attrs, defStyleAttr), SurfaceHolder.Callback {
private val particles = mutableListOf()
@@ -34,7 +37,7 @@ class ParticleView @JvmOverloads constructor(
private var _particleMaxRadius = 10
@ColorInt
- private var _particlesBackgroundColor = Color.BLACK
+ private var _particlesBackgroundColor = Color.TRANSPARENT
@ColorInt
private var _particleColor = Color.WHITE
@@ -44,6 +47,8 @@ class ParticleView @JvmOverloads constructor(
private var _particleLinesEnabled = true
+ private var _particlesSpeed = ParticlesSpeed.Default
+
// Core Attributes
var particleCount: Int
get() = _particleCount
@@ -100,6 +105,12 @@ class ParticleView @JvmOverloads constructor(
_particleLinesEnabled = value
}
+ var particleSpeed: ParticlesSpeed
+ get() = _particlesSpeed
+ set(value) {
+ _particlesSpeed = value
+ }
+
// Paints
private val paintParticles: Paint = Paint().apply {
isAntiAlias = true
@@ -121,47 +132,50 @@ class ParticleView @JvmOverloads constructor(
private fun obtainStyledAttributes(attrs: AttributeSet, defStyleAttr: Int) {
val typedArray = context.obtainStyledAttributes(
- attrs,
- R.styleable.ParticleView,
- defStyleAttr,
- 0
+ attrs,
+ R.styleable.ParticleView,
+ defStyleAttr,
+ 0
)
try {
particleCount = typedArray.getInt(
- R.styleable.ParticleView_particleCount,
- particleCount
+ R.styleable.ParticleView_particleCount,
+ particleCount
)
particleMinRadius = typedArray.getInt(
- R.styleable.ParticleView_particleMinRadius,
- particleMinRadius
+ R.styleable.ParticleView_particleMinRadius,
+ particleMinRadius
)
particleMaxRadius = typedArray.getInt(
- R.styleable.ParticleView_particleMaxRadius,
- particleMaxRadius
+ R.styleable.ParticleView_particleMaxRadius,
+ particleMaxRadius
)
particlesBackgroundColor = typedArray.getColor(
- R.styleable.ParticleView_particlesBackgroundColor,
- particlesBackgroundColor
+ R.styleable.ParticleView_particlesBackgroundColor,
+ particlesBackgroundColor
)
particleColor = typedArray.getColor(
- R.styleable.ParticleView_particleColor,
- particleColor
+ R.styleable.ParticleView_particleColor,
+ particleColor
)
particleLineColor = typedArray.getColor(
- R.styleable.ParticleView_particleLineColor,
- particleLineColor
+ R.styleable.ParticleView_particleLineColor,
+ particleLineColor
)
particleLinesEnabled = typedArray.getBoolean(
- R.styleable.ParticleView_particleLinesEnabled,
- particleLinesEnabled
+ R.styleable.ParticleView_particleLinesEnabled,
+ particleLinesEnabled
)
+
+ particleSpeed = typedArray.getEnum(R.styleable.ParticleView_particlesSpeed, ParticlesSpeed.Default)
+
} catch (e: Exception) {
e.printStackTrace()
} finally {
@@ -210,14 +224,14 @@ class ParticleView @JvmOverloads constructor(
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)
- )
+ 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)
+ )
)
}
}
@@ -232,10 +246,12 @@ class ParticleView @JvmOverloads constructor(
setupParticles()
while (running) {
+ if (particleSpeed == ParticlesSpeed.Slow)
+ sleep(20L)
try {
canvas = holder.lockCanvas()
- synchronized (holder) {
+ synchronized(holder) {
// Clear screen every frame
canvas?.drawColor(particlesBackgroundColor, PorterDuff.Mode.SRC)
@@ -312,4 +328,14 @@ class ParticleView @JvmOverloads constructor(
path.reset()
}
}
+
+
+ enum class ParticlesSpeed(val value: Int) {
+ Default(0),
+ Slow(1),
+ }
+
+ inline fun > TypedArray.getEnum(index: Int, default: T) =
+ getInt(index, -1).let { if (it >= 0) enumValues()[it] else default
+ }
}
\ No newline at end of file
diff --git a/particle/src/main/res/values/attrs.xml b/particle/src/main/res/values/attrs.xml
index b581210..9845d4f 100644
--- a/particle/src/main/res/values/attrs.xml
+++ b/particle/src/main/res/values/attrs.xml
@@ -1,6 +1,11 @@
+
+
+
+
+ >