FlowLayout.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. package de.tudarmstadt.informatik.hostage.ui2.layouts;
  2. import android.content.Context;
  3. import android.content.res.TypedArray;
  4. import android.graphics.Canvas;
  5. import android.graphics.Paint;
  6. import android.util.AttributeSet;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import de.tudarmstadt.informatik.hostage.R;
  10. public class FlowLayout extends ViewGroup {
  11. public static final int HORIZONTAL = 0;
  12. public static final int VERTICAL = 1;
  13. private int horizontalSpacing = 0;
  14. private int verticalSpacing = 0;
  15. private int orientation = 0;
  16. private boolean debugDraw = false;
  17. public FlowLayout(Context context) {
  18. super(context);
  19. this.readStyleParameters(context, null);
  20. }
  21. public FlowLayout(Context context, AttributeSet attributeSet) {
  22. super(context, attributeSet);
  23. this.readStyleParameters(context, attributeSet);
  24. }
  25. public FlowLayout(Context context, AttributeSet attributeSet, int defStyle) {
  26. super(context, attributeSet, defStyle);
  27. this.readStyleParameters(context, attributeSet);
  28. }
  29. @Override
  30. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  31. int sizeWidth = MeasureSpec.getSize(widthMeasureSpec) - this.getPaddingRight() - this.getPaddingLeft();
  32. int sizeHeight = MeasureSpec.getSize(heightMeasureSpec) - this.getPaddingTop() - this.getPaddingBottom();
  33. int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
  34. int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
  35. int size;
  36. int mode;
  37. if (orientation == HORIZONTAL) {
  38. size = sizeWidth;
  39. mode = modeWidth;
  40. } else {
  41. size = sizeHeight;
  42. mode = modeHeight;
  43. }
  44. int lineThicknessWithSpacing = 0;
  45. int lineThickness = 0;
  46. int lineLengthWithSpacing = 0;
  47. int lineLength;
  48. int prevLinePosition = 0;
  49. int controlMaxLength = 0;
  50. int controlMaxThickness = 0;
  51. final int count = getChildCount();
  52. for (int i = 0; i < count; i++) {
  53. final View child = getChildAt(i);
  54. if (child.getVisibility() == GONE) {
  55. continue;
  56. }
  57. LayoutParams lp = (LayoutParams) child.getLayoutParams();
  58. child.measure(
  59. getChildMeasureSpec(widthMeasureSpec, this.getPaddingLeft() + this.getPaddingRight(), lp.width),
  60. getChildMeasureSpec(heightMeasureSpec, this.getPaddingTop() + this.getPaddingBottom(), lp.height)
  61. );
  62. int hSpacing = this.getHorizontalSpacing(lp);
  63. int vSpacing = this.getVerticalSpacing(lp);
  64. int childWidth = child.getMeasuredWidth();
  65. int childHeight = child.getMeasuredHeight();
  66. int childLength;
  67. int childThickness;
  68. int spacingLength;
  69. int spacingThickness;
  70. if (orientation == HORIZONTAL) {
  71. childLength = childWidth;
  72. childThickness = childHeight;
  73. spacingLength = hSpacing;
  74. spacingThickness = vSpacing;
  75. } else {
  76. childLength = childHeight;
  77. childThickness = childWidth;
  78. spacingLength = vSpacing;
  79. spacingThickness = hSpacing;
  80. }
  81. lineLength = lineLengthWithSpacing + childLength;
  82. lineLengthWithSpacing = lineLength + spacingLength;
  83. boolean newLine = lp.newLine || (mode != MeasureSpec.UNSPECIFIED && lineLength > size);
  84. if (newLine) {
  85. prevLinePosition = prevLinePosition + lineThicknessWithSpacing;
  86. lineThickness = childThickness;
  87. lineLength = childLength;
  88. lineThicknessWithSpacing = childThickness + spacingThickness;
  89. lineLengthWithSpacing = lineLength + spacingLength;
  90. }
  91. lineThicknessWithSpacing = Math.max(lineThicknessWithSpacing, childThickness + spacingThickness);
  92. lineThickness = Math.max(lineThickness, childThickness);
  93. int posX;
  94. int posY;
  95. if (orientation == HORIZONTAL) {
  96. posX = getPaddingLeft() + lineLength - childLength;
  97. posY = getPaddingTop() + prevLinePosition;
  98. } else {
  99. posX = getPaddingLeft() + prevLinePosition;
  100. posY = getPaddingTop() + lineLength - childHeight;
  101. }
  102. lp.setPosition(posX, posY);
  103. controlMaxLength = Math.max(controlMaxLength, lineLength);
  104. controlMaxThickness = prevLinePosition + lineThickness;
  105. }
  106. /* need to take paddings into account */
  107. if (orientation == HORIZONTAL) {
  108. controlMaxLength += getPaddingLeft() + getPaddingRight();
  109. controlMaxThickness += getPaddingBottom() + getPaddingTop();
  110. } else {
  111. controlMaxLength += getPaddingBottom() + getPaddingTop();
  112. controlMaxThickness += getPaddingLeft() + getPaddingRight();
  113. }
  114. if (orientation == HORIZONTAL) {
  115. this.setMeasuredDimension(resolveSize(controlMaxLength, widthMeasureSpec), resolveSize(controlMaxThickness, heightMeasureSpec));
  116. } else {
  117. this.setMeasuredDimension(resolveSize(controlMaxThickness, widthMeasureSpec), resolveSize(controlMaxLength, heightMeasureSpec));
  118. }
  119. }
  120. private int getVerticalSpacing(LayoutParams lp) {
  121. int vSpacing;
  122. if (lp.verticalSpacingSpecified()) {
  123. vSpacing = lp.verticalSpacing;
  124. } else {
  125. vSpacing = this.verticalSpacing;
  126. }
  127. return vSpacing;
  128. }
  129. private int getHorizontalSpacing(LayoutParams lp) {
  130. int hSpacing;
  131. if (lp.horizontalSpacingSpecified()) {
  132. hSpacing = lp.horizontalSpacing;
  133. } else {
  134. hSpacing = this.horizontalSpacing;
  135. }
  136. return hSpacing;
  137. }
  138. @Override
  139. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  140. final int count = getChildCount();
  141. for (int i = 0; i < count; i++) {
  142. View child = getChildAt(i);
  143. LayoutParams lp = (LayoutParams) child.getLayoutParams();
  144. child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
  145. }
  146. }
  147. @Override
  148. protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
  149. boolean more = super.drawChild(canvas, child, drawingTime);
  150. this.drawDebugInfo(canvas, child);
  151. return more;
  152. }
  153. @Override
  154. protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
  155. return p instanceof LayoutParams;
  156. }
  157. @Override
  158. protected LayoutParams generateDefaultLayoutParams() {
  159. return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  160. }
  161. @Override
  162. public LayoutParams generateLayoutParams(AttributeSet attributeSet) {
  163. return new LayoutParams(getContext(), attributeSet);
  164. }
  165. @Override
  166. protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
  167. return new LayoutParams(p);
  168. }
  169. private void readStyleParameters(Context context, AttributeSet attributeSet) {
  170. TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout);
  171. try {
  172. horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_horizontalSpacing, 0);
  173. verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_verticalSpacing, 0);
  174. orientation = a.getInteger(R.styleable.FlowLayout_orientation, HORIZONTAL);
  175. debugDraw = a.getBoolean(R.styleable.FlowLayout_debugDraw, false);
  176. } finally {
  177. a.recycle();
  178. }
  179. }
  180. private void drawDebugInfo(Canvas canvas, View child) {
  181. if (!debugDraw) {
  182. return;
  183. }
  184. Paint childPaint = this.createPaint(0xffffff00);
  185. Paint layoutPaint = this.createPaint(0xff00ff00);
  186. Paint newLinePaint = this.createPaint(0xffff0000);
  187. LayoutParams lp = (LayoutParams) child.getLayoutParams();
  188. if (lp.horizontalSpacing > 0) {
  189. float x = child.getRight();
  190. float y = child.getTop() + child.getHeight() / 2.0f;
  191. canvas.drawLine(x, y, x + lp.horizontalSpacing, y, childPaint);
  192. canvas.drawLine(x + lp.horizontalSpacing - 4.0f, y - 4.0f, x + lp.horizontalSpacing, y, childPaint);
  193. canvas.drawLine(x + lp.horizontalSpacing - 4.0f, y + 4.0f, x + lp.horizontalSpacing, y, childPaint);
  194. } else if (this.horizontalSpacing > 0) {
  195. float x = child.getRight();
  196. float y = child.getTop() + child.getHeight() / 2.0f;
  197. canvas.drawLine(x, y, x + this.horizontalSpacing, y, layoutPaint);
  198. canvas.drawLine(x + this.horizontalSpacing - 4.0f, y - 4.0f, x + this.horizontalSpacing, y, layoutPaint);
  199. canvas.drawLine(x + this.horizontalSpacing - 4.0f, y + 4.0f, x + this.horizontalSpacing, y, layoutPaint);
  200. }
  201. if (lp.verticalSpacing > 0) {
  202. float x = child.getLeft() + child.getWidth() / 2.0f;
  203. float y = child.getBottom();
  204. canvas.drawLine(x, y, x, y + lp.verticalSpacing, childPaint);
  205. canvas.drawLine(x - 4.0f, y + lp.verticalSpacing - 4.0f, x, y + lp.verticalSpacing, childPaint);
  206. canvas.drawLine(x + 4.0f, y + lp.verticalSpacing - 4.0f, x, y + lp.verticalSpacing, childPaint);
  207. } else if (this.verticalSpacing > 0) {
  208. float x = child.getLeft() + child.getWidth() / 2.0f;
  209. float y = child.getBottom();
  210. canvas.drawLine(x, y, x, y + this.verticalSpacing, layoutPaint);
  211. canvas.drawLine(x - 4.0f, y + this.verticalSpacing - 4.0f, x, y + this.verticalSpacing, layoutPaint);
  212. canvas.drawLine(x + 4.0f, y + this.verticalSpacing - 4.0f, x, y + this.verticalSpacing, layoutPaint);
  213. }
  214. if (lp.newLine) {
  215. if (orientation == HORIZONTAL) {
  216. float x = child.getLeft();
  217. float y = child.getTop() + child.getHeight() / 2.0f;
  218. canvas.drawLine(x, y - 6.0f, x, y + 6.0f, newLinePaint);
  219. } else {
  220. float x = child.getLeft() + child.getWidth() / 2.0f;
  221. float y = child.getTop();
  222. canvas.drawLine(x - 6.0f, y, x + 6.0f, y, newLinePaint);
  223. }
  224. }
  225. }
  226. private Paint createPaint(int color) {
  227. Paint paint = new Paint();
  228. paint.setAntiAlias(true);
  229. paint.setColor(color);
  230. paint.setStrokeWidth(2.0f);
  231. return paint;
  232. }
  233. public static class LayoutParams extends ViewGroup.LayoutParams {
  234. private static int NO_SPACING = -1;
  235. private int x;
  236. private int y;
  237. private int horizontalSpacing = NO_SPACING;
  238. private int verticalSpacing = NO_SPACING;
  239. private boolean newLine = false;
  240. public LayoutParams(Context context, AttributeSet attributeSet) {
  241. super(context, attributeSet);
  242. this.readStyleParameters(context, attributeSet);
  243. }
  244. public LayoutParams(int width, int height) {
  245. super(width, height);
  246. }
  247. public LayoutParams(ViewGroup.LayoutParams layoutParams) {
  248. super(layoutParams);
  249. }
  250. public boolean horizontalSpacingSpecified() {
  251. return horizontalSpacing != NO_SPACING;
  252. }
  253. public boolean verticalSpacingSpecified() {
  254. return verticalSpacing != NO_SPACING;
  255. }
  256. public void setPosition(int x, int y) {
  257. this.x = x;
  258. this.y = y;
  259. }
  260. private void readStyleParameters(Context context, AttributeSet attributeSet) {
  261. TypedArray a = context.obtainStyledAttributes(attributeSet, R.styleable.FlowLayout_LayoutParams);
  262. try {
  263. horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, NO_SPACING);
  264. verticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_verticalSpacing, NO_SPACING);
  265. newLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_newLine, false);
  266. } finally {
  267. a.recycle();
  268. }
  269. }
  270. }
  271. }