Skip to content

Commit

Permalink
#26: added providing all necessary things inside SlashCollageView
Browse files Browse the repository at this point in the history
  • Loading branch information
programmerr47 committed Jan 11, 2020
1 parent 1ba2d2c commit 692eb25
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,75 @@ import android.graphics.Paint
import android.graphics.PointF
import android.graphics.Rect
import android.graphics.RectF
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.view.View
import com.programmerr47.phroom.Phroom
import kotlin.math.nextDown
import kotlin.properties.Delegates.observable
import kotlin.random.Random

class SlashCollageView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val rnd = Random(System.currentTimeMillis())
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private var collage: List<RectF> = emptyList()
private var colors: List<Int> = emptyList()

private var originCollage: List<RectF> = emptyList()
private var finalCollage: List<RectF> = emptyList()

private var urls: List<String> = emptyList()
private var collageDrawables: Array<Drawable?> = emptyArray()

lateinit var phroom: Phroom

var framePadding: Int by observable(0) { _, old, new ->
if (old != new) {
requestLayout()
invalidate()
}
}

var frameColor: Int by observable(0) { _, old, new ->
if (old != new) {
paint.color = new
invalidate()
}
}

override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
val widthNotUnspecified = MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.UNSPECIFIED
val heightNotUnspecified = MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.UNSPECIFIED

if (widthNotUnspecified && heightNotUnspecified && collage.isEmpty()) {
collage = generateCollage(
//TODO move that
if (widthNotUnspecified && heightNotUnspecified && originCollage.isEmpty()) {
originCollage = generateCollage(
MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec)
)
}

colors = collage.map { rnd.nextInt() }
finalCollage = originCollage.map {
RectF(it).apply {
left += framePadding
top += framePadding
right -= framePadding
bottom -= framePadding
}
}

setMeasuredDimension(widthMeasureSpec, heightMeasureSpec)
}

override fun onDraw(canvas: Canvas) {
collage.forEachIndexed { i, rect ->
paint.color = colors[i]
canvas.drawRect(rect, paint)
finalCollage.forEachIndexed { i, rect ->
collageDrawables[i]?.let { draw(canvas) } ?: canvas.drawRect(rect, paint)
}
}

fun generateAgain(urls: List<String>) {
this.urls = urls
collage = emptyList()
colors = emptyList()
originCollage = emptyList()
collageDrawables = arrayOfNulls(urls.size)
requestLayout()
invalidate()
}
Expand All @@ -64,6 +92,8 @@ class SlashCollageView @JvmOverloads constructor(
fun generate(n: Int, initial: Rect) = generate(n, RectF(initial))

fun generate(n: Int, initial: RectF): List<RectF> {
if (n == 0) return emptyList()

queue.clear()
weights.clear()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package com.programmerr47.phroom.targets
import android.graphics.Bitmap
import android.graphics.drawable.Drawable

//Made it internal for now, but we can easily expose it
internal interface Target {
interface Target {
val size: Size

fun onNew(initial: Bitmap?)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.programmerr47.phroom.sample

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import kotlinx.android.synthetic.main.activity_collage.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
Expand All @@ -17,6 +18,10 @@ class CollageActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_collage)

scvCollage.phroom = locator.phroom
scvCollage.framePadding = resources.getDimensionPixelSize(R.dimen.padding_collage_frame)
scvCollage.frameColor = ContextCompat.getColor(this, R.color.bgFrame)

generate()
btnGenerate.setOnClickListener { generate() }
}
Expand Down
2 changes: 2 additions & 0 deletions sample/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
<color name="colorAccent">#D81B60</color>

<color name="textPrimary">#DE000000</color>

<color name="bgFrame">#55000000</color>
</resources>
4 changes: 4 additions & 0 deletions sample/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="padding_collage_frame">2dp</dimen>
</resources>

0 comments on commit 692eb25

Please sign in to comment.