Skip to content

Commit

Permalink
Fixed bug #6 (graphic glitches while expanding menu first time)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImangazalievM committed Sep 12, 2017
1 parent 2a2b688 commit 35f01fd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 90 deletions.
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
apply plugin: 'com.android.library'
apply plugin: 'com.novoda.bintray-release'

final LIBRARY_VERSION_NAME = '1.0.3'
final LIBRARY_VERSION_CODE = 2
final LIBRARY_VERSION_NAME = '1.0.4'
final LIBRARY_VERSION_CODE = 3

android {
compileSdkVersion 25
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,11 @@ protected void onLayout(boolean changed, int l, int t, int r, int b) {

int centerButtonWidth = centerButton.getMeasuredWidth();
int centerButtonHeight = centerButton.getMeasuredHeight();
int centerButtonLeft = Math.round((float) ((circleWidth / 2.0) - centerButtonWidth / 2.0));
int centerButtonTop = Math.round((float) ((circleHeight / 2.0) - centerButtonHeight / 2.0));
centerButton.layout(centerButtonLeft, centerButtonTop, centerButtonLeft + centerButtonWidth, centerButtonTop + centerButtonHeight);
int centerButtonX = Math.round((float) ((circleWidth / 2.0) - centerButtonWidth / 2.0));
int centerButtonY = Math.round((float) ((circleHeight / 2.0) - centerButtonHeight / 2.0));
centerButton.layout(centerButtonX, centerButtonY, centerButtonX + centerButtonWidth, centerButtonY + centerButtonHeight);

menuController.calculateButtonsVertices(radius, circleStartAngle, circleWidth, circleHeight, centerButtonLeft, centerButtonTop);
menuController.calculateButtonsVertices(radius, circleStartAngle, circleWidth, circleHeight);
itemSelectionAnimator.setCircleRadius(radius, circleWidth, circleHeight);
}

Expand Down Expand Up @@ -247,7 +247,6 @@ public void toggle() {
menuController.toggle();
}


public boolean isExpanded() {
return menuController.isExpanded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@

public class MenuButtonPoint {

public int x;
public int y;
public int collapsedX;
public int collapsedY;
public int expandedX;
public int expandedY;
public float angle;

public MenuButtonPoint(int x, int y, float angle) {
this.x = x;
this.y = y;
public MenuButtonPoint(int collapsedX, int collapsedY, int expandedX, int expandedY, float angle) {
this.collapsedX = collapsedX;
this.collapsedY = collapsedY;
this.expandedX = expandedX;
this.expandedY = expandedY;
this.angle = angle;
}

Expand Down
125 changes: 47 additions & 78 deletions library/src/main/java/com/imangazaliev/circlemenu/MenuController.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,101 +37,79 @@ public interface ControllerListener {

private List<CircleMenuButton> buttons = new ArrayList<>();
private HashMap<CircleMenuButton, MenuButtonPoint> buttonsPositions = new HashMap<>();

private int centerPositionX, centerPositionY;
private final ControllerListener listener;
private View.OnClickListener onButtonItemClickListener;

@MenuState
private int state;

private final ControllerListener listener;


public MenuController(ControllerListener listener) {
this.listener = listener;
this.state = MenuState.COLLAPSED;
this.onButtonItemClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClick((CircleMenuButton) v);
}
};
}

public void calculateButtonsVertices(float radius, int startAngle, int circleWidth, int circleHeight, int centerX, int centerY) {
int left, top, childWidth, childHeight;
public void calculateButtonsVertices(float radius, int startAngle, int circleWidth, int circleHeight) {
int collapsedX, collapsedY, expandedX, expandedY;
int buttonWidth, buttonHeight;
int childCount = getButtonsCount();

float angleStep = 360.0f / (childCount);
float lastAngle = startAngle;

centerPositionX = centerX;
centerPositionY = centerY;

for (int i = 0; i < childCount; i++) {
final CircleMenuButton button = buttons.get(i);

if (button.getVisibility() == View.GONE) {
continue;
}

childWidth = button.getMeasuredWidth();
childHeight = button.getMeasuredHeight();
buttonWidth = button.getMeasuredWidth();
buttonHeight = button.getMeasuredHeight();

expandedX = Math.round((float) (((circleWidth / 2.0) - buttonWidth / 2.0) + radius * Math.cos(Math.toRadians(lastAngle))));
expandedY = Math.round((float) (((circleHeight / 2.0) - buttonHeight / 2.0) + radius * Math.sin(Math.toRadians(lastAngle))));
collapsedX = Math.round((float) ((circleWidth / 2.0) - buttonWidth / 2.0));
collapsedY = Math.round((float) ((circleHeight / 2.0) - buttonHeight / 2.0));

final float currentAngle = lastAngle;
left = Math.round((float) (((circleWidth / 2.0) - childWidth / 2.0) + radius * Math.cos(Math.toRadians(currentAngle))));
top = Math.round((float) (((circleHeight / 2.0) - childHeight / 2.0) + radius * Math.sin(Math.toRadians(currentAngle))));
MenuButtonPoint menuButtonPoint = new MenuButtonPoint(collapsedX, collapsedY, expandedX, expandedY, lastAngle);
buttonsPositions.put(button, menuButtonPoint);

if (currentAngle > 360) {
if (lastAngle > 360) {
lastAngle -= 360;
} else if (currentAngle < 0) {
} else if (lastAngle < 0) {
lastAngle += 360;
}
buttonsPositions.put(button, new MenuButtonPoint(left, top, currentAngle));

if (state == MenuState.COLLAPSED) {
left = Math.round((float) ((circleWidth / 2.0) - childWidth / 2.0));
top = Math.round((float) ((circleHeight / 2.0) - childHeight / 2.0));
button.layout(left, top, left + childWidth, top + childHeight);
} else {
button.layout(left, top, left + childWidth, top + childHeight);
}

button.layout(collapsedX, collapsedY, collapsedX + buttonWidth, collapsedY + buttonHeight);

lastAngle += angleStep;
}
}

public void toggle() {
if (isExpanded()) {
collapse();
} else {
expand();
}
}

private void collapse() {
public void toggle() {
if (isExpanded()) {
startCollapseAnimation();
}
}

private void expand() {
if (!isExpanded()) {
} else {
startExpandAnimation();
}
}

private void startCollapseAnimation() {
setButtonsStartPosition(false);
setState(MenuState.COLLAPSE_ANIMATION_STARTED);

for (int i = 0; i < getButtonsCount(); i++) {
final CircleMenuButton button = buttons.get(i);
for (final CircleMenuButton button : buttons) {
MenuButtonPoint buttonPosition = buttonsPositions.get(button);

float startPositionX = buttonPosition.x;
float endPositionX = centerPositionX;

float startPositionY = buttonPosition.y;
float endPositionY = centerPositionY;

button.setX(startPositionX);
button.setY(startPositionY);

ValueAnimator buttonAnimatorX = ValueAnimator.ofFloat(startPositionX, endPositionX);
//x axis animation
ValueAnimator buttonAnimatorX = ValueAnimator.ofFloat(buttonPosition.expandedX, buttonPosition.collapsedX);
buttonAnimatorX.setInterpolator(new DecelerateInterpolator());
buttonAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
Expand All @@ -144,14 +122,13 @@ public void onAnimationUpdate(ValueAnimator animation) {
buttonAnimatorX.setStartDelay(TOGGLE_ANIMATION_DELAY);
buttonAnimatorX.start();


ValueAnimator buttonAnimatorY = ValueAnimator.ofFloat(startPositionY, endPositionY);
//y axis animation
ValueAnimator buttonAnimatorY = ValueAnimator.ofFloat(buttonPosition.expandedY, buttonPosition.collapsedY);
buttonAnimatorY.setInterpolator(new DecelerateInterpolator());
buttonAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (float) animation.getAnimatedValue();
button.setY(animatedValue - button.getLayoutParams().height / 2);
button.setY((float) animation.getAnimatedValue() - button.getLayoutParams().height / 2);
button.requestLayout();
}
});
Expand All @@ -169,24 +146,14 @@ public void run() {
}

private void startExpandAnimation() {
setButtonsStartPosition(true);
setState(MenuState.EXPAND_ANIMATION_STARTED);

showItems();
for (int i = 0; i < getButtonsCount(); i++) {
final CircleMenuButton button = buttons.get(i);
for (final CircleMenuButton button : buttons) {
MenuButtonPoint buttonPosition = buttonsPositions.get(button);

float startPositionX = centerPositionX;
float endPositionX = buttonPosition.x;

float startPositionY = centerPositionY;
float endPositionY = buttonPosition.y;

button.setX(startPositionX);
button.setY(startPositionY);

//x axis animation
ValueAnimator buttonAnimatorX = ValueAnimator.ofFloat(startPositionX, endPositionX);
ValueAnimator buttonAnimatorX = ValueAnimator.ofFloat(buttonPosition.collapsedX, buttonPosition.expandedX);
buttonAnimatorX.setInterpolator(new DecelerateInterpolator());
buttonAnimatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
Expand All @@ -200,13 +167,12 @@ public void onAnimationUpdate(ValueAnimator animation) {
buttonAnimatorX.start();

//y axis animation
ValueAnimator buttonAnimatorY = ValueAnimator.ofFloat(startPositionY, endPositionY);
ValueAnimator buttonAnimatorY = ValueAnimator.ofFloat(buttonPosition.collapsedY, buttonPosition.expandedY);
buttonAnimatorY.setInterpolator(new DecelerateInterpolator());
buttonAnimatorY.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float animatedValue = (float) animation.getAnimatedValue();
button.setY(animatedValue - button.getLayoutParams().height / 2);
button.setY((float) animation.getAnimatedValue() - button.getLayoutParams().height / 2);
button.requestLayout();
}
});
Expand All @@ -223,11 +189,20 @@ public void run() {
}, TOGGLE_ANIMATION_DURATION);
}

private void setButtonsStartPosition(boolean collapsed) {
for (final CircleMenuButton button : buttons) {
MenuButtonPoint buttonPosition = buttonsPositions.get(button);
button.setX(collapsed ? buttonPosition.collapsedX : buttonPosition.expandedX);
button.setY(collapsed ? buttonPosition.collapsedY : buttonPosition.expandedY);
}
}

public void setState(@MenuState int state) {
this.state = state;
switch (state) {
case MenuState.EXPAND_ANIMATION_STARTED:
disableItems();
showItems();
listener.onStartExpanding();
break;
case MenuState.EXPANDED:
Expand Down Expand Up @@ -265,12 +240,7 @@ public boolean isExpanded() {

public void addButton(final CircleMenuButton menuButton) {
buttons.add(menuButton);
menuButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onItemClick(menuButton);
}
});
menuButton.setOnClickListener(onButtonItemClickListener);
}

private void onItemClick(CircleMenuButton menuButton) {
Expand Down Expand Up @@ -311,5 +281,4 @@ public MenuButtonPoint getButtonsPoint(CircleMenuButton menuButton) {
return buttonsPositions.get(menuButton);
}


}

0 comments on commit 35f01fd

Please sign in to comment.