Skip to content

Commit

Permalink
change:修复上边界划线问题
Browse files Browse the repository at this point in the history
  • Loading branch information
onestravel committed Jun 5, 2020
1 parent 0465b54 commit 229dff9
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -60,39 +60,55 @@ typealias OnItemSelectedListener = (item: OneBottomNavigationBar.Item, position:

class OneBottomNavigationBar : View {
private val TAG = "BottomNavigationBar"

// 导航菜单键列表
private var itemList: MutableList<Item> = ArrayList()

//总宽度 width
private var mWidth = 0

//总高度 height
private var mHeight = 0

//每个菜单的宽度 item width
private var mItemWidth = 0

//每个菜单的告诉 item height
private var mItemHeight = 0

//整体的上边距
private var topPadding = DensityUtils.dpToPx(resources, 3f)

//整体下边距
private var bottomPadding = DensityUtils.dpToPx(resources, 3f)

//文字相对于图标的边距
private var textTop = DensityUtils.dpToPx(resources, 3f)

//画笔
private val mPaint: Paint by lazy { Paint() }
private val mTextPaint: Paint by lazy { Paint() }

//图标的状态颜色列表
private var itemIconTintRes: ColorStateList? = null

//文字的状态颜色列表
private var itemColorStateList: ColorStateList? = null

//Item菜单的选中事件
private var onItemSelectedListener: OnItemSelectedListener? = null

// 当前选中的坐标位置
private var checkedPosition = 0

//是否开启上浮
private var floatingEnable: Boolean = false

//上浮距离
private var floatingUp: Int = 0

//背景资源
private var bgDrawable: Drawable? = null

//菜单的布局文件
@MenuRes
private var menuRes: Int = 0
Expand All @@ -117,6 +133,7 @@ class OneBottomNavigationBar : View {
*/
var isReplace: Boolean = false
private var linePaint: Paint? = null
private val lineWidth by lazy { DensityUtils.dpToPx(context, 1f).toFloat() }
private var topLineColor: Int = 0
private final val DEFAULT_MSG_COUNT_TEXT_PADDING: Int = DensityUtils.dpToPx(resources, 2f)

Expand Down Expand Up @@ -381,7 +398,7 @@ class OneBottomNavigationBar : View {
if (itemColorStateList == null) {
itemColorStateList = ResourcesCompat.getColorStateList(resources, R.drawable.default_blue_tab_tint, null)
}
topLineColor = ta.getColor(R.styleable.One_StyleBottomLayout_oneItemTopLineColor, Color.parseColor("#CCCCCC"))
topLineColor = ta.getColor(R.styleable.One_StyleBottomLayout_oneItemTopLineColor, Color.TRANSPARENT)
floatingEnable = ta.getBoolean(R.styleable.One_StyleBottomLayout_oneFloatingEnable, false)
floatingUp = ta.getDimension(R.styleable.One_StyleBottomLayout_oneFloatingUp, 20f).toInt()
titleSize = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemTextSize, DensityUtils.spToPx(resources, 12f).toFloat()).toInt()
Expand Down Expand Up @@ -423,7 +440,7 @@ class OneBottomNavigationBar : View {
}
}
linePaint = createPaint(topLineColor)
linePaint!!.strokeWidth = DensityUtils.dpToPx(context, 1f).toFloat()
linePaint!!.strokeWidth = lineWidth
}

/**
Expand Down Expand Up @@ -546,7 +563,7 @@ class OneBottomNavigationBar : View {
mItemWidth = (mWidth - paddingLeft - paddingRight) / itemList.size
topPadding = paddingTop
bottomPadding = paddingBottom
createTextPaint(titleSize, Color.BLACK)
setTextPaint(titleSize, Color.BLACK)
val textHeight = getTextHeight("首页", mTextPaint)
mHeight = if (specMode == View.MeasureSpec.AT_MOST) {
itemIconHeight = if (itemIconHeight < 50) itemIconHeight else 50
Expand Down Expand Up @@ -577,7 +594,7 @@ class OneBottomNavigationBar : View {
drawFloating(canvas)

val rectInit = Rect()
rectInit.set(0, floatingUpInt, mWidth, mHeight)
rectInit.set(0, (floatingUpInt + lineWidth/2).toInt(), mWidth, mHeight)
bgDrawable!!.bounds = rectInit
bgDrawable!!.draw(canvas)

Expand Down Expand Up @@ -655,7 +672,7 @@ class OneBottomNavigationBar : View {
if (!item.isCheckable) {
color = itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor)
}
createTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
setTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
val textHeight = getTextHeight(item.title, mTextPaint)
val textX = (rect.left + rect.right) / 2
val textY = rect.bottom - textHeight / 4
Expand Down Expand Up @@ -684,15 +701,15 @@ class OneBottomNavigationBar : View {
if (item.msgCount > 0) {
var countStr = when {
item.msgCount > 99 -> {
createTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
"99+"
}
item.msgCount < 10 -> {
createTextPaint(DensityUtils.dpToPx(resources, 9f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 9f), Color.WHITE)
item.msgCount.toString()
}
else -> {
createTextPaint(DensityUtils.dpToPx(resources, 8f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 8f), Color.WHITE)
item.msgCount.toString()
}
}
Expand All @@ -715,7 +732,7 @@ class OneBottomNavigationBar : View {

private fun getMsgCountRect(item: Item, position: Int): Rect {
val r = if (item.msgCount > 0) {
createTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
getTextWidth("99+", mTextPaint!!) / 2 + DEFAULT_MSG_COUNT_TEXT_PADDING
} else {
7
Expand Down Expand Up @@ -775,7 +792,7 @@ class OneBottomNavigationBar : View {
val to = Rect()
val floating = getFloatingUpHeight()
var iconSize = itemIconWidth.coerceAtMost(itemIconHeight)
createTextPaint(titleSize, Color.BLACK)
setTextPaint(titleSize, Color.BLACK)
val textHeight = getTextHeight("首页", mTextPaint!!)
if (TextUtils.isEmpty(item.title)) {
iconSize += (textTop + textHeight)
Expand All @@ -799,13 +816,14 @@ class OneBottomNavigationBar : View {
* @param textColor 文字颜色
* @return
*/
private fun createTextPaint(textSize: Int, textColor: Int): Paint {
mTextPaint!!.color = textColor//设置画笔的颜色
mTextPaint!!.textSize = textSize.toFloat()//设置文字大小
mTextPaint!!.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
mTextPaint!!.textAlign = Paint.Align.CENTER
mTextPaint!!.style = Paint.Style.FILL_AND_STROKE
return mTextPaint as Paint
private fun setTextPaint(textSize: Int, textColor: Int): Paint {
return mTextPaint?.apply {
this.color = textColor//设置画笔的颜色
this.textSize = textSize.toFloat()//设置文字大小
this.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
this.textAlign = Paint.Align.CENTER
this.style = Paint.Style.FILL_AND_STROKE
}
}

/**
Expand All @@ -815,11 +833,12 @@ class OneBottomNavigationBar : View {
* @return
*/
private fun createPaint(color: Int): Paint {
mPaint.color = color
mPaint.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
mPaint.textAlign = Paint.Align.CENTER
mPaint.style = Paint.Style.FILL
return mPaint
return Paint().apply {
this.color = color
this.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
this.textAlign = Paint.Align.CENTER
this.style = Paint.Style.FILL
}
}


Expand Down Expand Up @@ -863,7 +882,7 @@ class OneBottomNavigationBar : View {
DrawableCompat.setTintMode(wrappedDrawable, PorterDuff.Mode.MULTIPLY)
DrawableCompat.setTintList(wrappedDrawable, colors)
wrappedDrawable
}else{
} else {
drawable
}
}
Expand Down Expand Up @@ -905,7 +924,7 @@ class OneBottomNavigationBar : View {
val height = mItemHeight - topPadding - bottomPadding
if (!TextUtils.isEmpty(item.title)) {
val color = if (item.isChecked) itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor) else itemColorStateList!!.defaultColor
createTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
setTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
val textHeight = getTextHeight(item.title, mTextPaint!!)
val textY = startTop + height - textHeight / 4//上边距+图片文字内容高度
val w = textY - textHeight / 2 - topPadding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class OneBottomNavigationBar : View {
private var textTop = DensityUtils.dpToPx(resources, 3f)

//画笔
private val mPaint: Paint by lazy { Paint() }
private val mTextPaint: Paint by lazy { Paint() }

//图标的状态颜色列表
Expand Down Expand Up @@ -129,6 +128,7 @@ class OneBottomNavigationBar : View {
*/
var isReplace: Boolean = false
private var linePaint: Paint? = null
private val lineWidth by lazy { DensityUtils.dpToPx(context, 1f).toFloat() }
private var topLineColor: Int = 0
private final val DEFAULT_MSG_COUNT_TEXT_PADDING: Int = DensityUtils.dpToPx(resources, 2f)

Expand Down Expand Up @@ -393,7 +393,7 @@ class OneBottomNavigationBar : View {
if (itemColorStateList == null) {
itemColorStateList = ResourcesCompat.getColorStateList(resources, R.drawable.default_blue_tab_tint, null)
}
topLineColor = ta.getColor(R.styleable.One_StyleBottomLayout_oneItemTopLineColor, Color.parseColor("#CCCCCC"))
topLineColor = ta.getColor(R.styleable.One_StyleBottomLayout_oneItemTopLineColor, Color.TRANSPARENT)
floatingEnable = ta.getBoolean(R.styleable.One_StyleBottomLayout_oneFloatingEnable, false)
floatingUp = ta.getDimension(R.styleable.One_StyleBottomLayout_oneFloatingUp, 20f).toInt()
titleSize = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemTextSize, DensityUtils.spToPx(resources, 12f).toFloat()).toInt()
Expand Down Expand Up @@ -558,7 +558,7 @@ class OneBottomNavigationBar : View {
mItemWidth = (mWidth - paddingLeft - paddingRight) / itemList.size
topPadding = paddingTop
bottomPadding = paddingBottom
createTextPaint(titleSize, Color.BLACK)
setTextPaint(titleSize, Color.BLACK)
val textHeight = getTextHeight("首页", mTextPaint)
mHeight = if (specMode == View.MeasureSpec.AT_MOST) {
itemIconHeight = if (itemIconHeight < 50) itemIconHeight else 50
Expand Down Expand Up @@ -589,7 +589,7 @@ class OneBottomNavigationBar : View {
drawFloating(canvas)

val rectInit = Rect()
rectInit.set(0, floatingUpInt, mWidth, mHeight)
rectInit.set(0, (floatingUpInt + lineWidth/2).toInt(), mWidth, mHeight)
bgDrawable!!.bounds = rectInit
bgDrawable!!.draw(canvas)

Expand Down Expand Up @@ -667,7 +667,7 @@ class OneBottomNavigationBar : View {
if (!item.isCheckable) {
color = itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor)
}
createTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
setTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
val textHeight = getTextHeight(item.title, mTextPaint)
val textX = (rect.left + rect.right) / 2
val textY = rect.bottom - textHeight / 4
Expand Down Expand Up @@ -696,15 +696,15 @@ class OneBottomNavigationBar : View {
if (item.msgCount > 0) {
var countStr = when {
item.msgCount > 99 -> {
createTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
"99+"
}
item.msgCount < 10 -> {
createTextPaint(DensityUtils.dpToPx(resources, 9f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 9f), Color.WHITE)
item.msgCount.toString()
}
else -> {
createTextPaint(DensityUtils.dpToPx(resources, 8f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 8f), Color.WHITE)
item.msgCount.toString()
}
}
Expand All @@ -727,7 +727,7 @@ class OneBottomNavigationBar : View {

private fun getMsgCountRect(item: Item, position: Int): Rect {
val r = if (item.msgCount > 0) {
createTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
getTextWidth("99+", mTextPaint!!) / 2 + DEFAULT_MSG_COUNT_TEXT_PADDING
} else {
7
Expand Down Expand Up @@ -787,7 +787,7 @@ class OneBottomNavigationBar : View {
val to = Rect()
val floating = getFloatingUpHeight()
var iconSize = itemIconWidth.coerceAtMost(itemIconHeight)
createTextPaint(titleSize, Color.BLACK)
setTextPaint(titleSize, Color.BLACK)
val textHeight = getTextHeight("首页", mTextPaint!!)
if (TextUtils.isEmpty(item.title)) {
iconSize += (textTop + textHeight)
Expand All @@ -811,13 +811,14 @@ class OneBottomNavigationBar : View {
* @param textColor 文字颜色
* @return
*/
private fun createTextPaint(textSize: Int, textColor: Int): Paint {
mTextPaint!!.color = textColor//设置画笔的颜色
mTextPaint!!.textSize = textSize.toFloat()//设置文字大小
mTextPaint!!.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
mTextPaint!!.textAlign = Paint.Align.CENTER
mTextPaint!!.style = Paint.Style.FILL_AND_STROKE
return mTextPaint as Paint
private fun setTextPaint(textSize: Int, textColor: Int): Paint {
return mTextPaint?.apply {
this.color = textColor//设置画笔的颜色
this.textSize = textSize.toFloat()//设置文字大小
this.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
this.textAlign = Paint.Align.CENTER
this.style = Paint.Style.FILL_AND_STROKE
}
}

/**
Expand All @@ -827,11 +828,12 @@ class OneBottomNavigationBar : View {
* @return
*/
private fun createPaint(color: Int): Paint {
mPaint.color = color
mPaint.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
mPaint.textAlign = Paint.Align.CENTER
mPaint.style = Paint.Style.FILL
return mPaint
return Paint().apply {
this.color = color
this.isAntiAlias = true//设置抗锯齿功能 true表示抗锯齿 false则表示不需要这功能
this.textAlign = Paint.Align.CENTER
this.style = Paint.Style.FILL
}
}


Expand Down Expand Up @@ -917,7 +919,7 @@ class OneBottomNavigationBar : View {
val height = mItemHeight - topPadding - bottomPadding
if (!TextUtils.isEmpty(item.title)) {
val color = if (item.isChecked) itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor) else itemColorStateList!!.defaultColor
createTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
setTextPaint(if (item.titleSize == 0) DensityUtils.dpToPx(resources, 14f) else item.titleSize, color)
val textHeight = getTextHeight(item.title, mTextPaint!!)
val textY = startTop + height - textHeight / 4//上边距+图片文字内容高度
val w = textY - textHeight / 2 - topPadding
Expand Down
6 changes: 3 additions & 3 deletions OneBottomNavigationBar/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
<attr name="showFragment" format="boolean"></attr>
<declare-styleable name="One_StyleBottomLayout">
<attr name="oneItemPadding" format="dimension"></attr>
<attr name="oneItemIconTint" format="reference"></attr>
<attr name="oneItemIconTint" format="color|reference"></attr>
<attr name="oneItemIconWidth" format="dimension"></attr>
<attr name="oneItemIconHeight" format="dimension"></attr>
<attr name="oneItemTextColor" format="reference"></attr>
<attr name="oneItemTopLineColor" format="reference"></attr>
<attr name="oneItemTextColor" format="color|reference"></attr>
<attr name="oneItemTopLineColor" format="color|reference"></attr>
<attr name="oneItemTextSize" format="dimension"></attr>
<attr name="oneItemTextTopMargin" format="dimension"></attr>
<attr name="oneFloatingEnable" format="boolean"></attr>
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ dependencies {

### 版本更新说明

#### 1.2.2
- 修复菜单突出时,上边界划线失效问题


#### 1.2.0
- 优化图标尺寸计算方式
- 优化纬度角标绘制位置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class MainActivity : AppCompatActivity() {
oneBottomLayout.addFragment(R.id.tab1, FirstFragment())
oneBottomLayout.addFragment(R.id.tab4, FourFragment())
oneBottomLayout.addFragment(R.id.tab5, FiveFragment())
oneBottomLayout.setFloatingEnable(false)
oneBottomLayout.setFloatingEnable(true)
oneBottomLayout.setItemIconTint(R.drawable.item_check)
oneBottomLayout.setItemColorStateList(R.drawable.item_check)
oneBottomLayout.setMsgCount(0, 32)
Expand Down
Loading

0 comments on commit 229dff9

Please sign in to comment.