Skip to content

Commit

Permalink
Now we can change background color if space navigation already set up
Browse files Browse the repository at this point in the history
  • Loading branch information
armcha committed Sep 1, 2016
1 parent 952730d commit e88a721
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,28 @@ protected void onCreate(Bundle savedInstanceState) {
spaceNavigationView.initWithSaveInstanceState(savedInstanceState);
spaceNavigationView.addSpaceItem(new SpaceItem("HOME", R.drawable.home));
spaceNavigationView.addSpaceItem(new SpaceItem("SEARCH", R.drawable.magnify));


//spaceNavigationView.addSpaceItem(new SpaceItem("LIKE", R.drawable.bell));
//spaceNavigationView.addSpaceItem(new SpaceItem("ACCOUNT", R.drawable.account));
//spaceNavigationView.showIconOnly();

spaceNavigationView.setSpaceOnClickListener(new SpaceOnClickListener() {
@Override
public void onCentreButtonClick() {
spaceNavigationView.changeSpaceBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.colorAccent));
Log.d("onCentreButtonClick ", "onCentreButtonClick");
}

@Override
public void onItemClick(int itemIndex, String itemName) {
spaceNavigationView.changeCenterButtonIcon(R.drawable.magnify);
Log.d("onItemClick ", "" + itemIndex + " " + itemName);
}

@Override
public void onItemReselected(int itemIndex, String itemName) {
spaceNavigationView.changeSpaceBackgroundColor(ContextCompat.getColor(MainActivity.this,R.color.colorPrimary));
Log.d("onItemReselected ", "" + itemIndex + " " + itemName);
}
});
Expand Down
29 changes: 27 additions & 2 deletions spacelib/src/main/java/com/luseen/spacenavigation/BezierView.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
*/
package com.luseen.spacenavigation;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.support.v4.content.ContextCompat;
import android.widget.RelativeLayout;

@SuppressLint("ViewConstructor")
class BezierView extends RelativeLayout {

private Paint paint;
Expand All @@ -31,15 +33,17 @@ class BezierView extends RelativeLayout {

private int bezierWidth, bezierHeight;

private int backgroundColor;

private Context context;


public BezierView(Context context, int backgroundColor) {
BezierView(Context context, int backgroundColor) {
super(context);
this.context = context;
this.backgroundColor = backgroundColor;
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
path = new Path();
paint.setColor(backgroundColor);
paint.setStrokeWidth(0);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
Expand All @@ -54,6 +58,11 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
@Override
protected void onDraw(Canvas canvas) {

/**
* Set paint color to fill view
*/
paint.setColor(backgroundColor);

/**
* Reset path before drawing
*/
Expand All @@ -80,9 +89,25 @@ protected void onDraw(Canvas canvas) {
canvas.drawPath(path, paint);
}

/**
* Build bezier view with given width and height
*
* @param bezierWidth Given width
* @param bezierHeight Given height
*/
void build(int bezierWidth, int bezierHeight) {
this.bezierWidth = bezierWidth;
this.bezierHeight = bezierHeight;
}

/**
* Change bezier view background color
*
* @param backgroundColor Target color
*/
void changeBackgroundColor(int backgroundColor) {
this.backgroundColor = backgroundColor;
invalidate();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ public class SpaceNavigationView extends RelativeLayout {

private static final String CHANGED_ICON_AND_TEXT_BUNDLE_KEY = "changedIconAndText";

private static final String CENTRE_BUTTON_KEY = "centreButtonKey";
private static final String CENTRE_BUTTON_ICON_KEY = "centreButtonIconKey";

private static final String CENTRE_BUTTON_COLOR_KEY = "centreButtonColorKey";

private static final String SPACE_BACKGROUND_COLOR_KEY = "backgroundColorKey";

private static final int NOT_DEFINED = -777; //random number, not - 1 because it is Color.WHITE

Expand All @@ -77,6 +81,12 @@ public class SpaceNavigationView extends RelativeLayout {

private FloatingActionButton fab;

private RelativeLayout centreBackgroundView;

private LinearLayout leftContent, rightContent;

private BezierView centreContent;

private Typeface customFont;

private Context context;
Expand Down Expand Up @@ -241,7 +251,7 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
/**
* Redraw main view to make subviews visible
*/
postRequestLayout(this);
postRequestLayout();
}

//private methods
Expand All @@ -252,12 +262,12 @@ protected void onSizeChanged(int width, int height, int oldWidth, int oldHeight)
private void initAndAddViewsToMainView() {

RelativeLayout mainContent = new RelativeLayout(context);
RelativeLayout centreBackgroundView = new RelativeLayout(context);
centreBackgroundView = new RelativeLayout(context);

LinearLayout leftContent = new LinearLayout(context);
LinearLayout rightContent = new LinearLayout(context);
leftContent = new LinearLayout(context);
rightContent = new LinearLayout(context);

BezierView centreContent = buildBezierView();
centreContent = buildBezierView();

fab = new FloatingActionButton(context);
fab.setSize(FloatingActionButton.SIZE_NORMAL);
Expand Down Expand Up @@ -324,9 +334,7 @@ public boolean onLongClick(View v) {
/**
* Adding views background colors
*/
leftContent.setBackgroundColor(spaceBackgroundColor);
rightContent.setBackgroundColor(spaceBackgroundColor);
centreBackgroundView.setBackgroundColor(spaceBackgroundColor);
setBackgroundColors();

/**
* Adding view to centreContent
Expand Down Expand Up @@ -538,14 +546,23 @@ private void updateSpaceItems(final int selectedIndex) {
currentSelectedItem = selectedIndex;
}

/**
* Set views background colors
*/
private void setBackgroundColors() {
rightContent.setBackgroundColor(spaceBackgroundColor);
centreBackgroundView.setBackgroundColor(spaceBackgroundColor);
leftContent.setBackgroundColor(spaceBackgroundColor);
}

/**
* Indicate event queue that we have changed the View hierarchy during a layout pass
*/
private void postRequestLayout(final ViewGroup viewGroup) {
viewGroup.getHandler().post(new Runnable() {
private void postRequestLayout() {
SpaceNavigationView.this.getHandler().post(new Runnable() {
@Override
public void run() {
viewGroup.requestLayout();
SpaceNavigationView.this.requestLayout();
}
});
}
Expand Down Expand Up @@ -578,7 +595,7 @@ private void restoreBadges() {
}

/**
* Restore changed icons and texts from saveInstance
* Restore changed icons,colors and texts from saveInstance
*/
@SuppressWarnings("unchecked")
private void restoreChangedIconsAndTexts() {
Expand All @@ -596,10 +613,15 @@ private void restoreChangedIconsAndTexts() {
}
}

if (restoredBundle.containsKey(CENTRE_BUTTON_KEY)) {
centreButtonIcon = restoredBundle.getInt(CENTRE_BUTTON_KEY);
if (restoredBundle.containsKey(CENTRE_BUTTON_ICON_KEY)) {
centreButtonIcon = restoredBundle.getInt(CENTRE_BUTTON_ICON_KEY);
fab.setImageResource(centreButtonIcon);
}

if (restoredBundle.containsKey(SPACE_BACKGROUND_COLOR_KEY)) {
int backgroundColor = restoredBundle.getInt(SPACE_BACKGROUND_COLOR_KEY);
changeSpaceBackgroundColor(backgroundColor);
}
}
}

Expand Down Expand Up @@ -643,7 +665,8 @@ public void initWithSaveInstanceState(Bundle savedInstanceState) {
*/
public void onSaveInstanceState(Bundle outState) {
outState.putInt(CURRENT_SELECTED_ITEM_BUNDLE_KEY, currentSelectedItem);
outState.putInt(CENTRE_BUTTON_KEY, centreButtonIcon);
outState.putInt(CENTRE_BUTTON_ICON_KEY, centreButtonIcon);
outState.putInt(SPACE_BACKGROUND_COLOR_KEY, spaceBackgroundColor);

if (badgeSaveInstanceHashMap.size() > 0)
outState.putSerializable(BUDGES_ITEM_BUNDLE_KEY, badgeSaveInstanceHashMap);
Expand Down Expand Up @@ -766,6 +789,7 @@ public void setSpaceOnClickListener(SpaceOnClickListener spaceOnClickListener) {

/**
* Set space item and centre button long click
*
* @param spaceOnLongClickListener space long click listener
*/
public void setSpaceOnLongClickListener(SpaceOnLongClickListener spaceOnLongClickListener) {
Expand Down Expand Up @@ -869,8 +893,13 @@ public void setFont(Typeface customFont) {
* @param icon Target icon to change
*/
public void changeCenterButtonIcon(int icon) {
fab.setImageResource(icon);
centreButtonIcon = icon;
if (fab == null) {
Log.e(TAG, "You should call setCentreButtonIcon() instead, " +
"changeCenterButtonIcon works if space navigation already set up");
} else {
fab.setImageResource(icon);
centreButtonIcon = icon;
}
}

/**
Expand Down Expand Up @@ -909,9 +938,21 @@ public void changeItemTextAtPosition(int itemIndex, String newText) {
spaceItem.setItemName(newText);
changedItemAndIconHashMap.put(itemIndex, spaceItem);
}


}

/**
* Change space background color if space view already set up
*
* @param color Target color to change
*/
public void changeSpaceBackgroundColor(@ColorInt int color) {
if (color == spaceBackgroundColor) {
Log.d(TAG, "changeSpaceBackgroundColor: color already changed");
return;
}

spaceBackgroundColor = color;
setBackgroundColors();
centreContent.changeBackgroundColor(color);
}
}

0 comments on commit e88a721

Please sign in to comment.