Advertisement
Guest User

Untitled

a guest
Apr 18th, 2014
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. public class CircleView extends RelativeLayout {
  2. static final int centerId = 111;
  3. private final int radius;
  4. private RelativeLayout.LayoutParams createNewRelativeLayoutParams() {
  5. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
  6. RelativeLayout.LayoutParams.WRAP_CONTENT,
  7. RelativeLayout.LayoutParams.WRAP_CONTENT);
  8. lp.addRule(RelativeLayout.ABOVE, centerId);
  9. lp.addRule(RIGHT_OF, centerId);
  10. return lp;
  11. }
  12. private View prepareElementForCircle(View elem, int distX, int distY) {
  13. RelativeLayout.LayoutParams lp = createNewRelativeLayoutParams();
  14. elem.measure(0, 0);
  15. int deltaX = elem.getMeasuredWidth() / 2;
  16. int deltaY = elem.getMeasuredHeight() / 2;
  17. lp.setMargins(distX - deltaX, 0, 0, radius - distY - deltaY);
  18. elem.setLayoutParams(lp);
  19. return elem;
  20. }
  21. public CircleView(Context context, int radius, View[] elements) {
  22. super(context);
  23. this.radius = radius;
  24. RelativeLayout.LayoutParams lpView = new RelativeLayout.LayoutParams(
  25. RelativeLayout.LayoutParams.MATCH_PARENT,
  26. RelativeLayout.LayoutParams.MATCH_PARENT);
  27. this.setLayoutParams(lpView);
  28. View center = new View(context);
  29. center.setBackgroundColor(0xFFF77AC9);
  30. center.setId(centerId);
  31. RelativeLayout.LayoutParams lpcenter = new RelativeLayout.LayoutParams( 0, 0);
  32. lpcenter.addRule(CENTER_HORIZONTAL);
  33. lpcenter.addRule(CENTER_VERTICAL);
  34. center.setLayoutParams(lpcenter);
  35. this.addView(center);
  36. this.addView(prepareElementForCircle(elements[0], 0, 0));
  37. if (elements.length % 2 == 0) {
  38. this.addView(prepareElementForCircle(elements[elements.length / 2], 0, 2 * radius));
  39. }
  40. if (elements.length > 2) {
  41. for (int i = 1; i <= (elements.length - 1) / 2; i++) {
  42. int y = i * 4 * radius / elements.length;
  43. int x = (int) Math.sqrt(Math.pow(radius, 2) - Math.pow((radius - y), 2));
  44. this.addView(prepareElementForCircle(elements[i], x, y));
  45. this.addView(prepareElementForCircle(elements[elements.length - i], -x, y));
  46. }
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement