diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index f75e671..09a3ba7 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -29,7 +29,6 @@ app:itemPadding="10dp" app:iconTint="#C8FFFFFF" app:iconTintActive="#FFFFFF" - app:duration="200" app:menu="@menu/menu_bottom" app:layout_constraintBottom_toBottomOf="parent"/> diff --git a/lib/src/main/java/me/ibrahimsn/lib/BottomBarItem.kt b/lib/src/main/java/me/ibrahimsn/lib/BottomBarItem.kt index 127337f..f4e5eb2 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/BottomBarItem.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/BottomBarItem.kt @@ -5,7 +5,7 @@ import android.graphics.drawable.Drawable data class BottomBarItem( var title: String, - val icon: Drawable?, + val icon: Drawable, var rect: RectF = RectF(), var alpha: Int ) \ No newline at end of file diff --git a/lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt b/lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt index 19291a7..356a8f3 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt @@ -35,13 +35,8 @@ class BottomBarParser(private val context: Context, @XmlRes res: Int) { for (index in 0 until attributeCount) { when (parser.getAttributeName(index)) { - ICON_ATTRIBUTE -> { - itemDrawable = try { - ContextCompat.getDrawable(context, parser.getAttributeResourceValue(index, 0)) - } catch (notFoundException: Resources.NotFoundException) { - null - } - } + ICON_ATTRIBUTE -> + itemDrawable = ContextCompat.getDrawable(context, parser.getAttributeResourceValue(index, 0)) TITLE_ATTRIBUTE -> { itemText = try { context.getString(parser.getAttributeResourceValue(index, 0)) @@ -52,6 +47,9 @@ class BottomBarParser(private val context: Context, @XmlRes res: Int) { } } + if (itemDrawable == null) + throw Throwable("Item icon can not be null!") + return BottomBarItem(itemText ?: "", itemDrawable, alpha = 0) } } \ No newline at end of file diff --git a/lib/src/main/java/me/ibrahimsn/lib/Constants.kt b/lib/src/main/java/me/ibrahimsn/lib/Constants.kt index c96fb04..168ff1b 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/Constants.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/Constants.kt @@ -14,7 +14,7 @@ object Constants { const val DEFAULT_SIDE_MARGIN = 10f const val DEFAULT_ITEM_PADDING = 10f - const val DEFAULT_ANIM_DURATION = 300L + const val DEFAULT_ANIM_DURATION = 200L const val DEFAULT_ICON_SIZE = 18F const val DEFAULT_ICON_MARGIN = 4F const val DEFAULT_TEXT_SIZE = 11F diff --git a/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt b/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt index a5ca5b1..ea20898 100644 --- a/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt +++ b/lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt @@ -9,6 +9,7 @@ import android.graphics.Color import android.graphics.Paint import android.graphics.RectF import android.util.AttributeSet +import android.util.Log import android.view.MotionEvent import android.view.View import android.view.animation.DecelerateInterpolator @@ -157,17 +158,16 @@ class SmoothBottomBar : View { for ((index, item) in items.withIndex()) { val textLength = paintText.measureText(item.title) - if (item.icon != null) { - item.icon.mutate() - item.icon.setBounds(item.rect.centerX().toInt() - itemIconSize.toInt() / 2 - ((textLength / 2) * (1 - (OPAQUE - item.alpha) / OPAQUE)).toInt(), - height / 2 - itemIconSize.toInt() / 2, - item.rect.centerX().toInt() + itemIconSize.toInt() / 2 - ((textLength / 2) * (1 - (OPAQUE - item.alpha) / OPAQUE)).toInt(), - height / 2 + itemIconSize.toInt() / 2 - ) + item.icon.mutate() + item.icon.setBounds(item.rect.centerX().toInt() - itemIconSize.toInt() / 2 - ((textLength / 2) * (1 - (OPAQUE - item.alpha) / OPAQUE.toFloat())).toInt(), + height / 2 - itemIconSize.toInt() / 2, + item.rect.centerX().toInt() + itemIconSize.toInt() / 2 - ((textLength / 2) * (1 - (OPAQUE - item.alpha) / OPAQUE.toFloat())).toInt(), + height / 2 + itemIconSize.toInt() / 2 + ) + + DrawableCompat.setTint(item.icon, if(index == activeItemIndex) currentIconTint else itemIconTint) + item.icon.draw(canvas) - DrawableCompat.setTint(item.icon, if(index == activeItemIndex) currentIconTint else itemIconTint) - item.icon.draw(canvas) - } this.paintText.alpha = item.alpha canvas.drawText(item.title, item.rect.centerX() + itemIconSize / 2 + itemIconMargin, item.rect.centerY() - textHeight, paintText) } @@ -205,14 +205,15 @@ class SmoothBottomBar : View { fun setActiveItem(pos: Int) { activeItemIndex = pos - animateIndicator(pos) + for ((index, item) in items.withIndex()) { - if (index == pos) { + if (index == pos) animateAlpha(item, OPAQUE) - } else { + else animateAlpha(item, TRANSPARENT) - } } + + animateIndicator(pos) animateIconTint() }