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