update SlidePageDelegate

pull/32/head
Administrator 5 years ago
parent fc318d2579
commit ed5492536d
  1. 16
      app/src/main/java/io/legado/app/help/ImageLoader.kt
  2. 5
      app/src/main/java/io/legado/app/ui/main/MainActivity.kt
  3. 13
      app/src/main/java/io/legado/app/ui/widget/page/ContentView.kt
  4. 2
      app/src/main/java/io/legado/app/ui/widget/page/PageFactory.kt
  5. 4
      app/src/main/java/io/legado/app/ui/widget/page/PageView.kt
  6. 7
      app/src/main/java/io/legado/app/ui/widget/page/TextPageFactory.kt
  7. 16
      app/src/main/java/io/legado/app/ui/widget/page/delegate/CoverPageDelegate.kt
  8. 30
      app/src/main/java/io/legado/app/ui/widget/page/delegate/PageDelegate.kt
  9. 43
      app/src/main/java/io/legado/app/ui/widget/page/delegate/SlidePageDelegate.kt

@ -10,7 +10,9 @@ import com.bumptech.glide.Glide
import com.bumptech.glide.RequestBuilder import com.bumptech.glide.RequestBuilder
import com.bumptech.glide.RequestManager import com.bumptech.glide.RequestManager
import com.bumptech.glide.load.engine.DiskCacheStrategy 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.drawable.DrawableTransitionOptions
import com.bumptech.glide.load.resource.gif.GifDrawable import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.RequestOptions import com.bumptech.glide.request.RequestOptions
@ -74,27 +76,27 @@ object ImageLoader {
} }
fun toCropRound(corner: Int): ImageLoadBuilder<S> { fun toCropRound(corner: Int): ImageLoadBuilder<S> {
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(corner)) requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner))
return this return this
} }
fun toCircle(): ImageLoadBuilder<S> { fun toCircle(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CircleCrop()) requestOptions = requestOptions.circleCrop()
return this return this
} }
fun centerInside(): ImageLoadBuilder<S> { fun centerInside(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CenterInside()) requestOptions = requestOptions.centerInside()
return this return this
} }
fun fitCenter(): ImageLoadBuilder<S> { fun fitCenter(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(FitCenter()) requestOptions = requestOptions.fitCenter()
return this return this
} }
fun centerCrop(): ImageLoadBuilder<S> { fun centerCrop(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CenterCrop()) requestOptions = requestOptions.centerCrop()
return this return this
} }
@ -213,7 +215,7 @@ object ImageLoader {
} }
private fun <ResourceType> ensureOptions(builder: RequestBuilder<ResourceType>): RequestBuilder<ResourceType> { private fun <ResourceType> ensureOptions(builder: RequestBuilder<ResourceType>): RequestBuilder<ResourceType> {
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<R> constructor(private val target: ImageViewTarget<R>) : private inner class Target<R> constructor(private val target: ImageViewTarget<R>) :

@ -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.findbook.FindBookFragment
import io.legado.app.ui.main.my.MyFragment import io.legado.app.ui.main.my.MyFragment
import io.legado.app.ui.main.rss.RssFragment import io.legado.app.ui.main.rss.RssFragment
import io.legado.app.utils.getPrefInt import io.legado.app.utils.*
import io.legado.app.utils.getViewModel
import io.legado.app.utils.observeEvent
import io.legado.app.utils.putPrefInt
import kotlinx.android.synthetic.main.activity_main.* import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.launch import kotlinx.coroutines.launch

@ -1,7 +1,6 @@
package io.legado.app.ui.widget.page package io.legado.app.ui.widget.page
import android.content.Context import android.content.Context
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.util.AttributeSet import android.util.AttributeSet
import android.widget.FrameLayout import android.widget.FrameLayout
@ -41,14 +40,10 @@ class ContentView : FrameLayout {
} }
fun setBg(bg: Drawable?) { fun setBg(bg: Drawable?) {
if (bg is ColorDrawable) { //all supported
bgImage.setImageDrawable(null) ImageLoader.load(context, bg)
bgImage.background = bg .centerCrop()
} else { .setAsDrawable(bgImage)
ImageLoader.load(context, bg)
.centerCrop()
.setAsDrawable(bgImage)
}
} }
fun upTime() { fun upTime() {

@ -6,6 +6,8 @@ abstract class PageFactory<DATA>(protected val dataSource: DataSource) {
abstract fun moveToFirst() abstract fun moveToFirst()
abstract fun moveToLast()
abstract fun moveToNext():Boolean abstract fun moveToNext():Boolean
abstract fun moveToPrevious(): Boolean abstract fun moveToPrevious(): Boolean

@ -7,8 +7,8 @@ import android.util.AttributeSet
import android.view.MotionEvent import android.view.MotionEvent
import android.widget.FrameLayout import android.widget.FrameLayout
import io.legado.app.help.ReadBookConfig 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.PageDelegate
import io.legado.app.ui.widget.page.delegate.SlidePageDelegate
class PageView(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs), PageDelegate.PageInterface { 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() upBg()
setWillNotDraw(false) setWillNotDraw(false)
pageDelegate = CoverPageDelegate(this) pageDelegate = SlidePageDelegate(this)
setPageFactory(TextPageFactory.create(object : DataSource { setPageFactory(TextPageFactory.create(object : DataSource {
override fun isPrepared(): Boolean { override fun isPrepared(): Boolean {

@ -26,6 +26,10 @@ class TextPageFactory private constructor(dataSource: DataSource) : PageFactory<
index = 0 index = 0
} }
override fun moveToLast() {
}
override fun moveToNext(): Boolean { override fun moveToNext(): Boolean {
return if(hasNext()){ return if(hasNext()){
index = index.plus(1) index = index.plus(1)
@ -43,7 +47,8 @@ class TextPageFactory private constructor(dataSource: DataSource) : PageFactory<
} }
override fun currentPage(): TextPage? { override fun currentPage(): TextPage? {
return dataSource.getCurrentChapter()?.page(index) // return dataSource.getCurrentChapter()?.page(index)
return TextPage(index, "index:$index")
} }
override fun nextPage(): TextPage? { override fun nextPage(): TextPage? {

@ -69,15 +69,13 @@ class CoverPageDelegate(pageView: PageView) : PageDelegate(pageView) {
} }
} }
private fun addShadow(left: Int, canvas: Canvas?) { private fun addShadow(left: Int, canvas: Canvas) {
canvas?.let { if (left < 0) {
if (left < 0) { shadowDrawableR.setBounds(left + viewWidth, 0, left + viewWidth + 30, viewHeight)
shadowDrawableR.setBounds(left + viewWidth, 0, left + viewWidth + 30, viewHeight) shadowDrawableR.draw(canvas)
shadowDrawableR.draw(it) } else {
} else { shadowDrawableL.setBounds(left - 30, 0, left, viewHeight)
shadowDrawableL.setBounds(left - 30, 0, left, viewHeight) shadowDrawableL.draw(canvas)
shadowDrawableL.draw(it)
}
} }
} }
} }

@ -67,36 +67,41 @@ abstract class PageDelegate(protected val pageView: PageView) {
onScroll() onScroll()
} }
fun setViewSize(width: Int, height: Int) { protected fun invalidate() {
viewWidth = width
viewHeight = height
invalidate()
}
fun invalidate() {
pageView.invalidate() pageView.invalidate()
} }
fun start() { protected fun start() {
isRunning = true isRunning = true
isStarted = true isStarted = true
invalidate() invalidate()
} }
fun stop() { protected fun stop() {
isRunning = false isRunning = false
isStarted = false isStarted = false
bitmap = null bitmap = null
invalidate() 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() { fun scroll() {
if (scroller.computeScrollOffset()) { if (scroller.computeScrollOffset()) {
setTouchPoint(scroller.currX.toFloat(), scroller.currY.toFloat()) setTouchPoint(scroller.currX.toFloat(), scroller.currY.toFloat())
} else if (isStarted) { } else if (isStarted) {
setTouchPoint(scroller.finalX.toFloat(), scroller.finalY.toFloat(), false) setTouchPoint(scroller.finalX.toFloat(), scroller.finalY.toFloat(), false)
stop()
onScrollStop() onScrollStop()
stop()
} }
} }
@ -150,11 +155,6 @@ abstract class PageDelegate(protected val pageView: PageView) {
return detector.onTouchEvent(event) 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 onScrollStart()//scroller start
abstract fun onDraw(canvas: Canvas)//绘制 abstract fun onDraw(canvas: Canvas)//绘制

@ -1,22 +1,65 @@
package io.legado.app.ui.widget.page.delegate package io.legado.app.ui.widget.page.delegate
import android.graphics.Canvas import android.graphics.Canvas
import android.graphics.Matrix
import io.legado.app.ui.widget.page.PageView import io.legado.app.ui.widget.page.PageView
class SlidePageDelegate(pageView: PageView) : PageDelegate(pageView) { class SlidePageDelegate(pageView: PageView) : PageDelegate(pageView) {
private val bitmapMatrix = Matrix()
override fun onScrollStart() { 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) { 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() { 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() { override fun onScrollStop() {
curPage?.x = 0.toFloat()
if (!isCancel) {
pageView.fillPage(direction)
}
} }
} }
Loading…
Cancel
Save