From e88a7217cfedab13afff1451d5489ae639491af8 Mon Sep 17 00:00:00 2001 From: Arman Chatikyan Date: Thu, 1 Sep 2016 22:35:43 +0400 Subject: [PATCH] Now we can change background color if space navigation already set up --- .../spacenavigationview/MainActivity.java | 5 ++ .../luseen/spacenavigation/BezierView.java | 29 ++++++- .../spacenavigation/SpaceNavigationView.java | 81 ++++++++++++++----- 3 files changed, 93 insertions(+), 22 deletions(-) diff --git a/Sample/src/main/java/com/luseen/spacenavigationview/MainActivity.java b/Sample/src/main/java/com/luseen/spacenavigationview/MainActivity.java index 212124d..1191af8 100644 --- a/Sample/src/main/java/com/luseen/spacenavigationview/MainActivity.java +++ b/Sample/src/main/java/com/luseen/spacenavigationview/MainActivity.java @@ -31,6 +31,8 @@ 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(); @@ -38,16 +40,19 @@ protected void onCreate(Bundle savedInstanceState) { 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); } }); diff --git a/spacelib/src/main/java/com/luseen/spacenavigation/BezierView.java b/spacelib/src/main/java/com/luseen/spacenavigation/BezierView.java index 29f2d7b..6fbc00d 100644 --- a/spacelib/src/main/java/com/luseen/spacenavigation/BezierView.java +++ b/spacelib/src/main/java/com/luseen/spacenavigation/BezierView.java @@ -16,6 +16,7 @@ */ package com.luseen.spacenavigation; +import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; @@ -23,6 +24,7 @@ import android.support.v4.content.ContextCompat; import android.widget.RelativeLayout; +@SuppressLint("ViewConstructor") class BezierView extends RelativeLayout { private Paint paint; @@ -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); @@ -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 */ @@ -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(); + } } diff --git a/spacelib/src/main/java/com/luseen/spacenavigation/SpaceNavigationView.java b/spacelib/src/main/java/com/luseen/spacenavigation/SpaceNavigationView.java index 69f30b4..ab36cef 100644 --- a/spacelib/src/main/java/com/luseen/spacenavigation/SpaceNavigationView.java +++ b/spacelib/src/main/java/com/luseen/spacenavigation/SpaceNavigationView.java @@ -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 @@ -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; @@ -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 @@ -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); @@ -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 @@ -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(); } }); } @@ -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() { @@ -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); + } } } @@ -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); @@ -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) { @@ -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; + } } /** @@ -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); + } }