From ed5492536d82085619b197eafdbcc89136501277 Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Thu, 22 Aug 2019 15:57:45 +0800 Subject: [PATCH 1/2] update SlidePageDelegate --- .../java/io/legado/app/help/ImageLoader.kt | 16 ++++--- .../io/legado/app/ui/main/MainActivity.kt | 5 +-- .../legado/app/ui/widget/page/ContentView.kt | 13 ++---- .../legado/app/ui/widget/page/PageFactory.kt | 2 + .../io/legado/app/ui/widget/page/PageView.kt | 4 +- .../app/ui/widget/page/TextPageFactory.kt | 7 ++- .../widget/page/delegate/CoverPageDelegate.kt | 16 +++---- .../ui/widget/page/delegate/PageDelegate.kt | 30 ++++++------- .../widget/page/delegate/SlidePageDelegate.kt | 43 +++++++++++++++++++ 9 files changed, 89 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/ImageLoader.kt b/app/src/main/java/io/legado/app/help/ImageLoader.kt index 370be11cc..10a9a8a50 100644 --- a/app/src/main/java/io/legado/app/help/ImageLoader.kt +++ b/app/src/main/java/io/legado/app/help/ImageLoader.kt @@ -10,7 +10,9 @@ import com.bumptech.glide.Glide import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestManager import com.bumptech.glide.load.engine.DiskCacheStrategy -import com.bumptech.glide.load.resource.bitmap.* +import com.bumptech.glide.load.resource.bitmap.BitmapTransitionOptions +import com.bumptech.glide.load.resource.bitmap.CenterCrop +import com.bumptech.glide.load.resource.bitmap.RoundedCorners import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions import com.bumptech.glide.load.resource.gif.GifDrawable import com.bumptech.glide.request.RequestOptions @@ -74,27 +76,27 @@ object ImageLoader { } fun toCropRound(corner: Int): ImageLoadBuilder { - requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(corner)) + requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner)) return this } fun toCircle(): ImageLoadBuilder { - requestOptions = requestOptions.transform(CircleCrop()) + requestOptions = requestOptions.circleCrop() return this } fun centerInside(): ImageLoadBuilder { - requestOptions = requestOptions.transform(CenterInside()) + requestOptions = requestOptions.centerInside() return this } fun fitCenter(): ImageLoadBuilder { - requestOptions = requestOptions.transform(FitCenter()) + requestOptions = requestOptions.fitCenter() return this } fun centerCrop(): ImageLoadBuilder { - requestOptions = requestOptions.transform(CenterCrop()) + requestOptions = requestOptions.centerCrop() return this } @@ -213,7 +215,7 @@ object ImageLoader { } private fun ensureOptions(builder: RequestBuilder): RequestBuilder { - return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.RESOURCE)) + return builder.apply(requestOptions.diskCacheStrategy(if (noCache) DiskCacheStrategy.NONE else DiskCacheStrategy.AUTOMATIC)) } private inner class Target constructor(private val target: ImageViewTarget) : diff --git a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt index af8765812..db04c42f2 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainActivity.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainActivity.kt @@ -18,10 +18,7 @@ import io.legado.app.ui.main.bookshelf.BookshelfFragment import io.legado.app.ui.main.findbook.FindBookFragment import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.rss.RssFragment -import io.legado.app.utils.getPrefInt -import io.legado.app.utils.getViewModel -import io.legado.app.utils.observeEvent -import io.legado.app.utils.putPrefInt +import io.legado.app.utils.* import kotlinx.android.synthetic.main.activity_main.* import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.launch diff --git a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt index 27d794bd9..e2c15ee03 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt @@ -1,7 +1,6 @@ package io.legado.app.ui.widget.page import android.content.Context -import android.graphics.drawable.ColorDrawable import android.graphics.drawable.Drawable import android.util.AttributeSet import android.widget.FrameLayout @@ -41,14 +40,10 @@ class ContentView : FrameLayout { } fun setBg(bg: Drawable?) { - if (bg is ColorDrawable) { - bgImage.setImageDrawable(null) - bgImage.background = bg - } else { - ImageLoader.load(context, bg) - .centerCrop() - .setAsDrawable(bgImage) - } + //all supported + ImageLoader.load(context, bg) + .centerCrop() + .setAsDrawable(bgImage) } fun upTime() { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageFactory.kt index a0e0f32cd..f9b9ca4cb 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageFactory.kt @@ -6,6 +6,8 @@ abstract class PageFactory(protected val dataSource: DataSource) { abstract fun moveToFirst() + abstract fun moveToLast() + abstract fun moveToNext():Boolean abstract fun moveToPrevious(): Boolean diff --git a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt index c98785fcd..1269c6387 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/PageView.kt @@ -7,8 +7,8 @@ import android.util.AttributeSet import android.view.MotionEvent import android.widget.FrameLayout import io.legado.app.help.ReadBookConfig -import io.legado.app.ui.widget.page.delegate.CoverPageDelegate import io.legado.app.ui.widget.page.delegate.PageDelegate +import io.legado.app.ui.widget.page.delegate.SlidePageDelegate class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs), PageDelegate.PageInterface { @@ -30,7 +30,7 @@ class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, att upBg() setWillNotDraw(false) - pageDelegate = CoverPageDelegate(this) + pageDelegate = SlidePageDelegate(this) setPageFactory(TextPageFactory.create(object : DataSource { override fun isPrepared(): Boolean { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt index 9f3497a5e..31fb586b2 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt @@ -26,6 +26,10 @@ class TextPageFactory private constructor(dataSource: DataSource) : PageFactory< index = 0 } + override fun moveToLast() { + + } + override fun moveToNext(): Boolean { return if(hasNext()){ index = index.plus(1) @@ -43,7 +47,8 @@ class TextPageFactory private constructor(dataSource: DataSource) : PageFactory< } override fun currentPage(): TextPage? { - return dataSource.getCurrentChapter()?.page(index) +// return dataSource.getCurrentChapter()?.page(index) + return TextPage(index, "index:$index") } override fun nextPage(): TextPage? { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt index 69130b5f8..a0a5bde9e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt @@ -69,15 +69,13 @@ class CoverPageDelegate(pageView: PageView) : PageDelegate(pageView) { } } - private fun addShadow(left: Int, canvas: Canvas?) { - canvas?.let { - if (left < 0) { - shadowDrawableR.setBounds(left + viewWidth, 0, left + viewWidth + 30, viewHeight) - shadowDrawableR.draw(it) - } else { - shadowDrawableL.setBounds(left - 30, 0, left, viewHeight) - shadowDrawableL.draw(it) - } + private fun addShadow(left: Int, canvas: Canvas) { + if (left < 0) { + shadowDrawableR.setBounds(left + viewWidth, 0, left + viewWidth + 30, viewHeight) + shadowDrawableR.draw(canvas) + } else { + shadowDrawableL.setBounds(left - 30, 0, left, viewHeight) + shadowDrawableL.draw(canvas) } } } diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index 5ce6345b7..ea32785c7 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -67,36 +67,41 @@ abstract class PageDelegate(protected val pageView: PageView) { onScroll() } - fun setViewSize(width: Int, height: Int) { - viewWidth = width - viewHeight = height - invalidate() - } - - fun invalidate() { + protected fun invalidate() { pageView.invalidate() } - fun start() { + protected fun start() { isRunning = true isStarted = true invalidate() } - fun stop() { + protected fun stop() { isRunning = false isStarted = false bitmap = null invalidate() } + protected fun getDuration(distance: Float): Int { + val duration = 300 * abs(distance) / viewWidth + return duration.toInt() + } + + fun setViewSize(width: Int, height: Int) { + viewWidth = width + viewHeight = height + invalidate() + } + fun scroll() { if (scroller.computeScrollOffset()) { setTouchPoint(scroller.currX.toFloat(), scroller.currY.toFloat()) } else if (isStarted) { setTouchPoint(scroller.finalX.toFloat(), scroller.finalY.toFloat(), false) - stop() onScrollStop() + stop() } } @@ -150,11 +155,6 @@ abstract class PageDelegate(protected val pageView: PageView) { return detector.onTouchEvent(event) } - fun getDuration(distance: Float): Int { - val duration = 300 * abs(distance) / viewWidth - return duration.toInt() - } - abstract fun onScrollStart()//scroller start abstract fun onDraw(canvas: Canvas)//绘制 diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt index d78eb9698..434c45de1 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt @@ -1,22 +1,65 @@ package io.legado.app.ui.widget.page.delegate import android.graphics.Canvas +import android.graphics.Matrix import io.legado.app.ui.widget.page.PageView class SlidePageDelegate(pageView: PageView) : PageDelegate(pageView) { + private val bitmapMatrix = Matrix() + override fun onScrollStart() { + val distanceX: Float + when (direction) { + Direction.NEXT -> if (isCancel) { + var dis = viewWidth - startX + touchX + if (dis > viewWidth) { + dis = viewWidth.toFloat() + } + distanceX = viewWidth - dis + } else { + distanceX = -(touchX + (viewWidth - startX)) + } + else -> distanceX = if (isCancel) { + -(touchX - startX) + } else { + viewWidth - (touchX - startX) + } + } + scroller.startScroll(touchX.toInt(), 0, distanceX.toInt(), 0, getDuration(distanceX)) + start() } override fun onDraw(canvas: Canvas) { + val offsetX = touchX - startX + + if ((direction == Direction.NEXT && offsetX > 0) + || (direction == Direction.PREV && offsetX < 0) + ) return + + val distanceX = if (offsetX > 0) offsetX - viewWidth else offsetX + viewWidth + bitmap?.let { + bitmapMatrix.setTranslate(distanceX, 0.toFloat()) + canvas.drawBitmap(it, bitmapMatrix, null) + } } override fun onScroll() { + val offsetX = touchX - startX + + if ((direction == Direction.NEXT && offsetX > 0) + || (direction == Direction.PREV && offsetX < 0) + ) return + curPage?.translationX = offsetX } override fun onScrollStop() { + curPage?.x = 0.toFloat() + if (!isCancel) { + pageView.fillPage(direction) + } } } \ No newline at end of file From 3f13a65d7f1e59f82e5ae58de85a5dcb2d5a892d Mon Sep 17 00:00:00 2001 From: Administrator <1760316362@qq.com> Date: Thu, 22 Aug 2019 16:09:18 +0800 Subject: [PATCH 2/2] update SlidePageDelegate --- .../widget/page/delegate/CoverPageDelegate.kt | 3 +-- .../ui/widget/page/delegate/PageDelegate.kt | 24 +++++++------------ .../page/delegate/SimulationPageDelegate.kt | 15 ++++++++++++ .../widget/page/delegate/SlidePageDelegate.kt | 3 +-- 4 files changed, 25 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt index a0a5bde9e..ccab1295f 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt @@ -43,8 +43,7 @@ class CoverPageDelegate(pageView: PageView) : PageDelegate(pageView) { } } - scroller.startScroll(touchX.toInt(), 0, distanceX.toInt(), 0, getDuration(distanceX)) - start() + startScroll(touchX.toInt(), 0, distanceX.toInt(), 0) } override fun onScrollStop() { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt index ea32785c7..9261a0e06 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt @@ -6,6 +6,7 @@ import android.view.GestureDetector import android.view.MotionEvent import android.widget.Scroller import androidx.interpolator.view.animation.FastOutLinearInInterpolator +import io.legado.app.service.ReadAloudService.Companion.stop import io.legado.app.ui.widget.page.ContentView import io.legado.app.ui.widget.page.PageView import io.legado.app.utils.screenshot @@ -34,7 +35,7 @@ abstract class PageDelegate(protected val pageView: PageView) { protected var viewWidth: Int = pageView.width protected var viewHeight: Int = pageView.height - protected val scroller: Scroller by lazy { Scroller(pageView.context, FastOutLinearInInterpolator()) } + private val scroller: Scroller by lazy { Scroller(pageView.context, FastOutLinearInInterpolator()) } private val detector: GestureDetector by lazy { GestureDetector(pageView.context, GestureListener()) } @@ -71,13 +72,14 @@ abstract class PageDelegate(protected val pageView: PageView) { pageView.invalidate() } - protected fun start() { + protected fun startScroll(startX: Int, startY: Int, dx: Int, dy: Int) { + scroller.startScroll(startX, startY, dx, dy, getDuration((if(dx !=0) dx else dy).toFloat())) isRunning = true isStarted = true invalidate() } - protected fun stop() { + protected fun stopScroll() { isRunning = false isStarted = false bitmap = null @@ -101,7 +103,7 @@ abstract class PageDelegate(protected val pageView: PageView) { } else if (isStarted) { setTouchPoint(scroller.finalX.toFloat(), scroller.finalY.toFloat(), false) onScrollStop() - stop() + stopScroll() } } @@ -162,14 +164,14 @@ abstract class PageDelegate(protected val pageView: PageView) { abstract fun onScrollStop()//scroller finish open fun onScroll() {//移动contentView, slidePage - } enum class Direction { NONE, PREV, NEXT } - private inner class GestureListener : GestureDetector.OnGestureListener { + + private inner class GestureListener : GestureDetector.SimpleOnGestureListener() { override fun onDown(e: MotionEvent): Boolean { // abort() @@ -188,8 +190,6 @@ abstract class PageDelegate(protected val pageView: PageView) { return true } - override fun onShowPress(e: MotionEvent) {} - override fun onSingleTapUp(e: MotionEvent): Boolean { val x = e.x val y = e.y @@ -253,14 +253,6 @@ abstract class PageDelegate(protected val pageView: PageView) { } return isMoved } - - override fun onLongPress(e: MotionEvent) { - - } - - override fun onFling(e1: MotionEvent, e2: MotionEvent, velocityX: Float, velocityY: Float): Boolean { - return false - } } interface PageInterface { diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt new file mode 100644 index 000000000..2020e3a60 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SimulationPageDelegate.kt @@ -0,0 +1,15 @@ +package io.legado.app.ui.widget.page.delegate + +import android.graphics.Canvas +import io.legado.app.ui.widget.page.PageView + +class SimulationPageDelegate(pageView: PageView) : PageDelegate(pageView) { + override fun onScrollStart() { + } + + override fun onDraw(canvas: Canvas) { + } + + override fun onScrollStop() { + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt index 434c45de1..584a4aa2e 100644 --- a/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt +++ b/app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt @@ -27,8 +27,7 @@ class SlidePageDelegate(pageView: PageView) : PageDelegate(pageView) { } } - scroller.startScroll(touchX.toInt(), 0, distanceX.toInt(), 0, getDuration(distanceX)) - start() + startScroll(touchX.toInt(), 0, distanceX.toInt(), 0) } override fun onDraw(canvas: Canvas) {