Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import android.content.Context;
import android.content.res.Resources;

import android.text.Layout;
import android.text.StaticLayout;
import android.text.TextPaint;

import android.util.TypedValue;

import android.widget.TextView;

public class Main {
    private TextView mTextView;
    /**
     * Original textSize of the TextView.
     */
    private float mTextSize;
    private boolean mIsAutofitting;

    static int getTextHeight(CharSequence text, TextPaint paint, int targetWidth, float textSize) {
        TextPaint paintCopy = new TextPaint(paint);
        paintCopy.setTextSize(textSize);
        StaticLayout layout = new StaticLayout(text, paintCopy, (int) targetWidth, Layout.Alignment.ALIGN_NORMAL,
                1.0f, 0.0f, true);
        return layout.getHeight();
    }

    /**
     * Set the original text size of the View.
     *
     * @see TextView#setTextSize(float)
     */
    public void setTextSize(float size) {
        setTextSize(TypedValue.COMPLEX_UNIT_SP, size);
    }

    /**
     * Set the original text size of the View.
     *
     * @see TextView#setTextSize(int, float)
     */
    public void setTextSize(int unit, float size) {
        if (mIsAutofitting) {
            // We don't want to update the TextView's actual textSize while we're autofitting
            // since it'd get set to the autofitTextSize
            return;
        }
        Context context = mTextView.getContext();
        Resources r = Resources.getSystem();

        if (context != null) {
            r = context.getResources();
        }

        setRawTextSize(TypedValue.applyDimension(unit, size, r.getDisplayMetrics()));
    }

    private void setRawTextSize(float size) {
        if (mTextSize != size) {
            mTextSize = size;
        }
    }
}