Skip to content

Commit

Permalink
Make view match width and height values if any of the two is set as W…
Browse files Browse the repository at this point in the history
…RAP_CONTENT
  • Loading branch information
slaviboy committed Apr 11, 2020
1 parent ee3e148 commit e96990a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 14 deletions.
45 changes: 40 additions & 5 deletions colorpicker/src/main/java/com/slaviboy/colorpicker/window/Base.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import android.util.TypedValue
import android.view.MotionEvent
import android.view.View
import android.view.ViewTreeObserver
import android.view.ViewTreeObserver.OnPreDrawListener
import androidx.constraintlayout.widget.ConstraintLayout
import com.slaviboy.colorpicker.ColorHolder
import com.slaviboy.colorpicker.CornerRadius
import com.slaviboy.colorpicker.R
Expand Down Expand Up @@ -124,12 +124,48 @@ abstract class Base : View {

// called before drawing to init some values that need view size
this.afterMeasured {
onInitBase()
update()
onRedraw()
checkForWrapContent()
}
}

/**
* Check if any of the view has a wrap content set as size,
* this will make the width match height and vice versa.
*/
fun checkForWrapContent() {

// match width and height to have same value if any of is WRAP_CONTENT
var newWidth = width
var newHeight = height
if (layoutParams.height != layoutParams.width) {
if (layoutParams.height == ConstraintLayout.LayoutParams.WRAP_CONTENT) {
newHeight = width
} else if (layoutParams.width == ConstraintLayout.LayoutParams.WRAP_CONTENT) {
newWidth = height
}
}

// if change is made
if (newHeight != height || newWidth != width) {
layoutParams.width = newWidth
layoutParams.height = newHeight
layoutParams = layoutParams

// call since the layout params are changed
this.afterMeasured {
afterMeasuredInit()
}
} else {
afterMeasuredInit()
}
}

fun afterMeasuredInit() {
onInitBase()
update()
onRedraw()
}

/**
* Get unit value in pixels, by passing unit string value, supported unit types are:
* dp, sp, px, vw(view width) and vh(view height)
Expand Down Expand Up @@ -182,7 +218,6 @@ abstract class Base : View {
* is done. XML unit values are set after view width and height is needed to get unit values as pixels.
*/
private fun onInitBase() {

// get view width and height
halfWidth = width / 2f
halfHeight = height / 2f
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,6 @@ open class Circular : Base {
selectorPaint.style = Paint.Style.FILL
canvas.drawCircle(selectorX, selectorY, selectorRadius, selectorPaint)

// restore stroke color before calling super
selectorPaint.color = selectorColor
selectorPaint.style = Paint.Style.STROKE
super.drawSelector(canvas)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,6 @@ open class Rectangular : Base {
selectorPaint.style = Paint.Style.FILL
canvas.drawCircle(selectorX, selectorY, selectorRadius, selectorPaint)

// restore stroke color before calling super
selectorPaint.color = selectorColor
selectorPaint.style = Paint.Style.STROKE
super.drawSelector(canvas)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,6 @@ class SliderH : Slider {
selectorPaint.style = Paint.Style.FILL
canvas.drawCircle(selectorX, selectorY, selectorRadius, selectorPaint)

// restore stroke color before calling super
selectorPaint.color = selectorColor
selectorPaint.style = Paint.Style.STROKE
super.drawSelector(canvas)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import com.slaviboy.colorpicker.Range
import com.slaviboy.colorpicker.converter.ColorConverter
import com.slaviboy.colorpicker.window.Slider

// Copyright (C) 2020 Stanislav Georgiev
Expand Down Expand Up @@ -67,6 +68,17 @@ class SliderV : Slider {
invalidate()
}

override fun drawSelector(canvas: Canvas) {

// set fill color
val fillColor = ColorConverter.HSVtoColor(colorConverter.h, 100, colorConverter.v)
selectorPaint.color = fillColor
selectorPaint.style = Paint.Style.FILL
canvas.drawCircle(selectorX, selectorY, selectorRadius, selectorPaint)

super.drawSelector(canvas)
}

/**
* Set 'value' value, that will change the selector position in the slider.
* @param v 'value' value
Expand Down

0 comments on commit e96990a

Please sign in to comment.