From 72d33319ed8f1f0e8b4e74b4e1153fb8cf586457 Mon Sep 17 00:00:00 2001 From: Ismael Di Vita Date: Sun, 22 Mar 2020 20:04:56 +0000 Subject: [PATCH] Icon size and animation issue (#33) * Add default unselectedColor * Add iconSize attribute * Fix animation for HorizontaMenuItem on portrait --- .../chipnavigation/ChipNavigationBar.kt | 17 +--------- .../chipnavigation/model/MenuStyle.kt | 34 ++++++++++++++++--- .../view/HorizontalMenuItemView.kt | 9 +++-- .../view/VerticalMenuItemView.kt | 3 +- .../res/layout/cnb_horizontal_menu_item.xml | 4 +-- .../res/layout/cnb_vertical_menu_item.xml | 4 +-- .../src/main/res/values/attrs.xml | 1 + .../src/main/res/values/colors.xml | 1 + 8 files changed, 45 insertions(+), 28 deletions(-) diff --git a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/ChipNavigationBar.kt b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/ChipNavigationBar.kt index 9c519e9..73b052c 100644 --- a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/ChipNavigationBar.kt +++ b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/ChipNavigationBar.kt @@ -51,22 +51,7 @@ class ChipNavigationBar @JvmOverloads constructor( else -> MenuOrientation.HORIZONTAL } - val textAppearance = a.getResourceId(R.styleable.ChipNavigationBar_cnb_textAppearance, -1) - val radius = a.getDimension(R.styleable.ChipNavigationBar_cnb_radius, Float.MAX_VALUE) - val badgeColor = a.getColor( - R.styleable.ChipNavigationBar_cnb_badgeColor, - ContextCompat.getColor(context, R.color.cnb_default_badge_color) - ) - val unselectedColor = a.getColor( - R.styleable.ChipNavigationBar_cnb_unselectedColor, - context.getValueFromAttr(R.attr.colorButtonNormal) - ) - menuStyle = MenuStyle( - badgeColor = badgeColor, - unselectedColor = unselectedColor, - radius = radius, - textAppearance = textAppearance.takeIf { it > 0 } - ) + menuStyle = MenuStyle(context, a) a.recycle() setMenuOrientation(orientation) diff --git a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/model/MenuStyle.kt b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/model/MenuStyle.kt index e8dd39e..00ec3a4 100644 --- a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/model/MenuStyle.kt +++ b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/model/MenuStyle.kt @@ -1,11 +1,35 @@ package com.ismaeldivita.chipnavigation.model +import android.content.Context +import android.content.res.TypedArray import androidx.annotation.ColorInt import androidx.annotation.IdRes +import androidx.core.content.ContextCompat +import com.ismaeldivita.chipnavigation.R -class MenuStyle( - @ColorInt val badgeColor: Int, - @ColorInt val unselectedColor: Int, - @IdRes val textAppearance: Int?, +class MenuStyle(context: Context, attr: TypedArray) { + + @ColorInt val badgeColor: Int + @ColorInt val unselectedColor: Int + @IdRes val textAppearance: Int? val radius: Float -) \ No newline at end of file + val iconSize: Int + + init { + textAppearance = attr.getResourceId(R.styleable.ChipNavigationBar_cnb_textAppearance, -1) + .takeIf { it > 0 } + + radius = attr.getDimension(R.styleable.ChipNavigationBar_cnb_radius, Float.MAX_VALUE) + badgeColor = attr.getColor(R.styleable.ChipNavigationBar_cnb_badgeColor, + ContextCompat.getColor(context, R.color.cnb_default_badge_color) + ) + unselectedColor = attr.getColor( + R.styleable.ChipNavigationBar_cnb_unselectedColor, + ContextCompat.getColor(context, R.color.cnb_default_unselected_color) + ) + iconSize = attr.getDimension( + R.styleable.ChipNavigationBar_cnb_iconSize, + context.resources.getDimension(R.dimen.cnb_icon_size) + ).toInt() + } +} \ No newline at end of file diff --git a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/HorizontalMenuItemView.kt b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/HorizontalMenuItemView.kt index 260aa49..1b4f0e3 100644 --- a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/HorizontalMenuItemView.kt +++ b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/HorizontalMenuItemView.kt @@ -7,6 +7,7 @@ import android.graphics.drawable.Drawable import android.graphics.drawable.GradientDrawable import android.util.AttributeSet import android.view.View +import android.view.ViewGroup import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import android.widget.LinearLayout import android.widget.LinearLayout.LayoutParams @@ -44,6 +45,8 @@ internal class HorizontalMenuItemView @JvmOverloads constructor( unselectedColor = item.menuStyle.unselectedColor ) + icon.layoutParams.width = item.menuStyle.iconSize + icon.layoutParams.height = item.menuStyle.iconSize icon.setImageResource(item.icon) icon.setBadgeColor(item.menuStyle.badgeColor) icon.setColorStateListAnimator( @@ -51,6 +54,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor( unselectedColor = item.menuStyle.unselectedColor, mode = item.tintMode ) + val containerBackground = GradientDrawable().apply { cornerRadius = item.menuStyle.radius setTint(item.backgroundColor) @@ -82,6 +86,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor( override fun setSelected(selected: Boolean) { super.setSelected(selected) val isPortrait = context.resources.configuration.orientation == ORIENTATION_PORTRAIT + val childCollapse = (parent as ViewGroup).childCount > 3 if (selected) { /** Hack to fix the ripple issue before a scene transition on SDKs < P */ @@ -90,7 +95,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor( container.visibility = View.VISIBLE beginDelayedTransitionOnParent() - if (isPortrait) { + if (isPortrait && childCollapse) { updateLayoutParams { width = WRAP_CONTENT @@ -99,7 +104,7 @@ internal class HorizontalMenuItemView @JvmOverloads constructor( } title.visibility = View.VISIBLE } else { - if (isPortrait) { + if (isPortrait && childCollapse) { updateLayoutParams { width = 0 weight = 1F diff --git a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/VerticalMenuItemView.kt b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/VerticalMenuItemView.kt index 20632f2..7569b73 100644 --- a/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/VerticalMenuItemView.kt +++ b/chip-navigation-bar/src/main/java/com/ismaeldivita/chipnavigation/view/VerticalMenuItemView.kt @@ -61,7 +61,8 @@ internal class VerticalMenuItemView @JvmOverloads constructor( color = item.textColor, unselectedColor = item.menuStyle.unselectedColor ) - + icon.layoutParams.width = item.menuStyle.iconSize + icon.layoutParams.height = item.menuStyle.iconSize icon.setBadgeColor(item.menuStyle.badgeColor) icon.setImageResource(item.icon) icon.setColorStateListAnimator( diff --git a/chip-navigation-bar/src/main/res/layout/cnb_horizontal_menu_item.xml b/chip-navigation-bar/src/main/res/layout/cnb_horizontal_menu_item.xml index 3eec35d..f55b552 100644 --- a/chip-navigation-bar/src/main/res/layout/cnb_horizontal_menu_item.xml +++ b/chip-navigation-bar/src/main/res/layout/cnb_horizontal_menu_item.xml @@ -19,8 +19,8 @@ diff --git a/chip-navigation-bar/src/main/res/layout/cnb_vertical_menu_item.xml b/chip-navigation-bar/src/main/res/layout/cnb_vertical_menu_item.xml index 8a61623..625c345 100644 --- a/chip-navigation-bar/src/main/res/layout/cnb_vertical_menu_item.xml +++ b/chip-navigation-bar/src/main/res/layout/cnb_vertical_menu_item.xml @@ -21,8 +21,8 @@ + diff --git a/chip-navigation-bar/src/main/res/values/colors.xml b/chip-navigation-bar/src/main/res/values/colors.xml index a9da5db..c33c880 100644 --- a/chip-navigation-bar/src/main/res/values/colors.xml +++ b/chip-navigation-bar/src/main/res/values/colors.xml @@ -1,5 +1,6 @@ #F44336 + #696969