Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Border Line 、Radius #254

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Library to implement the Bottom Navigation component from Material Design guidel

## Demo
<img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo1.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo2.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo3.gif" width="208" height="368" /> <img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo4.gif" width="208" height="368" />

<img src="https://raw.githubusercontent.com/aurelhubert/ahbottomnavigation/master/demo5.gif" width="208" height="368" />
## What's new (2.0.6) - [Changelog](https://github.com/aurelhubert/ahbottomnavigation/blob/master/CHANGELOG.md)
* Fix selected item background for API >= 21
* Fix `isHidden()` method
Expand Down Expand Up @@ -124,6 +124,12 @@ bottomNavigation.setOnNavigationPositionListener(new AHBottomNavigation.OnNaviga
// Manage the new y position
}
});

// Set BottomBorder Line Or Radius
bottomNavigation.setNavigationBorderLine(iColor,iRadius,rShapePading);
// OR use default border line style: #a1a1a1 iRadius=0 new Rect(0,2,0,0)
bottomNavigation.setNavigationBorderLine();

```

### With XML menu
Expand Down
4 changes: 2 additions & 2 deletions ahbottomnavigation/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ ext {

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'

defaultConfig {
minSdkVersion 14
Expand All @@ -43,7 +43,7 @@ android {
}

dependencies {
compile 'com.android.support:design:25.2.0'
compile 'com.android.support:design:25.3.1'
}

// Place it at the end of the file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcelable;
Expand Down Expand Up @@ -117,6 +120,10 @@ public enum TitleState {
private int notificationActiveMarginLeft, notificationInactiveMarginLeft;
private int notificationActiveMarginTop, notificationInactiveMarginTop;

// bottom line
private @ColorInt int bottomNavigationBorderLineColor;
private @ColorInt int bottomNavigationBorderLineRadius=0;
private Rect bottomNavigationShapePading = new Rect(0,0,0,0);
/**
* Constructors
*/
Expand Down Expand Up @@ -219,6 +226,10 @@ private void init(Context context, AttributeSet attrs) {
notificationActiveMarginTop = (int) resources.getDimension(R.dimen.bottom_navigation_notification_margin_top_active);
notificationInactiveMarginTop = (int) resources.getDimension(R.dimen.bottom_navigation_notification_margin_top);

// bottom line
bottomNavigationBorderLineColor = ContextCompat.getColor(context, R.color.colorBottomNavigationShapLine);
bottomNavigationBorderLineRadius = 0;

ViewCompat.setElevation(this, resources.getDimension(R.dimen.bottom_navigation_elevation));
setClipToPadding(false);

Expand Down Expand Up @@ -272,6 +283,35 @@ public void run() {
});
}

public void setNavigationBorderLine(){
// bottomNavigationLineColor = R.color.colorBottomNavigationNotification;
setNavigationBorderLine(getResources().getColor(R.color.colorBottomNavigationShapLine),0,new Rect(0,2,0,0));
}
public void setNavigationBorderLine(@ColorInt int iColor,int iRadius,Rect rShapePading){
bottomNavigationBorderLineColor = iColor;
bottomNavigationBorderLineRadius = iRadius;
bottomNavigationShapePading.set(rShapePading);
// createItems();
setLayerDrawable(defaultBackgroundColor);
}

private void setLayerDrawable(@ColorInt int bgcolor){
GradientDrawable whiteDrawable = getGradientDrawable(getContext(), bgcolor, bottomNavigationBorderLineRadius);//getResources().getColor(android.R.color.transparent)
GradientDrawable grayDrawable = getGradientDrawable(getContext(), bottomNavigationBorderLineColor, bottomNavigationBorderLineRadius);
LayerDrawable layerDrawable = new LayerDrawable(new Drawable[]{grayDrawable, whiteDrawable});
//设置padding
layerDrawable.setLayerInset(0, 0, 0, 0, 0);
layerDrawable.setLayerInset(1, bottomNavigationShapePading.left, bottomNavigationShapePading.top, bottomNavigationShapePading.right, bottomNavigationShapePading.bottom);

ViewCompat.setBackground(this,layerDrawable);
}
public static GradientDrawable getGradientDrawable(Context context,@ColorInt int colID, int dp) {
GradientDrawable drawable = new GradientDrawable();
drawable.setColor(colID);
drawable.setCornerRadius(dp);
return drawable;
}

@SuppressLint("NewApi")
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private int calculateHeight(int layoutHeight) {
Expand Down Expand Up @@ -423,13 +463,15 @@ private void createClassicItems(LinearLayout linearLayout) {
if (colored) {
if (current) {
setBackgroundColor(item.getColor(context));
setLayerDrawable(item.getColor(context));
currentColor = item.getColor(context);
}
} else {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
// setBackgroundColor(defaultBackgroundColor);
setLayerDrawable(defaultBackgroundColor);
}
}

Expand Down Expand Up @@ -540,14 +582,16 @@ private void createSmallItems(LinearLayout linearLayout) {

if (colored) {
if (i == currentItem) {
setBackgroundColor(item.getColor(context));
// setBackgroundColor(item.getColor(context));
setLayerDrawable(item.getColor(context));
currentColor = item.getColor(context);
}
} else {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
// setBackgroundColor(defaultBackgroundColor);
setLayerDrawable(defaultBackgroundColor);
}
}

Expand Down Expand Up @@ -640,9 +684,9 @@ private void updateItems(final int itemIndex, boolean useCallback) {
int cy = view.getHeight() / 2;

if (circleRevealAnim != null && circleRevealAnim.isRunning()) {
circleRevealAnim.cancel();
setBackgroundColor(items.get(itemIndex).getColor(context));
circleRevealAnim.cancel();
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
setLayerDrawable(items.get(itemIndex).getColor(context));
}

circleRevealAnim = ViewAnimationUtils.createCircularReveal(backgroundColorView, cx, cy, 0, finalRadius);
Expand All @@ -654,9 +698,9 @@ public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
setBackgroundColor(items.get(itemIndex).getColor(context));
public void onAnimationEnd(Animator animation) {
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
setLayerDrawable(items.get(itemIndex).getColor(context));
}

@Override
Expand All @@ -670,12 +714,17 @@ public void onAnimationRepeat(Animator animation) {
circleRevealAnim.start();
} else if (colored) {
AHHelper.updateViewBackgroundColor(this, currentColor,
items.get(itemIndex).getColor(context));
items.get(itemIndex).getColor(context), new AHHelper.AHBgAnimation() {
@Override
public void backGroundColorAnimation(int color) {
setLayerDrawable(color);
}
});
} else {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
} else {
setLayerDrawable(defaultBackgroundColor);
}
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}
Expand All @@ -702,8 +751,8 @@ public void onAnimationRepeat(Animator animation) {
} else if (currentItem == CURRENT_ITEM_NONE) {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
} else {
setLayerDrawable(defaultBackgroundColor);
}
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}
Expand Down Expand Up @@ -766,8 +815,8 @@ private void updateSmallItems(final int itemIndex, boolean useCallback) {
int cy = views.get(itemIndex).getHeight() / 2;

if (circleRevealAnim != null && circleRevealAnim.isRunning()) {
circleRevealAnim.cancel();
setBackgroundColor(items.get(itemIndex).getColor(context));
circleRevealAnim.cancel();
setLayerDrawable(items.get(itemIndex).getColor(context));
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}

Expand All @@ -780,8 +829,8 @@ public void onAnimationStart(Animator animation) {
}

@Override
public void onAnimationEnd(Animator animation) {
setBackgroundColor(items.get(itemIndex).getColor(context));
public void onAnimationEnd(Animator animation) {
setLayerDrawable(items.get(itemIndex).getColor(context));
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}

Expand All @@ -796,12 +845,17 @@ public void onAnimationRepeat(Animator animation) {
circleRevealAnim.start();
} else if (colored) {
AHHelper.updateViewBackgroundColor(this, currentColor,
items.get(itemIndex).getColor(context));
items.get(itemIndex).getColor(context), new AHHelper.AHBgAnimation() {
@Override
public void backGroundColorAnimation(int color) {
setLayerDrawable(color);
}
});
} else {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
} else {
setLayerDrawable(defaultBackgroundColor);
}
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}
Expand Down Expand Up @@ -835,8 +889,8 @@ public void onAnimationRepeat(Animator animation) {
} else if (currentItem == CURRENT_ITEM_NONE) {
if (defaultBackgroundResource != 0) {
setBackgroundResource(defaultBackgroundResource);
} else {
setBackgroundColor(defaultBackgroundColor);
} else {
setLayerDrawable(defaultBackgroundColor);
}
backgroundColorView.setBackgroundColor(Color.TRANSPARENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@
*/
public class AHHelper {


public interface AHBgAnimation{
public void backGroundColorAnimation(@ColorInt int color);
}


/**
* Return a tint drawable
*
Expand Down Expand Up @@ -168,6 +174,21 @@ public void onAnimationUpdate(ValueAnimator animator) {
colorAnimation.start();
}

public static void updateViewBackgroundColor(final View view, @ColorInt int fromColor,
@ColorInt int toColor ,final AHBgAnimation ahBganimal) {
ValueAnimator colorAnimation = ValueAnimator.ofObject(new ArgbEvaluator(), fromColor, toColor);
colorAnimation.setDuration(150);
colorAnimation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animator) {
if(ahBganimal != null){
ahBganimal.backGroundColorAnimation((Integer) animator.getAnimatedValue());
}
}
});
colorAnimation.start();
}

/**
* Update image view color with animation
*/
Expand Down
1 change: 1 addition & 0 deletions ahbottomnavigation/src/main/res/values/colors.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
<color name="colorBottomNavigationInactiveColored">#50FFFFFF</color>
<color name="colorBottomNavigationSelectedBackground">#17000000</color>
<color name="colorBottomNavigationNotification">#F63D2B</color>
<color name="colorBottomNavigationShapLine">#d8d8d8</color>
</resources>
1 change: 0 additions & 1 deletion ahbottomnavigation/src/main/res/values/dimens.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,4 @@
<dimen name="bottom_navigation_notification_margin_top">14dp</dimen>
<dimen name="bottom_navigation_notification_margin_top_active">3dp</dimen>
<dimen name="bottom_navigation_notification_text_size">9sp</dimen>

</resources>
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ buildscript {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.android.tools.build:gradle:2.4.+'
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
classpath 'com.github.dcendents:android-maven-gradle-plugin:1.4.1'
}
Expand Down
8 changes: 4 additions & 4 deletions demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ apply plugin: 'com.android.application'

android {
compileSdkVersion 25
buildToolsVersion '25.0.2'
buildToolsVersion '25.0.3'

defaultConfig {
applicationId "com.aurelhubert.ahbottomnavigation.demo"
Expand All @@ -24,8 +24,8 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:25.0.0'
compile 'com.android.support:cardview-v7:25.0.0'
compile 'com.android.support:design:25.0.0'
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile project(':ahbottomnavigation')
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import android.animation.Animator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.ColorInt;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.content.ContextCompat;
Expand Down Expand Up @@ -283,6 +285,14 @@ public void setForceTitleHide(boolean forceTitleHide) {
bottomNavigation.setTitleState(state);
}

/**
* Show BorderLine
* default setBottomLine()
*/
public void setNavigationBorderLine(@ColorInt int iColor, int iRadius, Rect rShapePading) {
bottomNavigation.setNavigationBorderLine(iColor,iRadius,rShapePading);
}

/**
* Reload activity
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.aurelhubert.ahbottomnavigation.demo;

import android.content.Context;
import android.graphics.Rect;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -64,6 +65,7 @@ private void initDemoSettings(View view) {
final SwitchCompat showSelectedBackground = (SwitchCompat) view.findViewById(R.id.fragment_demo_selected_background);
final SwitchCompat switchForceTitleHide = (SwitchCompat) view.findViewById(R.id.fragment_demo_force_title_hide);
final SwitchCompat switchTranslucentNavigation = (SwitchCompat) view.findViewById(R.id.fragment_demo_translucent_navigation);
final SwitchCompat showNavigationBottomLine = (SwitchCompat) view.findViewById(R.id.fragment_demo_navigation_bottom_line);

switchColored.setChecked(demoActivity.isBottomNavigationColored());
switchFiveItems.setChecked(demoActivity.getBottomNavigationNbItems() == 5);
Expand Down Expand Up @@ -108,13 +110,15 @@ public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
demoActivity.updateSelectedBackgroundVisibility(isChecked);
}
});
switchForceTitleHide.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
showNavigationBottomLine.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
demoActivity.setForceTitleHide(isChecked);
bShowHideBorderLine =! bShowHideBorderLine;
demoActivity.setNavigationBorderLine(getResources().getColor(R.color.color_tab_5),bShowHideBorderLine?5:0,new Rect(0,bShowHideBorderLine?2:0,0,0));
}
});
}
private boolean bShowHideBorderLine=false;

/**
* Init the fragment
Expand Down
Loading