|
@@ -151,19 +151,11 @@ class OneBottomNavigationBar : View {
|
|
|
init(context, attrs, 0)
|
|
|
}
|
|
|
|
|
|
- constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
|
|
|
- context,
|
|
|
- attrs,
|
|
|
- defStyleAttr
|
|
|
- ) {
|
|
|
+ constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
|
|
|
init(context, attrs, defStyleAttr)
|
|
|
}
|
|
|
|
|
|
|
|
|
- public fun getList(): MutableList<Item> {
|
|
|
- return itemList;
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 设置选中的监听事件
|
|
|
*
|
|
@@ -229,19 +221,11 @@ class OneBottomNavigationBar : View {
|
|
|
* 绑定 ViewPager 使用,ViewPager 使用的 Fragment 数量需要与 Tab 数量一致,否则会出现 Fragment 显示与 tab 选中状态不对应的情况
|
|
|
* @param viewPager
|
|
|
*/
|
|
|
- fun attachViewPager(
|
|
|
- fragmentManager: FragmentManager,
|
|
|
- viewPager: ViewPager,
|
|
|
- fragments: List<Fragment>
|
|
|
- ) {
|
|
|
+ fun attachViewPager(fragmentManager: FragmentManager, viewPager: ViewPager, fragments: List<Fragment>) {
|
|
|
this.mViewPager = viewPager
|
|
|
if (this.mViewPager != null) {
|
|
|
mViewPager?.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
|
|
|
- override fun onPageScrolled(
|
|
|
- position: Int,
|
|
|
- positionOffset: Float,
|
|
|
- positionOffsetPixels: Int
|
|
|
- ) {
|
|
|
+ override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
|
|
|
}
|
|
|
|
|
|
override fun onPageSelected(position: Int) {
|
|
@@ -458,7 +442,7 @@ class OneBottomNavigationBar : View {
|
|
|
params.topMargin = 0 - floatingUp
|
|
|
}
|
|
|
is RelativeLayout.LayoutParams -> {
|
|
|
- params.topMargin = 0 - floatingUp * 2
|
|
|
+ params.topMargin = 0 - floatingUp
|
|
|
}
|
|
|
is FrameLayout.LayoutParams -> {
|
|
|
params.topMargin = 0 - floatingUp
|
|
@@ -482,41 +466,20 @@ class OneBottomNavigationBar : View {
|
|
|
private fun init(context: Context, attrs: AttributeSet?, defStyleAttr: Int) {
|
|
|
if (attrs != null) {
|
|
|
val ta = context.obtainStyledAttributes(attrs, R.styleable.One_StyleBottomLayout)
|
|
|
- itemIconTintRes =
|
|
|
- ta.getColorStateList(R.styleable.One_StyleBottomLayout_oneItemIconTint)
|
|
|
- itemColorStateList =
|
|
|
- ta.getColorStateList(R.styleable.One_StyleBottomLayout_oneItemTextColor)
|
|
|
+ itemIconTintRes = ta.getColorStateList(R.styleable.One_StyleBottomLayout_oneItemIconTint)
|
|
|
+ itemColorStateList = ta.getColorStateList(R.styleable.One_StyleBottomLayout_oneItemTextColor)
|
|
|
if (itemColorStateList == null) {
|
|
|
- itemColorStateList = ResourcesCompat.getColorStateList(
|
|
|
- resources,
|
|
|
- R.drawable.default_blue_tab_tint,
|
|
|
- null
|
|
|
- )
|
|
|
+ itemColorStateList = ResourcesCompat.getColorStateList(resources, R.drawable.default_blue_tab_tint, null)
|
|
|
}
|
|
|
- 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()
|
|
|
- textTop = ta.getDimension(
|
|
|
- R.styleable.One_StyleBottomLayout_oneItemTextTopMargin,
|
|
|
- DensityUtils.dpToPx(resources, 3f).toFloat()
|
|
|
- ).toInt()
|
|
|
- itemIconWidth =
|
|
|
- ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemIconWidth, 0f).toInt()
|
|
|
- itemIconHeight =
|
|
|
- ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemIconHeight, 0f).toInt()
|
|
|
- itemPadding =
|
|
|
- ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemPadding, 0f).toInt()
|
|
|
- itemFloatingPadding =
|
|
|
- ta.getDimension(R.styleable.One_StyleBottomLayout_oneFloatingPadding, 0f).toInt()
|
|
|
+ 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()
|
|
|
+ textTop = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemTextTopMargin, DensityUtils.dpToPx(resources, 3f).toFloat()).toInt()
|
|
|
+ itemIconWidth = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemIconWidth, 0f).toInt()
|
|
|
+ itemIconHeight = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemIconHeight, 0f).toInt()
|
|
|
+ itemPadding = ta.getDimension(R.styleable.One_StyleBottomLayout_oneItemPadding, 0f).toInt()
|
|
|
+ itemFloatingPadding = ta.getDimension(R.styleable.One_StyleBottomLayout_oneFloatingPadding, 0f).toInt()
|
|
|
val xmlRes = ta.getResourceId(R.styleable.One_StyleBottomLayout_oneMenu, 0)
|
|
|
parseXml(xmlRes)
|
|
|
}
|
|
@@ -532,17 +495,13 @@ class OneBottomNavigationBar : View {
|
|
|
}
|
|
|
bgDrawable = if (background != null && background is ColorDrawable) {
|
|
|
background
|
|
|
-
|
|
|
} else if (background is StateListDrawable) {
|
|
|
background
|
|
|
} else if (background is GradientDrawable) {
|
|
|
background
|
|
|
-
|
|
|
} else {
|
|
|
- // ColorDrawable(Color.BLACK)
|
|
|
- resources.getDrawable(R.drawable.rounded_corners)
|
|
|
+ ColorDrawable(Color.WHITE)
|
|
|
}
|
|
|
- //
|
|
|
for (item in itemList) {
|
|
|
item.titleSize = titleSize
|
|
|
item.iconWidth = itemIconWidth
|
|
@@ -573,7 +532,7 @@ class OneBottomNavigationBar : View {
|
|
|
when (event) {
|
|
|
XmlPullParser.START_DOCUMENT -> {
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
- //Log.e(TAG, "xml解析开始")
|
|
|
+ Log.e(TAG, "xml解析开始")
|
|
|
}
|
|
|
}
|
|
|
XmlPullParser.START_TAG ->
|
|
@@ -583,51 +542,28 @@ class OneBottomNavigationBar : View {
|
|
|
val item = Item()
|
|
|
for (i in 0 until xmlParser.attributeCount) {
|
|
|
//两种方法获取属性值
|
|
|
-// Log.e(TAG, "第" + (i + 1) + "个属性:" + xmlParser.getAttributeName(i)
|
|
|
-// + ": " + xmlParser.getAttributeValue(i));
|
|
|
+ // Log.e(TAG, "第" + (i + 1) + "个属性:" + xmlParser.getAttributeName(i)
|
|
|
+ // + ": " + xmlParser.getAttributeValue(i));
|
|
|
if ("id".equals(xmlParser.getAttributeName(i), ignoreCase = true)) {
|
|
|
item.id = xmlParser.getAttributeResourceValue(i, 0)
|
|
|
- } else if ("icon".equals(
|
|
|
- xmlParser.getAttributeName(i),
|
|
|
- ignoreCase = true
|
|
|
- )
|
|
|
- ) {
|
|
|
+ } else if ("icon".equals(xmlParser.getAttributeName(i), ignoreCase = true)) {
|
|
|
val drawableId = xmlParser.getAttributeResourceValue(i, 0)
|
|
|
- val drawable =
|
|
|
- ResourcesCompat.getDrawable(resources, drawableId, null)
|
|
|
+ val drawable = ResourcesCompat.getDrawable(resources, drawableId, null)
|
|
|
item.drawable = drawable!!.constantState!!.newDrawable()
|
|
|
var stateListDrawable = StateListDrawable()
|
|
|
if (drawable is StateListDrawable) {
|
|
|
stateListDrawable = drawable
|
|
|
- stateListDrawable.state =
|
|
|
- intArrayOf(android.R.attr.state_checked)
|
|
|
+ stateListDrawable.state = intArrayOf(android.R.attr.state_checked)
|
|
|
stateListDrawable.mutate()
|
|
|
} else {
|
|
|
- val selectedDrawable =
|
|
|
- tintListDrawable(drawable, itemIconTintRes)
|
|
|
- selectedDrawable.state =
|
|
|
- intArrayOf(android.R.attr.state_checked)
|
|
|
- stateListDrawable.addState(
|
|
|
- intArrayOf(android.R.attr.state_checked),
|
|
|
- selectedDrawable.current
|
|
|
- )
|
|
|
- stateListDrawable.addState(
|
|
|
- intArrayOf(android.R.attr.state_selected),
|
|
|
- selectedDrawable.current
|
|
|
- )
|
|
|
- stateListDrawable.addState(
|
|
|
- intArrayOf(android.R.attr.state_pressed),
|
|
|
- selectedDrawable.current
|
|
|
- )
|
|
|
- stateListDrawable.addState(
|
|
|
- intArrayOf(android.R.attr.state_focused),
|
|
|
- selectedDrawable.current
|
|
|
- )
|
|
|
+ val selectedDrawable = tintListDrawable(drawable, itemIconTintRes)
|
|
|
+ selectedDrawable.state = intArrayOf(android.R.attr.state_checked)
|
|
|
+ stateListDrawable.addState(intArrayOf(android.R.attr.state_checked), selectedDrawable.current)
|
|
|
+ stateListDrawable.addState(intArrayOf(android.R.attr.state_selected), selectedDrawable.current)
|
|
|
+ stateListDrawable.addState(intArrayOf(android.R.attr.state_pressed), selectedDrawable.current)
|
|
|
+ stateListDrawable.addState(intArrayOf(android.R.attr.state_focused), selectedDrawable.current)
|
|
|
selectedDrawable.state = intArrayOf()
|
|
|
- stateListDrawable.addState(
|
|
|
- intArrayOf(),
|
|
|
- selectedDrawable.current
|
|
|
- )
|
|
|
+ stateListDrawable.addState(intArrayOf(), selectedDrawable.current)
|
|
|
}
|
|
|
item.icon = stateListDrawable
|
|
|
} else if ("title" == xmlParser.getAttributeName(i)) {
|
|
@@ -656,7 +592,7 @@ class OneBottomNavigationBar : View {
|
|
|
}
|
|
|
XmlPullParser.END_TAG -> {
|
|
|
if (BuildConfig.DEBUG) {
|
|
|
- // Log.e(TAG, "xml解析结束")
|
|
|
+ Log.e(TAG, "xml解析结束")
|
|
|
}
|
|
|
}
|
|
|
else -> {
|
|
@@ -697,18 +633,16 @@ class OneBottomNavigationBar : View {
|
|
|
val specMode = MeasureSpec.getMode(heightMeasureSpec)
|
|
|
mWidth = MeasureSpec.getSize(widthMeasureSpec)
|
|
|
mItemWidth = (mWidth - paddingLeft - paddingRight) / itemList.size
|
|
|
- topPadding = topPadding
|
|
|
+ topPadding = paddingTop
|
|
|
bottomPadding = paddingBottom
|
|
|
setTextPaint(titleSize, Color.BLACK)
|
|
|
- val textHeight = getTextHeight("事件", mTextPaint)
|
|
|
+ val textHeight = getTextHeight("首页", mTextPaint)
|
|
|
mHeight = if (specMode == View.MeasureSpec.AT_MOST) {
|
|
|
-
|
|
|
itemIconHeight = if (itemIconHeight < 50) itemIconHeight else 50
|
|
|
topPadding + bottomPadding + itemIconHeight + textHeight + textTop + itemPadding * 2
|
|
|
} else {
|
|
|
val height = MeasureSpec.getSize(heightMeasureSpec)
|
|
|
- this.itemIconHeight =
|
|
|
- height - topPadding - bottomPadding - textHeight - textTop - itemPadding * 2
|
|
|
+ this.itemIconHeight = height - topPadding - bottomPadding - textHeight - textTop - itemPadding * 2
|
|
|
height
|
|
|
}
|
|
|
this.itemIconWidth = this.itemIconHeight
|
|
@@ -727,13 +661,7 @@ class OneBottomNavigationBar : View {
|
|
|
override fun onDraw(canvas: Canvas) {
|
|
|
super.onDraw(canvas)
|
|
|
var floatingUpInt = getFloatingUpHeight()
|
|
|
-// canvas.drawLine(
|
|
|
-// 0f,
|
|
|
-// floatingUpInt.toFloat(),
|
|
|
-// mWidth.toFloat(),
|
|
|
-// floatingUpInt.toFloat(),
|
|
|
-// linePaint!!
|
|
|
-// )
|
|
|
+ canvas.drawLine(0f, floatingUpInt.toFloat(), mWidth.toFloat(), floatingUpInt.toFloat(), linePaint!!)
|
|
|
//画背景
|
|
|
drawFloating(canvas)
|
|
|
|
|
@@ -777,12 +705,7 @@ class OneBottomNavigationBar : View {
|
|
|
linePaint!!.style = Paint.Style.STROKE
|
|
|
canvas.drawCircle(x.toFloat(), y.toFloat(), r, linePaint!!)
|
|
|
paint.style = Paint.Style.FILL
|
|
|
- canvas.drawCircle(
|
|
|
- x.toFloat(),
|
|
|
- y.toFloat(),
|
|
|
- r - linePaint!!.strokeWidth / 2,
|
|
|
- paint
|
|
|
- )
|
|
|
+ canvas.drawCircle(x.toFloat(), y.toFloat(), r - linePaint!!.strokeWidth / 2, paint)
|
|
|
}
|
|
|
}
|
|
|
}
|
|
@@ -817,22 +740,11 @@ class OneBottomNavigationBar : View {
|
|
|
}
|
|
|
var rect = getItemRect(item, position)
|
|
|
if (!TextUtils.isEmpty(item.title)) {
|
|
|
- var color = if (item.isChecked) itemColorStateList!!.getColorForState(
|
|
|
- intArrayOf(android.R.attr.state_checked),
|
|
|
- itemColorStateList!!.defaultColor
|
|
|
- ) else itemColorStateList!!.defaultColor
|
|
|
+ var color = if (item.isChecked) itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor) else itemColorStateList!!.defaultColor
|
|
|
if (!item.isCheckable) {
|
|
|
- color = itemColorStateList!!.getColorForState(
|
|
|
- intArrayOf(android.R.attr.state_checked),
|
|
|
- itemColorStateList!!.defaultColor
|
|
|
- )
|
|
|
+ color = itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor)
|
|
|
}
|
|
|
- setTextPaint(
|
|
|
- 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
|
|
@@ -844,9 +756,74 @@ class OneBottomNavigationBar : View {
|
|
|
it.draw(canvas)
|
|
|
}
|
|
|
}
|
|
|
-// if (item.msgCount != 0) {
|
|
|
-// drawItemMsgCount(item, position, canvas)
|
|
|
-// }
|
|
|
+ if (item.msgCount != 0) {
|
|
|
+ drawItemMsgCount(item, position, canvas)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 画出消息数
|
|
|
+ */
|
|
|
+ private fun drawItemMsgCount(item: Item, position: Int, canvas: Canvas) {
|
|
|
+ val msgCountRect = getMsgCountRect(item, position)
|
|
|
+ var x = (msgCountRect.left + msgCountRect.right) / 2
|
|
|
+ var y = (msgCountRect.top + msgCountRect.bottom) / 2
|
|
|
+ var r = (msgCountRect.bottom - msgCountRect.top) / 2
|
|
|
+
|
|
|
+ if (item.msgCount > 0) {
|
|
|
+ var countStr = when {
|
|
|
+ item.msgCount > 99 -> {
|
|
|
+ setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
|
|
|
+ "99+"
|
|
|
+ }
|
|
|
+ item.msgCount < 10 -> {
|
|
|
+ setTextPaint(DensityUtils.dpToPx(resources, 9f), Color.WHITE)
|
|
|
+ item.msgCount.toString()
|
|
|
+ }
|
|
|
+ else -> {
|
|
|
+ setTextPaint(DensityUtils.dpToPx(resources, 8f), Color.WHITE)
|
|
|
+ item.msgCount.toString()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ val paint = createPaint(Color.RED, Paint.Style.FILL, 0f)
|
|
|
+ canvas.drawCircle(x.toFloat(), y.toFloat(), r.toFloat(), paint)
|
|
|
+ canvas.drawText(countStr, x.toFloat(), (y + (r - DEFAULT_MSG_COUNT_TEXT_PADDING) / 2).toFloat(), mTextPaint)
|
|
|
+ paint.style = Paint.Style.STROKE
|
|
|
+ paint.color = Color.WHITE
|
|
|
+ paint.strokeWidth = DensityUtils.dpToPx(resources, 1f).toFloat()
|
|
|
+ canvas.drawCircle(x.toFloat(), y.toFloat(), r.toFloat(), paint)
|
|
|
+ } else {
|
|
|
+ val paint = createPaint(Color.RED, Paint.Style.FILL, 0f)
|
|
|
+ canvas.drawCircle(x.toFloat(), y.toFloat(), r.toFloat(), paint)
|
|
|
+ paint.style = Paint.Style.STROKE
|
|
|
+ paint.color = Color.WHITE
|
|
|
+ paint.strokeWidth = DensityUtils.dpToPx(resources, 1f).toFloat()
|
|
|
+ canvas.drawCircle(x.toFloat(), y.toFloat(), r.toFloat(), paint)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private fun getMsgCountRect(item: Item, position: Int): Rect {
|
|
|
+ val r = if (item.msgCount > 0) {
|
|
|
+ setTextPaint(DensityUtils.dpToPx(resources, 7f), Color.WHITE)
|
|
|
+ getTextWidth("99+", mTextPaint!!) / 2 + DEFAULT_MSG_COUNT_TEXT_PADDING
|
|
|
+ } else {
|
|
|
+ 7
|
|
|
+ }
|
|
|
+ val rect = getIconRect(item, position)
|
|
|
+ var x = (rect.left + rect.right) / 2
|
|
|
+ var y = (rect.top + rect.bottom) / 2
|
|
|
+ var r2 = y - rect.top
|
|
|
+ val offset = sqrt(r2 * r2 * 1.0 / 2)
|
|
|
+ val msgRect = Rect()
|
|
|
+ msgRect.left = (x + offset - r).toInt()
|
|
|
+ msgRect.top = (y - offset - r).toInt()
|
|
|
+ if (item.msgCount > 0) {
|
|
|
+ msgRect.left += r / 3
|
|
|
+ msgRect.top += r / 3
|
|
|
+ }
|
|
|
+ msgRect.right = msgRect.left + 2 * r
|
|
|
+ msgRect.bottom = msgRect.top + 2 * r
|
|
|
+ return msgRect
|
|
|
}
|
|
|
|
|
|
|
|
@@ -888,16 +865,13 @@ class OneBottomNavigationBar : View {
|
|
|
val floating = getFloatingUpHeight()
|
|
|
var iconSize = itemIconWidth.coerceAtMost(itemIconHeight)
|
|
|
setTextPaint(titleSize, Color.BLACK)
|
|
|
- val textHeight = getTextHeight("事件", mTextPaint!!)
|
|
|
+ val textHeight = getTextHeight("首页", mTextPaint!!)
|
|
|
if (TextUtils.isEmpty(item.title)) {
|
|
|
iconSize += (textTop + textHeight)
|
|
|
}
|
|
|
to.top = rect.top
|
|
|
if (floatingEnable && item.isFloating) {
|
|
|
- iconSize += floating * 3 / 3
|
|
|
- } else {
|
|
|
- iconSize = 65
|
|
|
- to.top = 85
|
|
|
+ iconSize += floating * 3 / 4
|
|
|
}
|
|
|
iconSize += (itemPadding - item.padding) * 2//高度修复
|
|
|
to.left = (rect.left + rect.right - iconSize) / 2
|
|
@@ -1022,33 +996,17 @@ 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
|
|
|
- setTextPaint(
|
|
|
- if (item.titleSize == 0) DensityUtils.dpToPx(
|
|
|
- resources,
|
|
|
- 14f
|
|
|
- ) else item.titleSize, color
|
|
|
- )
|
|
|
+ val color = if (item.isChecked) itemColorStateList!!.getColorForState(intArrayOf(android.R.attr.state_checked), itemColorStateList!!.defaultColor) else itemColorStateList!!.defaultColor
|
|
|
+ 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
|
|
|
// width = height = height - textHeight - textTop;
|
|
|
}
|
|
|
- val centerX =
|
|
|
- paddingLeft + i * mItemWidth + (mItemWidth - width) / 2 + width / 2
|
|
|
+ val centerX = paddingLeft + i * mItemWidth + (mItemWidth - width) / 2 + width / 2
|
|
|
val centerY = mItemHeight / 2
|
|
|
val r = mItemHeight / 2
|
|
|
- if (y >= getFloatingUpHeight() || item.isFloating && isInCircle(
|
|
|
- centerX,
|
|
|
- centerY,
|
|
|
- r,
|
|
|
- x.toInt(),
|
|
|
- y.toInt()
|
|
|
- )
|
|
|
- ) {
|
|
|
+ if (y >= getFloatingUpHeight() || item.isFloating && isInCircle(centerX, centerY, r, x.toInt(), y.toInt())) {
|
|
|
setSelected(i)
|
|
|
}
|
|
|
}
|
|
@@ -1070,21 +1028,13 @@ class OneBottomNavigationBar : View {
|
|
|
* @param touchY 触摸位置的 Y 坐标
|
|
|
* @return
|
|
|
*/
|
|
|
- private fun isInCircle(
|
|
|
- vCenterX: Int,
|
|
|
- vCenterY: Int,
|
|
|
- r: Int,
|
|
|
- touchX: Int,
|
|
|
- touchY: Int
|
|
|
- ): Boolean {
|
|
|
+ private fun isInCircle(vCenterX: Int, vCenterY: Int, r: Int, touchX: Int, touchY: Int): Boolean {
|
|
|
//点击位置x坐标与圆心的x坐标的距离
|
|
|
val distanceX = Math.abs(vCenterX - touchX)
|
|
|
//点击位置y坐标与圆心的y坐标的距离
|
|
|
val distanceY = Math.abs(vCenterY - touchY)
|
|
|
//点击位置与圆心的直线距离
|
|
|
- val distanceZ =
|
|
|
- Math.sqrt(Math.pow(distanceX.toDouble(), 2.0) + Math.pow(distanceY.toDouble(), 2.0))
|
|
|
- .toInt()
|
|
|
+ val distanceZ = Math.sqrt(Math.pow(distanceX.toDouble(), 2.0) + Math.pow(distanceY.toDouble(), 2.0)).toInt()
|
|
|
|
|
|
//如果点击位置与圆心的距离大于圆的半径,证明点击位置没有在圆内
|
|
|
return distanceZ <= r
|
|
@@ -1110,6 +1060,37 @@ class OneBottomNavigationBar : View {
|
|
|
}
|
|
|
|
|
|
|
|
|
+ internal fun drawable2Bitmap(drawable: Drawable?): Bitmap {
|
|
|
+ if (drawable is BitmapDrawable) {
|
|
|
+ return drawable.bitmap
|
|
|
+ } else if (drawable is NinePatchDrawable) {
|
|
|
+ val bitmap = Bitmap
|
|
|
+ .createBitmap(
|
|
|
+ drawable.intrinsicWidth,
|
|
|
+ drawable.intrinsicHeight,
|
|
|
+ if (drawable.opacity != PixelFormat.OPAQUE)
|
|
|
+ Bitmap.Config.ARGB_8888
|
|
|
+ else
|
|
|
+ Bitmap.Config.RGB_565)
|
|
|
+ val canvas = Canvas(bitmap)
|
|
|
+ drawable.setBounds(0, 0, drawable.intrinsicWidth,
|
|
|
+ drawable.intrinsicHeight)
|
|
|
+ drawable.draw(canvas)
|
|
|
+ return bitmap
|
|
|
+ } else {
|
|
|
+ val bitmap: Bitmap
|
|
|
+ if (drawable!!.intrinsicWidth <= 0 || drawable.intrinsicHeight <= 0) {
|
|
|
+ bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888) // Single color bitmap will be created of 1x1 pixel
|
|
|
+ } else {
|
|
|
+ bitmap = Bitmap.createBitmap(drawable.intrinsicWidth, drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
|
|
|
+ }
|
|
|
+ val canvas = Canvas(bitmap)
|
|
|
+ drawable.setBounds(0, 0, canvas.width, canvas.height)
|
|
|
+ drawable.draw(canvas)
|
|
|
+ return bitmap
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 改变Fragment
|
|
|
*
|
|
@@ -1164,10 +1145,7 @@ class OneBottomNavigationBar : View {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private class ViewpagerAdapter(
|
|
|
- fragmentManager: FragmentManager,
|
|
|
- val fragments: List<Fragment>
|
|
|
- ) : FragmentPagerAdapter(fragmentManager) {
|
|
|
+ private class ViewpagerAdapter(fragmentManager: FragmentManager, val fragments: List<Fragment>) : FragmentPagerAdapter(fragmentManager) {
|
|
|
override fun getCount(): Int {
|
|
|
return fragments.size
|
|
|
}
|