Advertisement
Guest User

AutoFitText

a guest
Oct 18th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.07 KB | None | 0 0
  1. /**
  2.  * A {@link ViewGroup} that re-sizes the text of it's children to be no larger than the width of the
  3.  * view.
  4.  *
  5.  * @attr ref R.styleable.AutofitTextView_sizeToFit
  6.  * @attr ref R.styleable.AutofitTextView_minTextSize
  7.  * @attr ref R.styleable.AutofitTextView_precision
  8.  */
  9. public class AutofitLayout extends FrameLayout {
  10.  
  11.     private boolean mEnabled;
  12.     private float mMinTextSize;
  13.     private float mPrecision;
  14.     private WeakHashMap<View, AutofitHelper> mHelpers = new WeakHashMap<View, AutofitHelper>();
  15.  
  16.     public AutofitLayout(Context context) {
  17.         super(context);
  18.         init(context, null, 0);
  19.     }
  20.  
  21.     public AutofitLayout(Context context, AttributeSet attrs) {
  22.         super(context, attrs);
  23.         init(context, attrs, 0);
  24.     }
  25.  
  26.     public AutofitLayout(Context context, AttributeSet attrs, int defStyle) {
  27.         super(context, attrs, defStyle);
  28.         init(context, attrs, defStyle);
  29.     }
  30.  
  31.     private void init(Context context, AttributeSet attrs, int defStyle) {
  32.         boolean sizeToFit = true;
  33.         int minTextSize = -1;
  34.         float precision = -1;
  35.  
  36.         if (attrs != null) {
  37.             TypedArray ta = context.obtainStyledAttributes(
  38.                     attrs,
  39.                     R.styleable.AutofitTextView,
  40.                     defStyle,
  41.                     0);
  42.             sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
  43.             minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
  44.                     minTextSize);
  45.             precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
  46.             ta.recycle();
  47.         }
  48.  
  49.         mEnabled = sizeToFit;
  50.         mMinTextSize = minTextSize;
  51.         mPrecision = precision;
  52.     }
  53.  
  54.     @Override
  55.     public void addView(View child, int index, ViewGroup.LayoutParams params) {
  56.         super.addView(child, index, params);
  57.         TextView textView = (TextView) child;
  58.         AutofitHelper helper = AutofitHelper.create(textView)
  59.                 .setEnabled(mEnabled);
  60.         if (mPrecision > 0) {
  61.             helper.setPrecision(mPrecision);
  62.         }
  63.         if (mMinTextSize > 0) {
  64.             helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize);
  65.         }
  66.         mHelpers.put(textView, helper);
  67.     }
  68.  
  69.     /**
  70.      * Returns the {@link AutofitHelper} for this child View.
  71.      */
  72.     public AutofitHelper getAutofitHelper(TextView textView) {
  73.         return mHelpers.get(textView);
  74.     }
  75.  
  76.     /**
  77.      * Returns the {@link AutofitHelper} for this child View.
  78.      */
  79.     public AutofitHelper getAutofitHelper(int index) {
  80.         return mHelpers.get(getChildAt(index));
  81.     }
  82. }
  83.  
  84.  
  85. /**
  86.  * A {@link ViewGroup} that re-sizes the text of it's children to be no larger than the width of the
  87.  * view.
  88.  *
  89.  * @attr ref R.styleable.AutofitTextView_sizeToFit
  90.  * @attr ref R.styleable.AutofitTextView_minTextSize
  91.  * @attr ref R.styleable.AutofitTextView_precision
  92.  */
  93. public class AutofitLayout extends FrameLayout {
  94.  
  95.     private boolean mEnabled;
  96.     private float mMinTextSize;
  97.     private float mPrecision;
  98.     private WeakHashMap<View, AutofitHelper> mHelpers = new WeakHashMap<View, AutofitHelper>();
  99.  
  100.     public AutofitLayout(Context context) {
  101.         super(context);
  102.         init(context, null, 0);
  103.     }
  104.  
  105.     public AutofitLayout(Context context, AttributeSet attrs) {
  106.         super(context, attrs);
  107.         init(context, attrs, 0);
  108.     }
  109.  
  110.     public AutofitLayout(Context context, AttributeSet attrs, int defStyle) {
  111.         super(context, attrs, defStyle);
  112.         init(context, attrs, defStyle);
  113.     }
  114.  
  115.     private void init(Context context, AttributeSet attrs, int defStyle) {
  116.         boolean sizeToFit = true;
  117.         int minTextSize = -1;
  118.         float precision = -1;
  119.  
  120.         if (attrs != null) {
  121.             TypedArray ta = context.obtainStyledAttributes(
  122.                     attrs,
  123.                     R.styleable.AutofitTextView,
  124.                     defStyle,
  125.                     0);
  126.             sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
  127.             minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
  128.                     minTextSize);
  129.             precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
  130.             ta.recycle();
  131.         }
  132.  
  133.         mEnabled = sizeToFit;
  134.         mMinTextSize = minTextSize;
  135.         mPrecision = precision;
  136.     }
  137.  
  138.     @Override
  139.     public void addView(View child, int index, ViewGroup.LayoutParams params) {
  140.         super.addView(child, index, params);
  141.         TextView textView = (TextView) child;
  142.         AutofitHelper helper = AutofitHelper.create(textView)
  143.                 .setEnabled(mEnabled);
  144.         if (mPrecision > 0) {
  145.             helper.setPrecision(mPrecision);
  146.         }
  147.         if (mMinTextSize > 0) {
  148.             helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize);
  149.         }
  150.         mHelpers.put(textView, helper);
  151.     }
  152.  
  153.     /**
  154.      * Returns the {@link AutofitHelper} for this child View.
  155.      */
  156.     public AutofitHelper getAutofitHelper(TextView textView) {
  157.         return mHelpers.get(textView);
  158.     }
  159.  
  160.     /**
  161.      * Returns the {@link AutofitHelper} for this child View.
  162.      */
  163.     public AutofitHelper getAutofitHelper(int index) {
  164.         return mHelpers.get(getChildAt(index));
  165.     }
  166. }
  167.  
  168.  
  169.  
  170. /**
  171.  * A {@link ViewGroup} that re-sizes the text of it's children to be no larger than the width of the
  172.  * view.
  173.  *
  174.  * @attr ref R.styleable.AutofitTextView_sizeToFit
  175.  * @attr ref R.styleable.AutofitTextView_minTextSize
  176.  * @attr ref R.styleable.AutofitTextView_precision
  177.  */
  178. public class AutofitLayout extends FrameLayout {
  179.  
  180.     private boolean mEnabled;
  181.     private float mMinTextSize;
  182.     private float mPrecision;
  183.     private WeakHashMap<View, AutofitHelper> mHelpers = new WeakHashMap<View, AutofitHelper>();
  184.  
  185.     public AutofitLayout(Context context) {
  186.         super(context);
  187.         init(context, null, 0);
  188.     }
  189.  
  190.     public AutofitLayout(Context context, AttributeSet attrs) {
  191.         super(context, attrs);
  192.         init(context, attrs, 0);
  193.     }
  194.  
  195.     public AutofitLayout(Context context, AttributeSet attrs, int defStyle) {
  196.         super(context, attrs, defStyle);
  197.         init(context, attrs, defStyle);
  198.     }
  199.  
  200.     private void init(Context context, AttributeSet attrs, int defStyle) {
  201.         boolean sizeToFit = true;
  202.         int minTextSize = -1;
  203.         float precision = -1;
  204.  
  205.         if (attrs != null) {
  206.             TypedArray ta = context.obtainStyledAttributes(
  207.                     attrs,
  208.                     R.styleable.AutofitTextView,
  209.                     defStyle,
  210.                     0);
  211.             sizeToFit = ta.getBoolean(R.styleable.AutofitTextView_sizeToFit, sizeToFit);
  212.             minTextSize = ta.getDimensionPixelSize(R.styleable.AutofitTextView_minTextSize,
  213.                     minTextSize);
  214.             precision = ta.getFloat(R.styleable.AutofitTextView_precision, precision);
  215.             ta.recycle();
  216.         }
  217.  
  218.         mEnabled = sizeToFit;
  219.         mMinTextSize = minTextSize;
  220.         mPrecision = precision;
  221.     }
  222.  
  223.     @Override
  224.     public void addView(View child, int index, ViewGroup.LayoutParams params) {
  225.         super.addView(child, index, params);
  226.         TextView textView = (TextView) child;
  227.         AutofitHelper helper = AutofitHelper.create(textView)
  228.                 .setEnabled(mEnabled);
  229.         if (mPrecision > 0) {
  230.             helper.setPrecision(mPrecision);
  231.         }
  232.         if (mMinTextSize > 0) {
  233.             helper.setMinTextSize(TypedValue.COMPLEX_UNIT_PX, mMinTextSize);
  234.         }
  235.         mHelpers.put(textView, helper);
  236.     }
  237.  
  238.     /**
  239.      * Returns the {@link AutofitHelper} for this child View.
  240.      */
  241.     public AutofitHelper getAutofitHelper(TextView textView) {
  242.         return mHelpers.get(textView);
  243.     }
  244.  
  245.     /**
  246.      * Returns the {@link AutofitHelper} for this child View.
  247.      */
  248.     public AutofitHelper getAutofitHelper(int index) {
  249.         return mHelpers.get(getChildAt(index));
  250.     }
  251. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement