feat: 优化

pull/209/head
kunfei 5 years ago
parent 31f390a1d2
commit d7d42dd8b3
  1. 40
      app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt
  2. 404
      app/src/main/java/io/legado/app/utils/ConstraintUtil.kt

@ -4,6 +4,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.graphics.drawable.Drawable
import android.view.MotionEvent
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.view.isGone
import androidx.core.view.isInvisible
@ -15,10 +16,7 @@ import io.legado.app.help.ReadBookConfig
import io.legado.app.help.ReadTipConfig
import io.legado.app.ui.book.read.page.entities.TextPage
import io.legado.app.ui.widget.BatteryView
import io.legado.app.utils.dp
import io.legado.app.utils.getCompatColor
import io.legado.app.utils.statusBarHeight
import io.legado.app.utils.visible
import io.legado.app.utils.*
import kotlinx.android.synthetic.main.view_book_page.view.*
import java.util.*
@ -102,10 +100,24 @@ class ContentView(context: Context) : FrameLayout(context) {
ll_footer.isGone = hideFooter
}
tvTitle = when (ReadTipConfig.chapterTitle) {
ReadTipConfig.tipHeaderLeft -> tv_header_left
ReadTipConfig.tipHeaderLeft -> {
ConstraintUtil(ll_header).begin()
.rightToLeftOf(R.id.tv_header_left, R.id.tv_header_right)
.setWidth(R.id.tv_header_left, 0)
.setHorizontalWeight(R.id.tv_header_left, 1f)
.commit()
tv_header_left
}
ReadTipConfig.tipHeaderMiddle -> tv_header_middle
ReadTipConfig.tipHeaderRight -> tv_header_right
ReadTipConfig.tipFooterLeft -> tv_footer_left
ReadTipConfig.tipFooterLeft -> {
ConstraintUtil(ll_footer).begin()
.rightToLeftOf(R.id.tv_footer_left, R.id.tv_footer_right)
.setWidth(R.id.tv_footer_left, 0)
.setHorizontalWeight(R.id.tv_footer_left, 1f)
.commit()
tv_footer_left
}
ReadTipConfig.tipFooterMiddle -> tv_footer_middle
ReadTipConfig.tipFooterRight -> tv_footer_right
else -> null
@ -128,10 +140,22 @@ class ContentView(context: Context) : FrameLayout(context) {
textSize = 12f
}
tvBattery = when (ReadTipConfig.battery) {
ReadTipConfig.tipHeaderLeft -> tv_header_left
ReadTipConfig.tipHeaderLeft -> {
ConstraintUtil(ll_header).begin()
.clear(R.id.tv_header_left, 2)
.setWidth(R.id.tv_header_left, ViewGroup.LayoutParams.WRAP_CONTENT)
.commit()
tv_header_left
}
ReadTipConfig.tipHeaderMiddle -> tv_header_middle
ReadTipConfig.tipHeaderRight -> tv_header_right
ReadTipConfig.tipFooterLeft -> tv_footer_left
ReadTipConfig.tipFooterLeft -> {
ConstraintUtil(ll_header).begin()
.clear(R.id.tv_footer_left, 2)
.setWidth(R.id.tv_footer_left, ViewGroup.LayoutParams.WRAP_CONTENT)
.commit()
tv_footer_left
}
ReadTipConfig.tipFooterMiddle -> tv_footer_middle
ReadTipConfig.tipFooterRight -> tv_footer_right
else -> null

@ -6,7 +6,7 @@ import androidx.constraintlayout.widget.ConstraintSet
import androidx.transition.TransitionManager
class ConstraintUtil(val constraintLayout: ConstraintLayout) {
class ConstraintUtil(private val constraintLayout: ConstraintLayout) {
private var begin: ConstraintBegin? = null
private val applyConstraintSet = ConstraintSet()
@ -19,14 +19,14 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) {
/**
* 开始修改
*/
fun begin(): ConstraintBegin? {
fun begin(): ConstraintBegin {
synchronized(ConstraintBegin::class.java) {
if (begin == null) {
begin = ConstraintBegin()
begin = ConstraintBegin(constraintLayout, applyConstraintSet)
}
}
applyConstraintSet.clone(constraintLayout)
return begin
return begin!!
}
/**
@ -53,214 +53,228 @@ class ConstraintUtil(val constraintLayout: ConstraintLayout) {
resetConstraintSet.applyTo(constraintLayout)
}
inner class ConstraintBegin {
/**
* 清除关系<br></br>
* 注意这里不仅仅会清除关系还会清除对应控件的宽高为 w:0,h:0
* @param viewIds
* @return
*/
fun clear(@IdRes vararg viewIds: Int): ConstraintBegin {
for (viewId in viewIds) {
applyConstraintSet.clear(viewId)
}
return this
}
}
/**
* 清除某个控件的某个关系
* @param viewId
* @param anchor
* @return
*/
fun clear(viewId: Int, anchor: Int): ConstraintBegin {
applyConstraintSet.clear(viewId, anchor)
return this
}
class ConstraintBegin(
private val constraintLayout: ConstraintLayout,
private val applyConstraintSet: ConstraintSet
) {
/**
* 为某个控件设置 margin
* @param viewId 某个控件ID
* @param left marginLeft
* @param top marginTop
* @param right marginRight
* @param bottom marginBottom
* @return
*/
fun setMargin(
@IdRes viewId: Int,
left: Int,
top: Int,
right: Int,
bottom: Int
): ConstraintBegin {
setMarginLeft(viewId, left)
setMarginTop(viewId, top)
setMarginRight(viewId, right)
setMarginBottom(viewId, bottom)
return this
/**
* 清除关系<br></br>
* 注意这里不仅仅会清除关系还会清除对应控件的宽高为 w:0,h:0
* @param viewIds
* @return
*/
fun clear(@IdRes vararg viewIds: Int): ConstraintBegin {
for (viewId in viewIds) {
applyConstraintSet.clear(viewId)
}
return this
}
/**
* 为某个控件设置 marginLeft
* @param viewId 某个控件ID
* @param left marginLeft
* @return
*/
fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left)
return this
}
/**
* 清除某个控件的某个关系
* @param viewId
* @param anchor
* @return
*/
fun clear(viewId: Int, anchor: Int): ConstraintBegin {
applyConstraintSet.clear(viewId, anchor)
return this
}
/**
* 为某个控件设置 marginRight
* @param viewId 某个控件ID
* @param right marginRight
* @return
*/
fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right)
return this
}
fun setHorizontalWeight(viewId: Int, weight: Float): ConstraintBegin {
applyConstraintSet.setHorizontalWeight(viewId, weight)
return this
}
/**
* 为某个控件设置 marginTop
* @param viewId 某个控件ID
* @param top marginTop
* @return
*/
fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top)
return this
}
fun setVerticalWeight(viewId: Int, weight: Float): ConstraintBegin {
applyConstraintSet.setVerticalWeight(viewId, weight)
return this
}
/**
* 为某个控件设置marginBottom
* @param viewId 某个控件ID
* @param bottom marginBottom
* @return
*/
fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom)
return this
}
/**
* 为某个控件设置 margin
* @param viewId 某个控件ID
* @param left marginLeft
* @param top marginTop
* @param right marginRight
* @param bottom marginBottom
* @return
*/
fun setMargin(
@IdRes viewId: Int,
left: Int,
top: Int,
right: Int,
bottom: Int
): ConstraintBegin {
setMarginLeft(viewId, left)
setMarginTop(viewId, top)
setMarginRight(viewId, right)
setMarginBottom(viewId, bottom)
return this
}
/**
* 为某个控件设置关联关系 left_to_left_of
* @param startId
* @param endId
* @return
*/
fun Left_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT)
return this
}
/**
* 为某个控件设置 marginLeft
* @param viewId 某个控件ID
* @param left marginLeft
* @return
*/
fun setMarginLeft(@IdRes viewId: Int, left: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left)
return this
}
/**
* 为某个控件设置关联关系 left_to_right_of
* @param startId
* @param endId
* @return
*/
fun Left_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT)
return this
}
/**
* 为某个控件设置 marginRight
* @param viewId 某个控件ID
* @param right marginRight
* @return
*/
fun setMarginRight(@IdRes viewId: Int, right: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right)
return this
}
/**
* 为某个控件设置关联关系 top_to_top_of
* @param startId
* @param endId
* @return
*/
fun Top_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP)
return this
}
/**
* 为某个控件设置 marginTop
* @param viewId 某个控件ID
* @param top marginTop
* @return
*/
fun setMarginTop(@IdRes viewId: Int, top: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top)
return this
}
/**
* 为某个控件设置关联关系 top_to_bottom_of
* @param startId
* @param endId
* @return
*/
fun Top_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM)
return this
}
/**
* 为某个控件设置marginBottom
* @param viewId 某个控件ID
* @param bottom marginBottom
* @return
*/
fun setMarginBottom(@IdRes viewId: Int, bottom: Int): ConstraintBegin {
applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom)
return this
}
/**
* 为某个控件设置关联关系 right_to_left_of
* @param startId
* @param endId
* @return
*/
fun Right_toLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT)
return this
}
/**
* 为某个控件设置关联关系 left_to_left_of
* @param startId
* @param endId
* @return
*/
fun leftToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT)
return this
}
/**
* 为某个控件设置关联关系 right_to_right_of
* @param startId
* @param endId
* @return
*/
fun Right_toRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT)
return this
}
/**
* 为某个控件设置关联关系 left_to_right_of
* @param startId
* @param endId
* @return
*/
fun leftToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT)
return this
}
/**
* 为某个控件设置关联关系 bottom_to_bottom_of
* @param startId
* @param endId
* @return
*/
fun Bottom_toBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM)
return this
}
/**
* 为某个控件设置关联关系 top_to_top_of
* @param startId
* @param endId
* @return
*/
fun topToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP)
return this
}
/**
* 为某个控件设置关联关系 bottom_to_top_of
* @param startId
* @param endId
* @return
*/
fun Bottom_toTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP)
return this
}
/**
* 为某个控件设置关联关系 top_to_bottom_of
* @param startId
* @param endId
* @return
*/
fun topToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM)
return this
}
/**
* 为某个控件设置宽度
* @param viewId
* @param width
* @return
*/
fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin {
applyConstraintSet.constrainWidth(viewId, width)
return this
}
/**
* 为某个控件设置关联关系 right_to_left_of
* @param startId
* @param endId
* @return
*/
fun rightToLeftOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT)
return this
}
/**
* 某个控件设置高度
* @param viewId
* @param height
* @return
*/
fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin {
applyConstraintSet.constrainHeight(viewId, height)
return this
}
/**
* 为某个控件设置关联关系 right_to_right_of
* @param startId
* @param endId
* @return
*/
fun rightToRightOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT)
return this
}
/**
* 提交应用生效
*/
fun commit() {
applyConstraintSet.applyTo(constraintLayout)
}
/**
* 为某个控件设置关联关系 bottom_to_bottom_of
* @param startId
* @param endId
* @return
*/
fun bottomToBottomOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM)
return this
}
/**
* 为某个控件设置关联关系 bottom_to_top_of
* @param startId
* @param endId
* @return
*/
fun bottomToTopOf(@IdRes startId: Int, @IdRes endId: Int): ConstraintBegin {
applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP)
return this
}
}
/**
* 为某个控件设置宽度
* @param viewId
* @param width
* @return
*/
fun setWidth(@IdRes viewId: Int, width: Int): ConstraintBegin {
applyConstraintSet.constrainWidth(viewId, width)
return this
}
/**
* 某个控件设置高度
* @param viewId
* @param height
* @return
*/
fun setHeight(@IdRes viewId: Int, height: Int): ConstraintBegin {
applyConstraintSet.constrainHeight(viewId, height)
return this
}
/**
* 提交应用生效
*/
fun commit() {
applyConstraintSet.applyTo(constraintLayout)
}
}

Loading…
Cancel
Save