Commit 3695a419 authored by Vladislav Bogdashkin's avatar Vladislav Bogdashkin 🎣

common drawing bubblie pin

parent b22a6d40
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="GradleMigrationSettings" migrationVersion="1" />
<component name="GradleSettings">
<option name="linkedExternalProjectsSettings">
<GradleProjectSettings>
......
package com.biganto.visual.roompark.presentation.screen.albums.util
import android.content.Context
import android.graphics.Rect
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.view.TextureView
import androidx.recyclerview.widget.RecyclerView
import timber.log.Timber
/**
* Created by Vladislav Bogdashkin on 22.12.2019.
*/
class BubbleSlider @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : TextureView(context, attrs, defStyleAttr)
,TextureView.SurfaceTextureListener{
private var texThread: RendererThread?=null
private var recyclerView:RecyclerView? = null
private var actualPosition = -1
private val actualRect = Rect()
fun setUpView(view:RecyclerView) {
recyclerView = view
recyclerView?.setOnScrollChangeListener { v, scrollX, scrollY, oldScrollX, oldScrollY ->
val child = recyclerView?.getChildAt(actualPosition)
child?.getDrawingRect(actualRect)
actualRect.let {
texThread?.setCenter(it.centerX())
}
}
}
fun onCurrentItemChanged(newPosition:Int){
actualPosition = newPosition
texThread?.setIndex(actualPosition)
}
fun processSliding(_position:Float){
texThread?.setSlidePosition(_position)
}
init {
surfaceTextureListener=this
}
override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
Timber.d("On Size Chagned")
super.onSizeChanged(w, h, oldw, oldh)
}
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture?, width: Int, height: Int) {
print(" onSurfaceTextureSizeChanged")
texThread?.setSize(width,height)
//Ignored
}
override fun onSurfaceTextureUpdated(surface: SurfaceTexture?) {
print(" onSurfaceTextureUpdated")
//Ignored
}
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture?): Boolean {
texThread?.isStopped=true
return true
}
override fun onSurfaceTextureAvailable(surface: SurfaceTexture?, width: Int, height: Int) {
Timber.d("avaliable surf: $surface")
surface?.let {
if (texThread?.isAlive==true)
return
texThread = RendererThread(context,it)
texThread?.setSize(width,height)
texThread?.start()
}
}
override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
texThread?.isStopped=true
}
}
package com.biganto.visual.roompark.presentation.screen.albums.util
import android.content.Context
import android.graphics.*
import android.view.Surface
import com.biganto.visual.roompark.R
import timber.log.Timber
import kotlin.math.tan
/**
* Created by Vladislav Bogdashkin on 18.07.2019.
*/
private const val CONST_SLEEP_THRESHOLD = 8L
private const val CONST_HEIGHT_MARGIN_BORDER_PX = 8
private const val CONST_DECAY_TIMER = 500L
class RendererThread(val context: Context, val surface: SurfaceTexture)
: Thread(){
private val dens:Float by lazy { context.resources.displayMetrics.density }
private val SLEEP_THRESHOLD get() = CONST_SLEEP_THRESHOLD
private val HEIGHT_MARGIN_BORDER_PX get() =
(CONST_HEIGHT_MARGIN_BORDER_PX *dens).int
private val DECAY_TIMER get() = CONST_DECAY_TIMER
private var isDirty = true
var isStopped = false
private var width = 0
private var height = 0
private var centerX = 0
private var holdIndex = 0
get() = if (isDragging) field
else currentIndex
private var currentIndex:Int = 0
set(value) {Timber.w("currentIndex to: $value");field = value}
fun setIndex(index:Int){
if (!isDragging) holdIndex = index
currentIndex = index
isDirty=true
}
private val Float.int:Int get() {return this.toInt()}
fun setCenter(centerX:Int){
this.centerX = centerX
isDirty = true
}
private var scrollScaleFactor = 1f
private var touchOffset = 0f
private var scrollOffset = 0f
private var isDragging = false
private var slidePosition = 0f
private var dragKoef = 1f
fun setSlidePosition(newPosition:Float){
slidePosition = newPosition
isDirty = true
}
private var decayFraction = 0f
private fun timerDecay(mills:Long){
isDirty=true
decayFraction+=mills
animateBorders()
scrollOffset = scrollOffset.smoothLerp(touchOffset-scrollOffset,.2f)
if (touchOffset> 0 && scrollOffset >= touchOffset) {
scrollOffset = touchOffset
isDirty=false
}
else if (touchOffset< 0 && scrollOffset <= touchOffset) {
scrollOffset = touchOffset
isDirty=false
}
if (decayFraction> DECAY_TIMER) isDirty=false
val dirty = animateBorders()
if (!isDirty) isDirty = dirty
}
private fun animateBorders():Boolean{
if (isDragging && dragKoef>0){
dragKoef = dragKoef.smoothLerp(-dragKoef,.12f)
if (dragKoef<.1f) dragKoef = 0f
return dragKoef>0
}
if (!isDragging && dragKoef<1f) {
dragKoef = dragKoef.smoothLerp(1f-dragKoef,.03f)
if (dragKoef>.99f) dragKoef = 1f
return dragKoef<1f
}
return false
}
private fun Float.smoothLerp( delta: Float, fraction:Float =.4f): Float {
return this + fraction * (delta)
}
fun setSize(w: Int, h: Int){
width=w
height=h
}
private val pinColor:Int by lazy {
return@lazy context.resources.getColor(R.color.colorOpacityBackground)
}
private val trianglePath:Path
get() {
val p = Path()
p.fillType = Path.FillType.EVEN_ODD
p.moveTo(centerX.toFloat(),height.toFloat())
p.lineTo(centerX.toFloat() + height*tan(Math.toRadians(60.0)).toFloat(),height.toFloat())
p.lineTo(centerX.toFloat() - height*tan(Math.toRadians(60.0)).toFloat(),height.toFloat())
p.lineTo(centerX.toFloat(),height.toFloat())
p.close()
return p
}
private val trianglePaint:Paint by lazy {
val p = Paint()
p.color = pinColor
p.style = Paint.Style.FILL_AND_STROKE
p.isAntiAlias = true
p
}
override fun run() {
super.run()
Timber.w("isStopped: $isStopped")
Timber.w("isDirty: $isDirty")
isDirty = true
while (!isStopped) {
while (!isDirty)
sleep(SLEEP_THRESHOLD) // in real life this sleep is more complicated
isDirty = false
val surf = Surface(surface)
val dRect = Rect(
0
, 0
, width
, height
)
val c = surf.lockCanvas(dRect)
c.drawColor(Color.TRANSPARENT)
c.drawPath(trianglePath,trianglePaint)
timerDecay(SLEEP_THRESHOLD)
surf.unlockCanvasAndPost(c)
surf.release()
// isDirty = false
}
Timber.w("STOPPED; $isStopped")
Timber.w("STOPPED; $isDirty")
isDirty=false
}
}
......@@ -14,7 +14,7 @@ import com.biganto.visual.roompark.base.RoomParkMainActivity
import com.biganto.visual.roompark.conductor.BigantoBaseController
import com.biganto.visual.roompark.conductor.dialogs.ChooseResolutionDialogController
import com.biganto.visual.roompark.conductor.dialogs.change_handler.DialogChangeHandler
import com.biganto.visual.roompark.presentation.screen.photo.util.PhotoPreviewSlider
import com.biganto.visual.roompark.presentation.screen.albums.util.PhotoPreviewSlider
import com.biganto.visual.roompark.presentation.screen.photo.util.PhotosAdapter
import com.bluelinelabs.conductor.RouterTransaction
import com.google.android.material.textview.MaterialTextView
......
......@@ -75,7 +75,7 @@
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/iic_full_view"/>
<com.biganto.visual.roompark.presentation.screen.photo.util.PhotoPreviewSlider
<com.biganto.visual.roompark.presentation.screen.albums.util.PhotoPreviewSlider
android:id="@+id/photosPreviewSlider"
android:layout_width="0dp"
android:layout_height="48dp"
......
......@@ -2,7 +2,7 @@ ext {
$APPLICATION_ID = "com.biganto.visual.roompark"
targetSdkVersion_RoomPark = 28
minSdkVersion_RoomPark = 21
minSdkVersion_RoomPark = 23
compileSdkVersion_RoomPark = 28
VERSION_CODE = 1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment