Skip to content

Commit

Permalink
version 1.0.1
Browse files Browse the repository at this point in the history
code 2
改进了ReaderView的翻页逻辑,现在滑动屏幕也可以正确翻页了
  • Loading branch information
YuanWenHai committed Jan 18, 2021
1 parent 14f30fd commit 7e4449e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
applicationId "com.will.reader"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
versionCode 2
versionName "1.0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
19 changes: 18 additions & 1 deletion app/src/main/java/com/will/reader/reader/ReaderView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.view.ViewConfiguration
import com.will.reader.util.LOG_TAG
import kotlin.math.abs

/**
* created by will on 2020/11/29 11:52
*/
class ReaderView(context: Context,attributeSet: AttributeSet): View(context,attributeSet) {

private var clickFlag = false
private var downX = 0f
private val mTouchSlop = ViewConfiguration.get(context).scaledTouchSlop * 5
private var onClick: ((which: Int) -> Unit)? = null
private var printConfig: PrintConfig? = null
private var mContent = Content(emptyList(),"","","")
Expand All @@ -25,7 +29,10 @@ class ReaderView(context: Context,attributeSet: AttributeSet): View(context,attr
@SuppressLint("ClickableViewAccessibility")
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> clickFlag = true
MotionEvent.ACTION_DOWN -> {
clickFlag = true
downX = event.x
}
MotionEvent.ACTION_CANCEL -> clickFlag = false
MotionEvent.ACTION_OUTSIDE -> clickFlag = false
MotionEvent.ACTION_UP -> {
Expand Down Expand Up @@ -94,6 +101,16 @@ class ReaderView(context: Context,attributeSet: AttributeSet): View(context,attr

private fun handleActonUp(event: MotionEvent) {
val viewWidth = width
val offset = downX - event.x
if(abs(offset) > mTouchSlop){
if (offset > 0){
onClick?.let { it(RIGHT_CLICK) }
}else{
onClick?.let { it(LEFT_CLICK) }
}
return
}

when (event.x) {
in 0f..(viewWidth / 3f) -> onClick?.let { it(LEFT_CLICK) }
in ((width / 3f) * 2)..width.toFloat() -> onClick?.let { it(RIGHT_CLICK) }
Expand Down

0 comments on commit 7e4449e

Please sign in to comment.