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. 7
      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. 8
      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.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<S> {
requestOptions = requestOptions.transforms(CenterCrop(), RoundedCorners(corner))
requestOptions = requestOptions.transform(CenterCrop(), RoundedCorners(corner))
return this
}
fun toCircle(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CircleCrop())
requestOptions = requestOptions.circleCrop()
return this
}
fun centerInside(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CenterInside())
requestOptions = requestOptions.centerInside()
return this
}
fun fitCenter(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(FitCenter())
requestOptions = requestOptions.fitCenter()
return this
}
fun centerCrop(): ImageLoadBuilder<S> {
requestOptions = requestOptions.transform(CenterCrop())
requestOptions = requestOptions.centerCrop()
return this
}
@ -213,7 +215,7 @@ object ImageLoader {
}
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>) :

@ -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

@ -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,15 +40,11 @@ class ContentView : FrameLayout {
}
fun setBg(bg: Drawable?) {
if (bg is ColorDrawable) {
bgImage.setImageDrawable(null)
bgImage.background = bg
} else {
//all supported
ImageLoader.load(context, bg)
.centerCrop()
.setAsDrawable(bgImage)
}
}
fun upTime() {

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

@ -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 {

@ -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? {

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

@ -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)//绘制

@ -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)
}
}
}
Loading…
Cancel
Save