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..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() { @@ -69,15 +68,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..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()) } @@ -67,36 +68,42 @@ 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 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() } - fun stop() { + protected fun stopScroll() { 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() + stopScroll() } } @@ -150,11 +157,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)//绘制 @@ -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 d78eb9698..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 @@ -1,22 +1,64 @@ 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) + } + } + startScroll(touchX.toInt(), 0, distanceX.toInt(), 0) } 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