From 692eb25ecaa5fca6bb2b9666ba9abe3dea4a18f2 Mon Sep 17 00:00:00 2001 From: Mikhail Spitsin Date: Sat, 11 Jan 2020 19:01:25 +0400 Subject: [PATCH] #26: added providing all necessary things inside SlashCollageView --- .../phroom/collage/SlashCollageView.kt | 52 +++++++++++++++---- .../programmerr47/phroom/targets/Target.kt | 3 +- .../phroom/sample/CollageActivity.kt | 5 ++ sample/src/main/res/values/colors.xml | 2 + sample/src/main/res/values/dimens.xml | 4 ++ 5 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 sample/src/main/res/values/dimens.xml diff --git a/collage/src/main/java/com/programmerr47/phroom/collage/SlashCollageView.kt b/collage/src/main/java/com/programmerr47/phroom/collage/SlashCollageView.kt index 023300c..051b265 100644 --- a/collage/src/main/java/com/programmerr47/phroom/collage/SlashCollageView.kt +++ b/collage/src/main/java/com/programmerr47/phroom/collage/SlashCollageView.kt @@ -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 = emptyList() - private var colors: List = emptyList() + + private var originCollage: List = emptyList() + private var finalCollage: List = emptyList() + private var urls: List = emptyList() + private var collageDrawables: Array = 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) { this.urls = urls - collage = emptyList() - colors = emptyList() + originCollage = emptyList() + collageDrawables = arrayOfNulls(urls.size) requestLayout() invalidate() } @@ -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 { + if (n == 0) return emptyList() + queue.clear() weights.clear() diff --git a/library/src/main/java/com/programmerr47/phroom/targets/Target.kt b/library/src/main/java/com/programmerr47/phroom/targets/Target.kt index eb36593..719c313 100644 --- a/library/src/main/java/com/programmerr47/phroom/targets/Target.kt +++ b/library/src/main/java/com/programmerr47/phroom/targets/Target.kt @@ -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?) diff --git a/sample/src/main/java/com/programmerr47/phroom/sample/CollageActivity.kt b/sample/src/main/java/com/programmerr47/phroom/sample/CollageActivity.kt index 05c5a41..881a3fd 100644 --- a/sample/src/main/java/com/programmerr47/phroom/sample/CollageActivity.kt +++ b/sample/src/main/java/com/programmerr47/phroom/sample/CollageActivity.kt @@ -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 @@ -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() } } diff --git a/sample/src/main/res/values/colors.xml b/sample/src/main/res/values/colors.xml index 6803d9e..4bb6093 100644 --- a/sample/src/main/res/values/colors.xml +++ b/sample/src/main/res/values/colors.xml @@ -5,4 +5,6 @@ #D81B60 #DE000000 + + #55000000 diff --git a/sample/src/main/res/values/dimens.xml b/sample/src/main/res/values/dimens.xml new file mode 100644 index 0000000..9208d31 --- /dev/null +++ b/sample/src/main/res/values/dimens.xml @@ -0,0 +1,4 @@ + + + 2dp +