feat: 优化代码

pull/117/head
kunfei 5 years ago
parent ce5cf8a3f2
commit 1f771d4fc0
  1. 42
      app/src/main/java/io/legado/app/ui/book/read/page/delegate/SimulationPageDelegate.kt

@ -221,13 +221,13 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
override fun onDraw(canvas: Canvas) { override fun onDraw(canvas: Canvas) {
if (mDirection === Direction.NEXT) { if (mDirection === Direction.NEXT) {
calcPoints() calcPoints()
drawCurrentPageArea(canvas, curBitmap, mPath0) drawCurrentPageArea(canvas, curBitmap)
drawNextPageAreaAndShadow(canvas, nextBitmap) drawNextPageAreaAndShadow(canvas, nextBitmap)
drawCurrentPageShadow(canvas) drawCurrentPageShadow(canvas)
drawCurrentBackArea(canvas, curBitmap) drawCurrentBackArea(canvas, curBitmap)
} else { } else {
calcPoints() calcPoints()
drawCurrentPageArea(canvas, prevBitmap, mPath0) drawCurrentPageArea(canvas, prevBitmap)
drawNextPageAreaAndShadow(canvas, curBitmap) drawNextPageAreaAndShadow(canvas, curBitmap)
drawCurrentPageShadow(canvas) drawCurrentPageShadow(canvas)
drawCurrentBackArea(canvas, prevBitmap) drawCurrentBackArea(canvas, prevBitmap)
@ -258,7 +258,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
val left: Int val left: Int
val right: Int val right: Int
if (mIsRtOrLb) { if (mIsRtOrLb) {
left = mBezierStart1.x.toInt() - 1 left = (mBezierStart1.x - 1).toInt()
right = (mBezierStart1.x + f3 + 1).toInt() right = (mBezierStart1.x + f3 + 1).toInt()
mFolderShadowDrawable = mFolderShadowDrawableLR mFolderShadowDrawable = mFolderShadowDrawableLR
} else { } else {
@ -337,15 +337,14 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
var mCurrentPageShadow: GradientDrawable var mCurrentPageShadow: GradientDrawable
if (mIsRtOrLb) { if (mIsRtOrLb) {
leftX = mBezierControl1.x.toInt() leftX = mBezierControl1.x.toInt()
rightX = mBezierControl1.x.toInt() + 25 rightX = (mBezierControl1.x + 25).toInt()
mCurrentPageShadow = mFrontShadowDrawableVLR mCurrentPageShadow = mFrontShadowDrawableVLR
} else { } else {
leftX = mBezierControl1.x.toInt() - 25 leftX = (mBezierControl1.x - 25).toInt()
rightX = mBezierControl1.x.toInt() + 1 rightX = (mBezierControl1.x + 1).toInt()
mCurrentPageShadow = mFrontShadowDrawableVRL mCurrentPageShadow = mFrontShadowDrawableVRL
} }
var rotateDegrees: Float = var rotateDegrees = Math.toDegrees(
Math.toDegrees(
atan2(mTouchX - mBezierControl1.x, mBezierControl1.y - mTouchY).toDouble() atan2(mTouchX - mBezierControl1.x, mBezierControl1.y - mTouchY).toDouble()
).toFloat() ).toFloat()
canvas.rotate(rotateDegrees, mBezierControl1.x, mBezierControl1.y) canvas.rotate(rotateDegrees, mBezierControl1.x, mBezierControl1.y)
@ -372,24 +371,25 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
if (mIsRtOrLb) { if (mIsRtOrLb) {
leftX = mBezierControl2.y.toInt() leftX = mBezierControl2.y.toInt()
rightX = mBezierControl2.y.toInt() + 25 rightX = (mBezierControl2.y + 25).toInt()
mCurrentPageShadow = mFrontShadowDrawableHTB mCurrentPageShadow = mFrontShadowDrawableHTB
} else { } else {
leftX = mBezierControl2.y.toInt() - 25 leftX = (mBezierControl2.y - 25).toInt()
rightX = mBezierControl2.y.toInt() + 1 rightX = (mBezierControl2.y + 1).toInt()
mCurrentPageShadow = mFrontShadowDrawableHBT mCurrentPageShadow = mFrontShadowDrawableHBT
} }
rotateDegrees = Math.toDegrees( rotateDegrees = Math.toDegrees(
atan2(mBezierControl2.y - mTouchY, mBezierControl2.x - mTouchX).toDouble() atan2(mBezierControl2.y - mTouchY, mBezierControl2.x - mTouchX).toDouble()
).toFloat() ).toFloat()
canvas.rotate(rotateDegrees, mBezierControl2.x, mBezierControl2.y) canvas.rotate(rotateDegrees, mBezierControl2.x, mBezierControl2.y)
val temp: Float = val temp =
if (mBezierControl2.y < 0) mBezierControl2.y - viewHeight else mBezierControl2.y if (mBezierControl2.y < 0) (mBezierControl2.y - viewHeight).toDouble()
val hmg = hypot(mBezierControl2.x.toDouble(), temp.toDouble()).toInt() else mBezierControl2.y.toDouble()
val hmg = hypot(mBezierControl2.x.toDouble(), temp)
if (hmg > mMaxLength) if (hmg > mMaxLength)
mCurrentPageShadow.setBounds( mCurrentPageShadow.setBounds(
(mBezierControl2.x - 25).toInt() - hmg, leftX, (mBezierControl2.x - 25 - hmg).toInt(), leftX,
(mBezierControl2.x + mMaxLength).toInt() - hmg, rightX (mBezierControl2.x + mMaxLength - hmg).toInt(), rightX
) )
else else
mCurrentPageShadow.setBounds( mCurrentPageShadow.setBounds(
@ -400,6 +400,7 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
canvas.restore() canvas.restore()
} }
//
private fun drawNextPageAreaAndShadow( private fun drawNextPageAreaAndShadow(
canvas: Canvas, canvas: Canvas,
bitmap: Bitmap? bitmap: Bitmap?
@ -447,10 +448,10 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
canvas.restore() canvas.restore()
} }
//
private fun drawCurrentPageArea( private fun drawCurrentPageArea(
canvas: Canvas, canvas: Canvas,
bitmap: Bitmap?, bitmap: Bitmap?
path: Path
) { ) {
bitmap ?: return bitmap ?: return
mPath0.reset() mPath0.reset()
@ -461,11 +462,12 @@ class SimulationPageDelegate(pageView: PageView) : HorizontalPageDelegate(pageVi
mPath0.quadTo(mBezierControl2.x, mBezierControl2.y, mBezierStart2.x, mBezierStart2.y) mPath0.quadTo(mBezierControl2.x, mBezierControl2.y, mBezierStart2.x, mBezierStart2.y)
mPath0.lineTo(mCornerX.toFloat(), mCornerY.toFloat()) mPath0.lineTo(mCornerX.toFloat(), mCornerY.toFloat())
mPath0.close() mPath0.close()
canvas.save() canvas.save()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
canvas.clipOutPath(path) canvas.clipOutPath(mPath0)
} else { } else {
canvas.clipPath(path, Region.Op.XOR) canvas.clipPath(mPath0, Region.Op.XOR)
} }
canvas.drawBitmap(bitmap, 0f, 0f, null) canvas.drawBitmap(bitmap, 0f, 0f, null)
canvas.restore() canvas.restore()

Loading…
Cancel
Save