From ca2931e81f2ff900180bc3dfc948ec75987a9a54 Mon Sep 17 00:00:00 2001 From: Balys Valentukevicius Date: Tue, 26 Aug 2014 17:08:00 +0100 Subject: [PATCH] Minor code refactoring --- .../material/drawable/menu/MaterialMenu.java | 2 +- .../drawable/menu/MaterialMenuDrawable.java | 43 ++++++++++++------- 2 files changed, 28 insertions(+), 17 deletions(-) diff --git a/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenu.java b/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenu.java index c018355..98bd3a7 100644 --- a/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenu.java +++ b/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenu.java @@ -76,5 +76,5 @@ public interface MaterialMenu { /** * @return {@link MaterialMenuDrawable} to be used for the menu */ - public abstract MaterialMenuDrawable getDrawable(); + public MaterialMenuDrawable getDrawable(); } diff --git a/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenuDrawable.java b/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenuDrawable.java index 778fffe..849fc93 100644 --- a/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenuDrawable.java +++ b/library/src/main/java/com/balysv/material/drawable/menu/MaterialMenuDrawable.java @@ -184,21 +184,26 @@ private void drawMiddleLine(Canvas canvas, float ratio) { switch (animationState) { case BURGER_ARROW: - // rotate by 180 and shorten one end - rotation = transformationValue * ARROW_MID_LINE_ANGLE; + // rotate by 180 + if (isMorphingForward()) { + rotation = ratio * ARROW_MID_LINE_ANGLE; + } else { + rotation = ARROW_MID_LINE_ANGLE + (1 - ratio) * ARROW_MID_LINE_ANGLE; + } + // shorten one end stopX -= ratio * strokeWidth / 2; break; case BURGER_X: // fade out - alpha = Math.max(0, Math.min(255, (int) ((1 - ratio) * 255))); + alpha = (int) ((1 - ratio) * 255); break; case ARROW_X: // fade out and shorten one end - alpha = Math.max(0, Math.min(255, (int) ((1 - ratio) * 255))); + alpha = (int) ((1 - ratio) * 255); startX += (1 - ratio) * strokeWidth / 2; break; case ARROW_CHECK: - if (transformationValue <= TRANSFORMATION_MID) { + if (isMorphingForward()) { // rotate until required angle rotation = ratio * CHECK_MIDDLE_ANGLE; // lengthen both ends @@ -214,6 +219,7 @@ private void drawMiddleLine(Canvas canvas, float ratio) { pivotX = width / 2 + strokeWidth * 2; break; case BURGER_CHECK: + // rotate until required angle rotation = ratio * CHECK_MIDDLE_ANGLE; // lengthen both ends startX += ratio * (dip4 + strokeWidth / 2); @@ -255,7 +261,7 @@ private void drawTopLine(Canvas canvas, float ratio) { switch (animationState) { case BURGER_ARROW: - if (transformationValue <= TRANSFORMATION_MID) { + if (isMorphingForward()) { // rotate until required angle rotation = ratio * ARROW_BOT_LINE_ANGLE; } else { @@ -298,7 +304,7 @@ private void drawTopLine(Canvas canvas, float ratio) { break; case ARROW_CHECK: // fade out - alpha = Math.max(0, Math.min(255, (int) ((1 - ratio) * 255))); + alpha = (int) ((1 - ratio) * 255); // retain starting arrow configuration rotation = ARROW_BOT_LINE_ANGLE; pivotX = width / 2; @@ -307,7 +313,8 @@ private void drawTopLine(Canvas canvas, float ratio) { stopX -= dip3; break; case BURGER_CHECK: - alpha = Math.max(0, Math.min(255, (int) ((1 - ratio) * 255))); + // fade out + alpha = (int) ((1 - ratio) * 255); break; case X_CHECK: // retain X configuration @@ -318,7 +325,7 @@ private void drawTopLine(Canvas canvas, float ratio) { stopX += dip3; // fade out - alpha = Math.max(0, Math.min(255, (int) ((1 - ratio) * 255))); + alpha = (int) ((1 - ratio) * 255); break; } @@ -346,7 +353,7 @@ private void drawBottomLine(Canvas canvas, float ratio) { switch (animationState) { case BURGER_ARROW: - if (transformationValue <= TRANSFORMATION_MID) { + if (isMorphingForward()) { // rotate to required angle rotation = ARROW_TOP_LINE_ANGLE * ratio; } else { @@ -362,7 +369,7 @@ private void drawBottomLine(Canvas canvas, float ratio) { stopX = width - sidePadding - dip3 * ratio; break; case BURGER_X: - if (transformationValue <= TRANSFORMATION_MID) { + if (isMorphingForward()) { // rotate around rotation2 = -X_ROTATION_ANGLE * ratio; } else { @@ -419,11 +426,11 @@ private void drawBottomLine(Canvas canvas, float ratio) { case X_CHECK: // rotate from X to CHECK angles rotation2 = -X_ROTATION_ANGLE * (1 - ratio); - rotation = X_BOT_LINE_ANGLE + ratio * (-X_BOT_LINE_ANGLE + CHECK_BOTTOM_ANGLE + ARROW_TOP_LINE_ANGLE); + rotation = X_BOT_LINE_ANGLE + (CHECK_BOTTOM_ANGLE + ARROW_TOP_LINE_ANGLE - X_BOT_LINE_ANGLE) * ratio; - // move pivot from X to CHECL - pivotX = sidePadding + dip4 + ratio * (-sidePadding - dip4 + width / 2 - strokeWidth); - pivotY = height - topPadding - dip3 + ratio * (-height + topPadding + dip3 + height / 2 - strokeWidth); + // move pivot from X to CHECK + pivotX = sidePadding + dip4 + (width / 2 - strokeWidth - sidePadding - dip4) * ratio; + pivotY = height - topPadding - dip3 + (topPadding + dip3 + height / 2 - strokeWidth - height) * ratio; // shorten both ends startX += dip3 - dip3 * (1 - ratio); @@ -451,6 +458,10 @@ private void drawGrid(Canvas canvas) { } } + private boolean isMorphingForward() { + return transformationValue <= TRANSFORMATION_MID; + } + @Override public void setAlpha(int alpha) { iconPaint.setAlpha(alpha); } @@ -510,7 +521,7 @@ public synchronized void setIconState(IconState iconState) { transformationValue = TRANSFORMATION_MID; break; case CHECK: - animationState = AnimationState.ARROW_CHECK; + animationState = AnimationState.BURGER_CHECK; transformationValue = TRANSFORMATION_MID; } currentIconState = iconState;