Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adding particles speed enum #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ apply plugin: 'kotlin-android'

android {
compileSdkVersion 30
buildToolsVersion "29.0.3"
defaultConfig {
applicationId "me.ibrahimsn.particles"
minSdkVersion 15
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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">

<me.ibrahimsn.particle.ParticleView
Expand All @@ -15,9 +15,10 @@
app:particleCount="60"
app:particleMinRadius="3"
app:particleMaxRadius="10"
app:particlesBackgroundColor="#23262a"
app:particlesSpeed="Slow"
app:particlesBackgroundColor="@android:color/white"
app:particleColor="@android:color/holo_green_dark"
app:particleLineColor="@android:color/holo_green_dark"
app:particleLinesEnabled="true" />
app:particleLinesEnabled="false" />

</FrameLayout>
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you decrease the distribution version?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

didnt find a reason for it to be 6.7-bin

88 changes: 57 additions & 31 deletions particle/src/main/java/me/ibrahimsn/particle/ParticleView.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<Particle>()
Expand All @@ -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
Expand All @@ -44,6 +47,8 @@ class ParticleView @JvmOverloads constructor(

private var _particleLinesEnabled = true

private var _particlesSpeed = ParticlesSpeed.Default

// Core Attributes
var particleCount: Int
get() = _particleCount
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
)
)
}
}
Expand All @@ -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)

Expand Down Expand Up @@ -312,4 +328,14 @@ class ParticleView @JvmOverloads constructor(
path.reset()
}
}


enum class ParticlesSpeed(val value: Int) {
Default(0),
Slow(1),
}

inline fun <reified T : Enum<T>> TypedArray.getEnum(index: Int, default: T) =
getInt(index, -1).let { if (it >= 0) enumValues<T>()[it] else default
}
}
5 changes: 5 additions & 0 deletions particle/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="ParticleView">
<attr name="particlesSpeed" format="enum">
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we use enum? We can use integer value with better customization options i think.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

modularized code is more user friendly

<enum name="Slow" value="1"/>
<enum name="Default" value="0"/>
</attr>
<!--<attr name="particlesSpeed" format="integer"/-->>
<attr name="particleCount" format="integer" />
<attr name="particleMinRadius" format="integer" />
<attr name="particleMaxRadius" format="integer" />
Expand Down