Skip to content

Instantly share code, notes, and snippets.

@o4oren
o4oren / properties.json
Last active Nov 12, 2018
properties.json
View properties.json
{
"serverBaseUri": "http://localhost:3000",
"mongoConnectUrl": "mongodb://localhost:27017/",
"dbName": "ahem",
"appListenPort": 3000,
"smtpPort" : 2525,
"emailDeleteInterval" : 3600,
"emailDeleteAge" : 86400,
"allowAutocomplete" : true,
"allowedDomains" : ["my.domain.com"],
View calculate.kt
private fun calculateAngle(x: Float, y: Float): Float {
val px = (x / width.toFloat()) - 0.5
val py = ( 1 - y / height.toFloat()) - 0.5
var angle = -(Math.toDegrees(atan2(py, px)))
.toFloat() + 90
if (angle > 180) angle -= 360
return angle
}
View rotary_knob_view.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/knobImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
@o4oren
o4oren / attrs.xml
Created May 2, 2020
Rotary knob attibutes
View attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="RotaryKnobView">
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
<attr name="initialValue" format="integer" />
<attr name="knobDrawable" format="reference" />
</declare-styleable>
</resources>
@o4oren
o4oren / RotaryKnobView.kt
Created May 2, 2020
RotaryKnobView signature
View RotaryKnobView.kt
class RotaryKnobView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr), GestureDetector.OnGestureListener {
}
View RotaryKnobView.kt
override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float)
: Boolean {
return true
}
override fun onTouchEvent(event: MotionEvent): Boolean {
return if (gestureDetector.onTouchEvent(event))
true
else
super.onTouchEvent(event)
View RotaryKnobView.kt
override fun onScroll(e1: MotionEvent, e2: MotionEvent, distanceX: Float, distanceY: Float)
: Boolean {
val rotationDegrees = calculateAngle(e2.x, e2.y)
// use only -150 to 150 range (knob min/max points
if (rotationDegrees >= -150 && rotationDegrees <= 150) {
setKnobPosition(rotationDegrees)
// Calculate rotary value
// The range is the 300 degrees between -150 and 150, so we'll add 150 to adjust the
@o4oren
o4oren / RotaryKnobView.kt
Last active May 2, 2020
Rotary knob constructor
View RotaryKnobView.kt
class RotaryKnobView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : RelativeLayout(context, attrs, defStyleAttr), GestureDetector.OnGestureListener {
private val gestureDetector: GestureDetectorCompat
private var maxValue = 99
private var minValue = 0
var listener: RotaryKnobListener? = null
var value = 50
private var knobDrawable: Drawable? = null
private var divider = 300f / (maxValue - minValue)
@o4oren
o4oren / RotaryKnobView.kt
Created May 2, 2020
RotartyKnob initialization
View RotaryKnobView.kt
init {
this.maxValue = maxValue + 1
LayoutInflater.from(context)
.inflate(R.layout.rotary_knob_view, this, true)
context.theme.obtainStyledAttributes(
attrs,
R.styleable.RotaryKnobView,
0,
View RotaryKnobView.kt
interface RotaryKnobListener {
fun onRotate(value: Int)
}
You can’t perform that action at this time.