Skip to content

Commit

Permalink
Merge pull request #4 from lhoyong/dev
Browse files Browse the repository at this point in the history
Fix: touch event motion
  • Loading branch information
lhoyong authored Apr 5, 2018
2 parents cd9846a + cf60227 commit 1f39d95
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
2 changes: 1 addition & 1 deletion lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apply plugin: 'com.android.library'
apply plugin: 'com.github.dcendents.android-maven'

group = 'com.github.lhoyong'
version = '0.0.1'
version = '0.0.2'

android {
compileSdkVersion rootProject.ext.compileSdkVersion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.drawable.Drawable
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View

Expand Down Expand Up @@ -35,6 +36,12 @@ class TransParencySeekBar @JvmOverloads constructor(
// Use thumb
var mEnabled: Boolean = a.getBoolean(R.styleable.TransParencySeekBar_enable, true)


/* test value */
var touchX: Float = 0F

/* ---- test value --- */

init {
a?.recycle()
}
Expand All @@ -43,17 +50,19 @@ class TransParencySeekBar @JvmOverloads constructor(
override fun onTouchEvent(event: MotionEvent): Boolean {
if (mEnabled) {
when (event.action) {
MotionEvent.ACTION_DOWN -> checkTouchPosition(event.x)
MotionEvent.ACTION_MOVE -> if (isPressed) changeProgress(event.x)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> isPressed = false
MotionEvent.ACTION_DOWN -> isPressed = true
MotionEvent.ACTION_MOVE -> if (isPressed) changeProgress(event.x)
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
isPressed = false
touchX = 0F
}
}
}
return mEnabled
}

@SuppressLint("DrawAllocation")
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

val height = View.getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
val width = View.getDefaultSize(suggestedMinimumWidth, widthMeasureSpec)

Expand Down Expand Up @@ -109,16 +118,23 @@ class TransParencySeekBar @JvmOverloads constructor(
return (width.toFloat() / 100) * percent
}

// check for Touch Position
private fun checkTouchPosition(x: Float) {
if (thumbX + 20 >= x && thumbX - 20 <= x) isPressed = true
else isPressed = false
}

// change Progress
private fun changeProgress(x: Float) {
progress = ((maxProgress / 100) * ((thumbX / measuredWidth) * 100)).toInt()
thumbX = x
if (touchX == 0F) touchX = x
else if (thumbX < 0) {
thumbX = 0F
progress = 0
} else if (thumbX > measuredWidth) {
thumbX = measuredWidth.toFloat()
progress = maxProgress
} else {
progress = ((maxProgress.toFloat() / 100) * ((thumbX / measuredWidth) * 100)).toInt()
thumbX += x - touchX

if (x >= touchX) touchX += x - touchX
else touchX -= touchX - x
}

invalidate()
}
}

0 comments on commit 1f39d95

Please sign in to comment.