Advertisement
Guest User

Untitled

a guest
Mar 25th, 2017
298
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. package com.example.android.layout;
  2.  
  3. import android.content.Context;
  4. import android.content.res.TypedArray;
  5. import android.graphics.Canvas;
  6. import android.graphics.Paint;
  7. import android.util.AttributeSet;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10.  
  11. public class FlowLayout extends ViewGroup {
  12.     private int mHorizontalSpacing;
  13.     private int mVerticalSpacing;
  14.     private Paint mPaint;
  15.  
  16.     public FlowLayout(Context context, AttributeSet attrs) {
  17.         super(context, attrs);
  18.        
  19.         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout);
  20.         try {
  21.             mHorizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_horizontalSpacing, 0);
  22.             mVerticalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_verticalSpacing, 0);
  23.         } finally {
  24.             a.recycle();
  25.         }
  26.        
  27.         mPaint = new Paint();
  28.         mPaint.setAntiAlias(true);
  29.         mPaint.setColor(0xffff0000);
  30.         mPaint.setStrokeWidth(2.0f);
  31.     }
  32.  
  33.     @Override
  34.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  35.         int widthSize = MeasureSpec.getSize(widthMeasureSpec) - getPaddingRight();
  36.         int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  37.  
  38.         boolean growHeight = widthMode != MeasureSpec.UNSPECIFIED;
  39.  
  40.         int width = 0;
  41.         int height = getPaddingTop();
  42.  
  43.         int currentWidth = getPaddingLeft();
  44.         int currentHeight = 0;
  45.  
  46.         boolean breakLine = false;
  47.         boolean newLine = false;
  48.         int spacing = 0;
  49.  
  50.         final int count = getChildCount();
  51.         for (int i = 0; i < count; i++) {
  52.             View child = getChildAt(i);
  53.             measureChild(child, widthMeasureSpec, heightMeasureSpec);
  54.  
  55.             LayoutParams lp = (LayoutParams) child.getLayoutParams();
  56.             spacing = mHorizontalSpacing;
  57.             if (lp.horizontalSpacing >= 0) {
  58.                 spacing = lp.horizontalSpacing;
  59.             }
  60.  
  61.             if (growHeight && (breakLine || currentWidth + child.getMeasuredWidth() > widthSize)) {
  62.                 height += currentHeight + mVerticalSpacing;
  63.                 currentHeight = 0;
  64.                 width = Math.max(width, currentWidth - spacing);
  65.                 currentWidth = getPaddingLeft();
  66.                 newLine = true;
  67.             } else {
  68.                 newLine = false;
  69.             }
  70.  
  71.             lp.x = currentWidth;
  72.             lp.y = height;
  73.  
  74.             currentWidth += child.getMeasuredWidth() + spacing;
  75.             currentHeight = Math.max(currentHeight, child.getMeasuredHeight());
  76.            
  77.             breakLine = lp.breakLine;
  78.         }
  79.  
  80.         if (!newLine) {
  81.             height += currentHeight;
  82.             width = Math.max(width, currentWidth - spacing);
  83.         }
  84.  
  85.         width += getPaddingRight();
  86.         height += getPaddingBottom();
  87.  
  88.         setMeasuredDimension(resolveSize(width, widthMeasureSpec),
  89.                 resolveSize(height, heightMeasureSpec));
  90.     }
  91.  
  92.     @Override
  93.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  94.         final int count = getChildCount();
  95.         for (int i = 0; i < count; i++) {
  96.             View child = getChildAt(i);
  97.             LayoutParams lp = (LayoutParams) child.getLayoutParams();
  98.             child.layout(lp.x, lp.y, lp.x + child.getMeasuredWidth(), lp.y + child.getMeasuredHeight());
  99.         }
  100.     }
  101.    
  102.     @Override
  103.     protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
  104.         boolean more = super.drawChild(canvas, child, drawingTime);
  105.         LayoutParams lp = (LayoutParams) child.getLayoutParams();
  106.         if (lp.horizontalSpacing > 0) {
  107.             float x = child.getRight();
  108.             float y = child.getTop() + child.getHeight() / 2.0f;
  109.             canvas.drawLine(x, y - 4.0f, x, y + 4.0f, mPaint);
  110.             canvas.drawLine(x, y, x + lp.horizontalSpacing, y, mPaint);
  111.             canvas.drawLine(x + lp.horizontalSpacing, y - 4.0f, x + lp.horizontalSpacing, y + 4.0f, mPaint);
  112.         }
  113.         if (lp.breakLine) {
  114.             float x = child.getRight();
  115.             float y = child.getTop() + child.getHeight() / 2.0f;
  116.             canvas.drawLine(x, y, x, y + 6.0f, mPaint);
  117.             canvas.drawLine(x, y + 6.0f, x + 6.0f, y + 6.0f, mPaint);
  118.         }
  119.         return more;
  120.     }
  121.  
  122.     @Override
  123.     protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
  124.         return p instanceof LayoutParams;
  125.     }
  126.  
  127.     @Override
  128.     protected LayoutParams generateDefaultLayoutParams() {
  129.         return new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
  130.     }
  131.  
  132.     @Override
  133.     public LayoutParams generateLayoutParams(AttributeSet attrs) {
  134.         return new LayoutParams(getContext(), attrs);
  135.     }
  136.    
  137.     @Override
  138.     protected LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
  139.         return new LayoutParams(p.width, p.height);
  140.     }
  141.  
  142.     public static class LayoutParams extends ViewGroup.LayoutParams {
  143.         int x;
  144.         int y;
  145.        
  146.         public int horizontalSpacing;
  147.         public boolean breakLine;
  148.  
  149.         public LayoutParams(Context context, AttributeSet attrs) {
  150.             super(context, attrs);
  151.             TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FlowLayout_LayoutParams);
  152.             try {
  153.                 horizontalSpacing = a.getDimensionPixelSize(R.styleable.FlowLayout_LayoutParams_layout_horizontalSpacing, -1);
  154.                 breakLine = a.getBoolean(R.styleable.FlowLayout_LayoutParams_layout_breakLine, false);
  155.             } finally {
  156.                 a.recycle();
  157.             }
  158.         }
  159.  
  160.         public LayoutParams(int w, int h) {
  161.             super(w, h);
  162.         }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement