Skip to content

Commit

Permalink
Merge branch 'meteorites-bitch!'
Browse files Browse the repository at this point in the history
  • Loading branch information
sofakingforever committed Oct 14, 2018
2 parents d8f8f1a + ed510ee commit b2b66e6
Show file tree
Hide file tree
Showing 9 changed files with 199 additions and 31 deletions.
4 changes: 4 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
android:layout_height="match_parent"
app:starsView_bigStarThreshold="20dp"
app:starsView_maxStarSize="4dp"
app:starsView_meteoritesColors="@array/meteorites_colors"
app:starsView_meteoritesEnabled="true"
app:starsView_meteoritesInterval="2000"
app:starsView_minStarSize="1dp"
app:starsView_starColors="@array/star_colors_small"
app:starsView_starCount="30" />
Expand All @@ -24,6 +27,7 @@
android:layout_height="match_parent"
app:starsView_bigStarThreshold="8dp"
app:starsView_maxStarSize="16dp"
app:starsView_meteoritesEnabled="false"
app:starsView_minStarSize="8dp"
app:starsView_starColors="@array/star_colors_big"
app:starsView_starCount="2" />
Expand Down
12 changes: 11 additions & 1 deletion app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<resources>



<integer-array name="star_colors_small">

<!-- This is how you can configure the ratio of star colors-->
Expand All @@ -24,5 +23,16 @@
<item>@color/star_color_3</item>
</integer-array>

<integer-array name="meteorites_colors">
<item>@color/star_color_1</item>
<item>@color/star_color_2</item>
<item>@color/star_color_1</item>
<item>@color/star_color_1</item>
<item>@color/star_color_1</item>
<item>@color/star_color_3</item>
<item>@color/star_color_1</item>
<item>@color/star_color_1</item>
<item>@color/star_color_4</item>
</integer-array>

</resources>
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
ext.kotlin_version = '1.2.61'
ext.kotlin_version = '1.2.50'
ext.kotlin_version = '1.2.71'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.4'
classpath 'com.android.tools.build:gradle:3.2.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

// NOTE: Do not place your application dependencies here; they belong
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Tue Aug 21 22:29:52 IDT 2018
#Sun Oct 14 03:02:00 IDT 2018
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-4.6-all.zip
4 changes: 2 additions & 2 deletions stars/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ buildscript {
}
}

def versionNameStr = "1.0.3"
def versionNameStr = "1.0.4"

android {
compileSdkVersion 28
Expand All @@ -23,7 +23,7 @@ android {
defaultConfig {
minSdkVersion 21
targetSdkVersion 28
versionCode 103
versionCode 104
versionName versionNameStr

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
Expand Down
68 changes: 57 additions & 11 deletions stars/src/main/java/com/sofakingforever/stars/AnimatedStarsView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ constructor(

private var starCount: Int
private var starColors: IntArray
private var meteoritesColors : IntArray
private var bigStarThreshold: Int
private var minStarSize: Int
private var maxStarSize: Int
Expand All @@ -43,6 +44,7 @@ constructor(


private var stars: List<Star> = emptyList()
private var meteorEntity: MeteorEntity? = null

private lateinit var timer: Timer
private lateinit var task: TimerTask
Expand All @@ -51,6 +53,11 @@ constructor(
private var initiated: Boolean = false
private var started: Boolean = false


private var meteoritesEnabled: Boolean

private var meteoritesInterval: Int

/**
* init view's attributes
*/
Expand All @@ -65,12 +72,22 @@ constructor(
bigStarThreshold = array.getDimensionPixelSize(R.styleable.AnimatedStarsView_starsView_bigStarThreshold, Integer.MAX_VALUE)
starConstraints = Star.StarConstraints(minStarSize, maxStarSize, bigStarThreshold)

meteoritesColors = intArrayOf()
meteoritesEnabled = array.getBoolean(R.styleable.AnimatedStarsView_starsView_meteoritesEnabled, true)
meteoritesInterval = array.getInt(R.styleable.AnimatedStarsView_starsView_meteoritesInterval, 5000)

val starColorsArrayId = array.getResourceId(R.styleable.AnimatedStarsView_starsView_starColors, 0)
val meteoritesColorsArrayId = array.getResourceId(R.styleable.AnimatedStarsView_starsView_meteoritesColors, 0)

if (starColorsArrayId != 0) {
starColors = context.resources.getIntArray(starColorsArrayId)
}

if (meteoritesColorsArrayId!= 0) {
meteoritesColors = context.resources.getIntArray(meteoritesColorsArrayId)

}

array.recycle()

}
Expand Down Expand Up @@ -135,8 +152,10 @@ constructor(
var newCanvas = canvas

if (stars.isNotEmpty()) {
// draw each star on the canvas
stars.forEach { newCanvas = it.draw(newCanvas) }
// onDraw each star on the canvas
stars.forEach { newCanvas = it.onDraw(newCanvas) }

newCanvas = meteorEntity?.onDraw(newCanvas)

// reset flag
starsCalculatedFlag = false
Expand All @@ -148,25 +167,51 @@ constructor(
}


private lateinit var onDoneListener: () -> Unit

/**
* create x stars with a random point location and opacity
*/
private fun initStars() {

if (!started) return
val generateColor = { starColors[random.nextInt(starColors.size)] }

onDoneListener = {

if (meteoritesEnabled) {
postDelayed({

meteorEntity = MeteorEntity(starConstraints = starConstraints,
x = viewWidth,
y = Math.round(Math.random() * viewHeight).toInt(),
color = meteoritesColors[random.nextInt(starColors.size)],
viewWidth = viewWidth,
viewHeight = viewHeight,
colorListener = generateColor,
onDoneListener = onDoneListener
)

}, meteoritesInterval.toLong())
}

}

stars = List(starCount) {

Star(
starConstraints, // constraints
Math.round(Math.random() * viewWidth).toInt(), // x
Math.round(Math.random() * viewHeight).toInt(), // y
Math.random(), // opacity
starColors[it % starColors.size], // color
viewWidth,
viewHeight
// function for generating new color
) { starColors[random.nextInt(starColors.size)] }
starConstraints = starConstraints,
x = 0,
y = 0,
randomizeLocation = true,
opacity = Math.random(),
color = starColors[it % starColors.size],
viewWidth = viewWidth,
viewHeight = viewHeight,
colorListener = generateColor
)
}
onDoneListener.invoke()

// so we know lateinit var was initiated
initiated = true
Expand All @@ -185,6 +230,7 @@ constructor(

// recalculate stars position and alpha on a background thread
stars.forEach { it.calculateFrame(viewWidth, viewHeight) }
meteorEntity?.calculateFrame(viewWidth, viewHeight)
starsCalculatedFlag = true

// then post to ui thread
Expand Down
103 changes: 103 additions & 0 deletions stars/src/main/java/com/sofakingforever/stars/MeteorEntity.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.sofakingforever.stars

import android.graphics.Canvas
import android.graphics.Paint

internal class MeteorEntity(starConstraints: Star.StarConstraints, var x: Int, var y: Int, var color: Int, viewWidth: Int, viewHeight: Int, private val colorListener: () -> Int, private val onDoneListener: () -> Unit) {

// private val length: Double = (starConstraints.minStarSize + Math.random() * (starConstraints.maxStarSize - starConstraints.minStarSize))
private val fillPaint = Paint(Paint.ANTI_ALIAS_FLAG)
// private val strokePaint = Paint(Paint.ANTI_ALIAS_FLAG)

private var shape: Star.StarShape = Star.StarShape.Dot

private val meteor: Meteor = Meteor(starConstraints, x, y, color, viewWidth, viewHeight, colorListener, onDoneListener)
private val trail: Trail = Trail(meteor.star.length, meteor.star.fillPaint)

init {

// init fill paint for small and big stars
fillPaint.color = color


}

fun calculateFrame(viewWidth: Int, viewHeight: Int) {
trail.addPixel(meteor.star.x, meteor.star.y)
trail.calculatePixels()
meteor.calculateFrame(viewWidth, viewHeight)
}

fun onDraw(canvas: Canvas?): Canvas? {
var newCanvas = canvas
newCanvas = meteor.onDraw(newCanvas)
newCanvas = trail.onDraw(newCanvas)

return newCanvas
}

internal class Meteor(starConstraints: Star.StarConstraints, var x: Int, var y: Int, var color: Int, viewWidth: Int, viewHeight: Int, private val colorListener: () -> Int, private val onDoneListener: () -> Unit) {

private var onDoneInvoked = false
internal val star: Star = Star(starConstraints, x, y, false, 1.0, color, viewWidth, viewHeight, colorListener)


fun calculateFrame(viewWidth: Int, viewHeight: Int) {
// star.calculateFrame(viewWidth, viewHeight)
star.x = star.x - 8
star.y = star.y + 8

if (star.x < viewWidth * -1 && !onDoneInvoked) {
onDoneListener.invoke()
onDoneInvoked = true
}
}

fun onDraw(canvas: Canvas?): Canvas? {
canvas?.drawCircle(star.x.toFloat(), star.y.toFloat(), star.length.toFloat() / 2f, star.fillPaint)

return canvas
}

}


internal class Trail(val length: Double, val fillPaint: Paint) {

private val trailPixels: MutableList<TrailPixel> = mutableListOf()

fun addPixel(x: Int, y: Int) {
trailPixels.add(TrailPixel(x, y, length))
}

fun calculatePixels() {
trailPixels.forEach { it.calculatePixel() }
}

fun onDraw(canvas: Canvas?): Canvas? {

val newList = trailPixels.toList()
newList.forEach {
val newPaint = fillPaint
newPaint.alpha = (it.opacity * 255.0).toInt()
canvas?.drawCircle(it.x.toFloat(), it.y.toFloat(), it.length.toFloat() / 2f, fillPaint)
}
return canvas
}


internal class TrailPixel(val x: Int, val y: Int, val length: Double) {
fun calculatePixel() {
opacity -= 0.015

if (opacity < 0) {
opacity = 0.0
}
}

var opacity: Double = 0.90


}
}
}
Loading

0 comments on commit b2b66e6

Please sign in to comment.