Skip to content

Commit

Permalink
fix item drawable animation
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrahimsn98 committed Nov 21, 2019
1 parent 35df2b9 commit fab6c49
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 24 deletions.
1 change: 0 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"/>

Expand Down
2 changes: 1 addition & 1 deletion lib/src/main/java/me/ibrahimsn/lib/BottomBarItem.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 5 additions & 7 deletions lib/src/main/java/me/ibrahimsn/lib/BottomBarParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion lib/src/main/java/me/ibrahimsn/lib/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 15 additions & 14 deletions lib/src/main/java/me/ibrahimsn/lib/SmoothBottomBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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()
}

Expand Down

0 comments on commit fab6c49

Please sign in to comment.