feat: 优化代码

pull/111/head
kunfei 5 years ago
parent 2a87606be5
commit d291540513
  1. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  2. 12
      app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt
  3. 24
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  4. 2
      app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt

@ -71,8 +71,7 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
override val viewModel: ReadBookViewModel
get() = getViewModel(ReadBookViewModel::class.java)
override val isInitFinish: Boolean
get() = viewModel.isInitFinish
override val isInitFinish: Boolean get() = viewModel.isInitFinish
private val mHandler = Handler()
private val keepScreenRunnable: Runnable = Runnable { Help.keepScreenOn(window, false) }
@ -80,6 +79,8 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
private var screenTimeOut: Long = 0
private var timeElectricityReceiver: TimeElectricityReceiver? = null
override val headerHeight: Int get() = page_view.headerHeight
override fun onActivityCreated(savedInstanceState: Bundle?) {
Help.upLayoutInDisplayCutoutMode(window)
initView()
@ -373,10 +374,11 @@ class ReadBookActivity : VMBaseActivity<ReadBookViewModel>(R.layout.activity_boo
}
textActionMenu?.let { popup ->
if (!popup.isShowing) {
popup.showAsDropDown(
popup.showAtLocation(
cursor_left,
cursor_left.width,
-cursor_left.height - ReadBookConfig.durConfig.textSize.dp
Gravity.BOTTOM or Gravity.START,
cursor_left.x.toInt() + cursor_left.width,
page_view.height - cursor_left.y.toInt() + ReadBookConfig.durConfig.textSize.dp + popup.height
)
}
}

@ -22,8 +22,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
style = Paint.Style.FILL
}
}
private var activityCallBack: CallBack? = null
var headerHeight = 0
private var activityCallBack: CallBack
var selectAble = context.getPrefBoolean(PreferKey.textSelectAble)
var selectStartLine = 0
var selectStartChar = 0
@ -32,7 +31,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
private var textPage: TextPage? = null
init {
activityCallBack = activity as? CallBack
activityCallBack = activity as CallBack
}
fun setContent(textPage: TextPage?) {
@ -181,11 +180,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
private fun upSelectedStart(x: Float, y: Float) {
activityCallBack?.upSelectedStart(x, y + headerHeight)
activityCallBack.upSelectedStart(x, y + activityCallBack.headerHeight)
}
private fun upSelectedEnd(x: Float, y: Float) {
activityCallBack?.upSelectedEnd(x, y + headerHeight)
activityCallBack.upSelectedEnd(x, y + activityCallBack.headerHeight)
}
fun cancelSelect() {
@ -197,7 +196,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
invalidate()
}
activityCallBack?.onCancelSelect()
activityCallBack.onCancelSelect()
}
val selectedText: String
@ -209,5 +208,6 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
fun upSelectedStart(x: Float, y: Float)
fun upSelectedEnd(x: Float, y: Float)
fun onCancelSelect()
val headerHeight: Int
}
}

@ -23,6 +23,7 @@ class ContentView(context: Context) : FrameLayout(context) {
//设置背景防止切换背景时文字重叠
setBackgroundColor(context.getCompatColor(R.color.background))
inflate(context, R.layout.view_book_page, this)
upStyle()
upTime()
}
@ -33,9 +34,8 @@ class ContentView(context: Context) : FrameLayout(context) {
tv_top_right.typeface = ChapterProvider.typeface
tv_bottom_left.typeface = ChapterProvider.typeface
tv_bottom_right.typeface = ChapterProvider.typeface
if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
//显示状态栏时隐藏header
ll_header.visible()
if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
ll_header.layoutParams =
ll_header.layoutParams.apply { height = context.getStatusBarHeight() }
ll_header.setPadding(
@ -44,12 +44,11 @@ class ContentView(context: Context) : FrameLayout(context) {
headerPaddingRight.dp,
headerPaddingBottom.dp
)
content_text_view.headerHeight = ll_header.height
ll_header.visible()
page_panel.setPadding(0, 0, 0, 0)
} else {
ll_header.gone()
content_text_view.headerHeight = context.getStatusBarHeight()
page_panel.setPadding(0, content_text_view.headerHeight, 0, 0)
page_panel.setPadding(0, context.getStatusBarHeight(), 0, 0)
}
content_text_view.setPadding(
paddingLeft.dp,
@ -72,6 +71,15 @@ class ContentView(context: Context) : FrameLayout(context) {
}
}
val headerHeight: Int
get() {
return if (context.getPrefBoolean(PreferKey.hideStatusBar, false)) {
ll_header.height
} else {
context.getStatusBarHeight()
}
}
fun setBg(bg: Drawable?) {
page_panel.background = bg
}
@ -105,16 +113,16 @@ class ContentView(context: Context) : FrameLayout(context) {
}
fun selectText(e: MotionEvent): Boolean {
val y = e.y - content_text_view.headerHeight
val y = e.y - headerHeight
return content_text_view.selectText(e.x, y)
}
fun selectStartMove(x: Float, y: Float) {
content_text_view.selectStartMove(x, y - content_text_view.headerHeight)
content_text_view.selectStartMove(x, y - headerHeight)
}
fun selectEndMove(x: Float, y: Float) {
content_text_view.selectEndMove(x, y - content_text_view.headerHeight)
content_text_view.selectEndMove(x, y - headerHeight)
}
fun cancelSelect() {

@ -191,6 +191,8 @@ class PageView(context: Context, attrs: AttributeSet) :
curPage.selectEndMove(x, y)
}
val headerHeight: Int get() = curPage.headerHeight
val selectedText: String get() = curPage.selectedText
override val isScrollDelegate: Boolean

Loading…
Cancel
Save