All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.name.TextFitTextView Maven / Gradle / Ivy

/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * $Id: TextFitTextView.java,v 0c777eac481c 2010/08/25 23:58:53 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.name;

import javax.annotation.Nonnull;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.TextView;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class TextFitTextView extends TextView
  {
    static final String TAG = "TextFitTextView";

    private float scale = 1f;

    public TextFitTextView (final @Nonnull Context context)
      {
        super(context);
      }

    public TextFitTextView (final @Nonnull Context context, final @Nonnull AttributeSet attrs)
      {
        super(context, attrs);
        init(context, attrs);
      }

    public TextFitTextView (final @Nonnull Context context, final @Nonnull AttributeSet attrs, final int defStyle)
      {
        super(context, attrs, defStyle);
        init(context, attrs);
      }

    private void init (final @Nonnull Context context, final @Nonnull AttributeSet attrs)
      {
//        final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextFitTextView);
//        scale = a.getFloat(R.styleable.TextFitTextView_scale, scale);
//        a.recycle();
      }

    @Override
    protected void onSizeChanged (final int width, final int height, final int oldWidth, final int oldHeight)
      {
        super.onSizeChanged(width, height, oldWidth, oldHeight);
        final float originalTextSize = getTextSize();

        if ((width > 0) && (height > 0))
          {
            computeFontSize(width);
            final int newHeight = (int)(height * getTextSize() / originalTextSize);

              System.err.println("H " + height + " NEW H " + newHeight);
//            if (newHeight != height)
            if (Math.abs(newHeight - height) > 1)
              {
                post(new Runnable()
                  {
                    public void run()
                      {
                        setHeight(newHeight);
                        requestLayout();
                      }
                  });
              }
          }
      }

    private void computeFontSize (int width)
      {
        if (getTypeface().isItalic())
          {
            scale = 0.5f;
          }
        
        final String text = getText().toString();
        width -= getPaddingLeft() + getPaddingRight();
        float textWidth = getPaint().measureText(text);

        for (double s = 0.95f; s >= 0.1f; s -= 0.1f)
          {
            setTextSize((float)Math.floor((scale * s * getTextSize() * width) / textWidth));
            textWidth = getPaint().measureText(text);
//              System.err.println("SCALE " + scale + " TEXT WIDTH " + textWidth + " vs " + width);

            if (textWidth < Math.floor(scale * width))
              {
                break;
              }
          }
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy