/** * Initializes the ViewGroup and modifies it's default behavior by the passed attributes * * @param attrs the attributes used to modify default settings */ protected void init(AttributeSet attrs) { gestureDetector = new GestureDetector(getContext(), new MyGestureListener()); quadrantTouched = new boolean[] {false, false, false, false, false}; if (attrs != null) { TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.CircleLayout); speed = a.getInt(R.styleable.CircleLayout_speed, speed); radius = a.getDimension(R.styleable.CircleLayout_radius, radius); isRotating = a.getBoolean(R.styleable.CircleLayout_isRotating, isRotating); // The angle where the first menu item will be drawn angle = a.getInt(R.styleable.CircleLayout_firstChildPosition, (int) angle); for (FirstChildPosition pos : FirstChildPosition.values()) { if (pos.getAngle() == angle) { firstChildPosition = pos; break; } } a.recycle(); // Needed for the ViewGroup to be drawn setWillNotDraw(false); } }
public void setFirstChildPosition(FirstChildPosition firstChildPosition) { this.firstChildPosition = firstChildPosition; if (selectedView != null) { if (isRotating) { rotateViewToCenter(selectedView); } else { setAngle(firstChildPosition.getAngle()); } } }
/** * Rotates the given view to the firstChildPosition * * @param view the view to be rotated */ public void rotateViewToCenter(View view) { if (isRotating) { float viewAngle = view.getTag() != null ? (Float) view.getTag() : 0; float destAngle = firstChildPosition.getAngle() - viewAngle; if (destAngle < 0) { destAngle += 360; } if (destAngle > 180) { destAngle = -1 * (360 - destAngle); } animateTo(angle + destAngle, 7500L / speed); } }
private void setChildAngles() { int left, top, childWidth, childHeight, childCount = getChildCount(); float angleDelay = 360.0f / childCount; float halfAngle = angleDelay / 2; float localAngle = angle; for (int i = 0; i < childCount; i++) { final View child = getChildAt(i); if (child.getVisibility() == GONE) { continue; } if (localAngle > 360) { localAngle -= 360; } else if (localAngle < 0) { localAngle += 360; } childWidth = child.getMeasuredWidth(); childHeight = child.getMeasuredHeight(); left = Math.round( (float) (((circleWidth / 2.0) - childWidth / 2.0) + radius * Math.cos(Math.toRadians(localAngle)))); top = Math.round( (float) (((circleHeight / 2.0) - childHeight / 2.0) + radius * Math.sin(Math.toRadians(localAngle)))); child.setTag(localAngle); float distance = Math.abs(localAngle - firstChildPosition.getAngle()); boolean isFirstItem = distance <= halfAngle || distance >= (360 - halfAngle); if (isFirstItem && selectedView != child) { selectedView = child; if (onItemSelectedListener != null && isRotating) { onItemSelectedListener.onItemSelected(child); } } child.layout(left, top, left + childWidth, top + childHeight); localAngle += angleDelay; } }