Example usage for android.graphics Paint measureText

List of usage examples for android.graphics Paint measureText

Introduction

In this page you can find the example usage for android.graphics Paint measureText.

Prototype

public float measureText(String text) 

Source Link

Document

Return the width of the text.

Usage

From source file:ch.ethz.dcg.jukefox.manager.libraryimport.AndroidAlbumCoverFetcherThread.java

private void drawTextOnBitmap(CompleteAlbum album, Bitmap bitmapHigh, int bitmapSize, float textHeight) {
    Canvas canvas = new Canvas(bitmapHigh);
    canvas.drawARGB(0, 125, 125, 125);/*from   www. j  a v  a2 s.  c  o  m*/
    Paint paint = new Paint();
    paint.setColor(Color.BLACK);
    paint.setTypeface(Typeface.SERIF);
    paint.setSubpixelText(true);
    paint.setTextSize(textHeight);
    paint.setAntiAlias(true);
    String artist = album.getArtists().get(0).getName();
    String shortenedText = new String(artist);

    int textLength = artist.length();
    if (textLength > 18) {
        shortenedText = artist.substring(0, 15) + "...";
        textLength = 18;
    } else if (textLength < 8) {
        while (shortenedText.length() < 8) {
            shortenedText = " " + shortenedText + " ";
        }
    }
    float pixelLength = paint.measureText(shortenedText);
    paint.setTextSize(textHeight * (bitmapSize * 2 / 3) / pixelLength);

    canvas.drawText(shortenedText, bitmapSize / 6, bitmapSize / 3, paint);

    shortenedText = album.getName();
    textLength = album.getName().length();
    if (textLength > 18) {
        shortenedText = album.getName().substring(0, 15) + "...";
        textLength = 18;
    } else if (textLength < 8) {
        while (shortenedText.length() < 8) {
            shortenedText = " " + shortenedText + " ";
        }
    }
    paint.setTextSize(bitmapSize / 10f);
    pixelLength = paint.measureText(shortenedText);
    textHeight = textHeight * bitmapSize * 2f / 3f / pixelLength;
    paint.setTextSize(textHeight);

    canvas.drawText(shortenedText, bitmapSize / 6f, bitmapSize * 2 / 3f + textHeight, paint);
}

From source file:net.toload.main.hd.candidate.CandidateView.java

/**
 * Update composing to correct location with a delay after setComposing.
 *//* w w  w .j a v a2  s. c om*/

public void doUpdateComposing() {
    if (DEBUG)
        Log.i(TAG, "doUpdateComposing(): this.isShown()" + this.isShown() + "; embeddedComposing is null:"
                + (embeddedComposing == null));

    if (embeddedComposing != null)
        return; //Jeremy '15,6, 4 bypass updating popup when composing view is embedded in candidate container

    //mComposingTextView.measure(
    //MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
    //MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    //final int popupWidth = mComposingTextView.getMeasuredWidth();  //Jeremy '12,6,2 use getWidth and getHeight instead
    //final int popupHeight = mComposingTextView.getMeasuredHeight();
    // getMeasuredWidth cannot get correct width of textVIEW in android 6 Jeremy '16,7,16
    String composingText = String.valueOf(mComposingTextView.getText());
    if (composingText == null)
        return; // avoid measureText on null object.  Jeremy '16/7/26

    Paint paint = mComposingTextView.getPaint();
    Paint.FontMetrics metrics = paint.getFontMetrics();
    final int popupWidth = (int) paint.measureText(composingText);
    final int popupHeight = (int) (metrics.bottom - metrics.top);

    int[] offsetInWindow = new int[2];
    this.getLocationInWindow(offsetInWindow);
    int mPopupComposingY = offsetInWindow[1];
    int mPopupComposingX = 0;

    mPopupComposingY -= popupHeight;

    if (DEBUG)
        Log.i(TAG,
                "doUpdateComposing():mPopupComposingX:" + mPopupComposingX + ". mPopupComposingY:"
                        + mPopupComposingY + ". popupWidth = " + popupWidth + ". popupHeight = " + popupHeight
                        + ". mComposingTextPopup.isShowing()=" + mComposingTextPopup.isShowing());

    try {
        if (mComposingTextPopup.isShowing()) {
            mComposingTextPopup.update(mPopupComposingX, mPopupComposingY, popupWidth, popupHeight);
        } else {
            mComposingTextPopup.setWidth(popupWidth);
            mComposingTextPopup.setHeight(popupHeight);
            mComposingTextPopup.showAtLocation(this, Gravity.NO_GRAVITY, mPopupComposingX, mPopupComposingY);
        }
    } catch (Exception e) {
        // ignore candidate construct error
        e.printStackTrace();
    }

}

From source file:com.google.corp.productivity.specialprojects.android.samples.fft.AnalyzeActivity.java

@SuppressWarnings("deprecation")
private void setTextViewFontSize() {
    TextView tv = (TextView) findViewById(R.id.textview_cur);
    // At this point tv.getWidth(), tv.getLineCount() will return 0

    Paint mTestPaint = new Paint();
    mTestPaint.setTextSize(tv.getTextSize());
    mTestPaint.setTypeface(Typeface.MONOSPACE);

    final String text = "Peak:XXXXX.XHz(AX#+XX) -XXX.XdB";
    Display display = getWindowManager().getDefaultDisplay();

    // pixels left
    float px = display.getWidth() - getResources().getDimension(R.dimen.textview_RMS_layout_width) - 5;

    float fs = tv.getTextSize(); // size in pixel
    while (mTestPaint.measureText(text) > px && fs > 5) {
        fs -= 0.5;//w  w w.  j  a v a  2 s . c o m
        mTestPaint.setTextSize(fs);
    }
    ((TextView) findViewById(R.id.textview_cur)).setTextSize(fs / DPRatio);
    ((TextView) findViewById(R.id.textview_peak)).setTextSize(fs / DPRatio);
}

From source file:com.google.corp.productivity.specialprojects.android.samples.fft.AnalyzeActivity.java

public PopupWindow popupMenuCreate(String[] popUpContents, int resId) {

    // initialize a pop up window type
    PopupWindow popupWindow = new PopupWindow(this);

    // the drop down list is a list view
    ListView listView = new ListView(this);

    // set our adapter and pass our pop up window contents
    ArrayAdapter<String> aa = popupMenuAdapter(popUpContents);
    listView.setAdapter(aa);//from  w ww  . j  a v a 2s  . com

    // set the item click listener
    listView.setOnItemClickListener(this);

    listView.setTag(resId); // button res ID, so we can trace back which button is pressed

    // get max text width
    Paint mTestPaint = new Paint();
    mTestPaint.setTextSize(listItemTextSize);
    float w = 0;
    float wi; // max text width in pixel
    for (int i = 0; i < popUpContents.length; i++) {
        String sts[] = popUpContents[i].split("::");
        String st = sts[0];
        if (sts.length == 2 && sts[1].equals("0")) {
            mTestPaint.setTextSize(listItemTitleTextSize);
            wi = mTestPaint.measureText(st);
            mTestPaint.setTextSize(listItemTextSize);
        } else {
            wi = mTestPaint.measureText(st);
        }
        if (w < wi) {
            w = wi;
        }
    }

    // left and right padding, at least +7, or the whole app will stop respond, don't know why
    w = w + 20 * DPRatio;
    if (w < 60) {
        w = 60;
    }

    // some other visual settings
    popupWindow.setFocusable(true);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    // Set window width according to max text width
    popupWindow.setWidth((int) w);
    // also set button width
    ((Button) findViewById(resId)).setWidth((int) (w + 2 * DPRatio));
    // Set the text on button in updatePreferenceSaved()

    // set the list view as pop up window content
    popupWindow.setContentView(listView);

    return popupWindow;
}

From source file:github.bewantbe.audio_analyzer_for_android.AnalyzeActivity.java

@SuppressWarnings("deprecation")
private void setTextViewFontSize() {
    TextView tv = (TextView) findViewById(R.id.textview_cur);
    // At this point tv.getWidth(), tv.getLineCount() will return 0

    Paint mTestPaint = new Paint();
    mTestPaint.setTextSize(tv.getTextSize());
    mTestPaint.setTypeface(Typeface.MONOSPACE);

    final String text = getString(R.string.textview_peak_text);
    Display display = getWindowManager().getDefaultDisplay();

    // pixels left
    float px = display.getWidth() - getResources().getDimension(R.dimen.textview_RMS_layout_width) - 5;

    float fs = tv.getTextSize(); // size in pixel
    while (mTestPaint.measureText(text) > px && fs > 5) {
        fs -= 0.5;/*from  w w w.  j a va2 s. co  m*/
        mTestPaint.setTextSize(fs);
    }
    ((TextView) findViewById(R.id.textview_cur)).setTextSize(fs / DPRatio);
    ((TextView) findViewById(R.id.textview_peak)).setTextSize(fs / DPRatio);
}

From source file:in.sc9.discreteslider.DiscreteSlider.java

private void drawTextArray(Canvas canvas, int thumbWidth, int halfThumb, float yPos, int left, float intervel) {
    int lenTextArray = textArray.length;
    if (lenTextArray == 0)
        return;/*w  w w.j a  v  a2 s  . c o m*/

    float currentLeft = left;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Paint.Align.LEFT);
    paint.setColor(mtextColor);
    paint.setTextSize(mTextSize);
    //paint.setTypeface(); //TODO setTypeface in Discrete Seek Bar
    //elloWorldTextView2.setTextSize(activity.pixelsToSp(activity, textSize));
    //paint.m

    for (int i = 0; i < lenTextArray; i++) {
        /*if((int)getThumbValue(0)==i*(int)scaleStep)
        paint.setColor(Color.parseColor("#ffff0000"));
        else
        paint.setColor(Color.parseColor("#ff22445b"));*/

        float textLen = paint.measureText(textArray[i]);

        if (i == 0) {
            if (textStyle != NONE && (textStyle == ODD || textStyle == EXTREMES || textStyle == ALL))
                canvas.drawText(textArray[i], currentLeft, yPos, paint);
        } else if (i == lenTextArray - 1) {
            if (textStyle != NONE)
                if ((textStyle == ALL) || (i % 2 == 0 && textStyle == ODD) || (i % 2 != 0 && textStyle == EVEN)
                        || (textStyle == EXTREMES))
                    canvas.drawText(textArray[i], currentLeft - textLen, yPos, paint);
        } else {
            if (textStyle != 0 && textStyle != 3) {
                if ((textStyle == ALL) || (i % 2 == 0 && textStyle == ODD) || (i % 2 != 0 && textStyle == EVEN))
                    canvas.drawText(textArray[i], currentLeft - (textLen / 2), yPos, paint);
            }
        }
        currentLeft += intervel;
    }
}

From source file:com.lt.adamlee.aagame.GameView.java

public void about(Canvas c) {
    int textsize = (int) F.hf(20.0f);
    Paint p3 = new Paint();
    p3.setTypeface(tf);/*  w  w w.j ava 2  s  .  co  m*/
    p3.setColor(ViewCompat.MEASURED_STATE_MASK);
    p3.setFlags(1);
    p3.setTextSize((float) textsize);
    Paint pagepaint = new Paint();
    pagepaint.setColor(getResources().getColor(R.color.abc_secondary_text_material_light));
    c.drawRect(0.0f, 0.0f, (float) screenW, (float) screenH, pagepaint);
    p3.setTextSize(F.hf(30.0f));
    c.drawText(ctx.getString(R.string.about),
            ((float) (screenW / 2)) - (p3.measureText(ctx.getString(R.string.about)) / 2.0f),
            (float) (screenH / 4), p3);
    p3.setTextSize((float) textsize);
    c.drawText(ctx.getString(R.string.app_name),
            ((float) (screenW / 2)) - (p3.measureText(ctx.getString(R.string.app_name)) / 2.0f),
            (float) (((textsize * 2) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    c.drawText("License Version 1.7", ((float) (screenW / 2)) - (p3.measureText("License Version 1.7") / 2.0f),
            (float) (((textsize * 3) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    p3.setTextSize(F.hf(17.0f));
    c.drawText("Powered by Map Game Studio",
            ((float) (screenW / 2)) - (p3.measureText("Powered by Map Game Studio") / 2.0f),
            (float) (((textsize * 4) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
    p3.setTextSize(F.hf(17.0f));
    c.drawText("Email:mapgamestudio@gmail.com",
            ((float) (screenW / 2)) - (p3.measureText("Email:mapgamestudio@gmail.com") / 2.0f),
            (float) (((textsize * 5) + 1) + ((screenH - (textsize * 7)) / 2)), p3);
}

From source file:com.inmobi.ultrapush.AnalyzeActivity.java

public PopupWindow popupMenuCreate(String[] popUpContents, int resId) {

    // initialize a pop up window type
    PopupWindow popupWindow = new PopupWindow(this);

    // the drop down list is a list view
    ListView listView = new ListView(this);

    // set our adapter and pass our pop up window contents
    ArrayAdapter<String> aa = popupMenuAdapter(popUpContents);
    listView.setAdapter(aa);//from  www .  jav  a 2 s.com

    // set the item click listener
    listView.setOnItemClickListener(this);

    listView.setTag(resId); // button res ID, so we can trace back which button is pressed

    // get max text width
    Paint mTestPaint = new Paint();
    mTestPaint.setTextSize(listItemTextSize);
    float w = 0;
    float wi; // max text width in pixel
    for (int i = 0; i < popUpContents.length; i++) {
        String sts[] = popUpContents[i].split("::");
        String st = sts[0];
        if (sts.length == 2 && sts[1].equals("0")) {
            mTestPaint.setTextSize(listItemTitleTextSize);
            wi = mTestPaint.measureText(st);
            mTestPaint.setTextSize(listItemTextSize);
        } else {
            wi = mTestPaint.measureText(st);
        }
        if (w < wi) {
            w = wi;
        }
    }

    // left and right padding, at least +7, or the whole app will stop respond, don't know why
    w = w + 20 * DPRatio;
    if (w < 60) {
        w = 60;
    }

    // some other visual settings
    popupWindow.setFocusable(true);
    popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
    // Set window width according to max text width
    popupWindow.setWidth((int) w);
    // also set button width
    ((Button) findViewById(resId)).setWidth((int) (w + 4 * DPRatio));
    // Set the text on button in updatePreferenceSaved()

    // set the list view as pop up window content
    popupWindow.setContentView(listView);

    return popupWindow;
}

From source file:com.inmobi.ultrapush.AnalyzeActivity.java

@SuppressWarnings("deprecation")
private void setTextViewFontSize() {
    TextView tv = (TextView) findViewById(R.id.textview_cur);
    // At this point tv.getWidth(), tv.getLineCount() will return 0

    Paint mTestPaint = new Paint();
    mTestPaint.setTextSize(tv.getTextSize());
    mTestPaint.setTypeface(Typeface.MONOSPACE);

    final String text = getString(R.string.textview_peak_text);
    Display display = getWindowManager().getDefaultDisplay();

    // pixels left
    float px = display.getWidth() - getResources().getDimension(R.dimen.textview_RMS_layout_width) - 5;

    float fs = tv.getTextSize(); // size in pixel
    while (mTestPaint.measureText(text) > px && fs > 5) {
        fs -= 0.5;//w w  w  . j  a v a  2  s . c o m
        mTestPaint.setTextSize(fs);
    }

    ((TextView) findViewById(R.id.textview_cur)).setTextSize(fs / DPRatio);
    ((TextView) findViewById(R.id.textview_peak)).setTextSize(fs / DPRatio);
}

From source file:com.lt.adamlee.aagame.GameView.java

public void help(Canvas c) {
    String x = "";
    float textsize = F.hf(19.0f);
    Paint p4 = new Paint();
    Paint p5 = new Paint();
    Paint p6 = new Paint();
    String text = ctx.getString(R.string.helptext);
    p4.setColor(getResources().getColor(R.color.bright_foreground_inverse_material_light));
    p4.setFlags(1);/*  w w w  .j av  a  2  s . c  o  m*/
    p5.setTypeface(tf);
    p5.setTextSize(F.hf(30.0f));
    p6.setTextSize(textsize);
    p5.setColor(ViewCompat.MEASURED_STATE_MASK);
    c.drawRect(0.0f, 0.0f, (float) screenW, (float) screenH, p4);
    c.drawText(ctx.getString(R.string.help),
            ((float) (screenW / 2)) - (p5.measureText(ctx.getString(R.string.help)) / 2.0f),
            (float) (screenH / 8), p5);
    int i = 0;
    int xv1 = 0;
    int yv1 = (int) textsize;
    while (i < ctx.getString(R.string.helptext).length()) {
        if (text.charAt(i) != '$') {
            c.drawText("  " + ctx.getString(R.string.helptext).charAt(i), (float) xv1,
                    ((float) ((screenH / 8) + yv1)) + (4.0f * textsize), p6);
            xv1 = (int) (((float) xv1)
                    + p6.measureText(Character.toString(ctx.getString(R.string.helptext).charAt(i))));
        }
        if (ctx.getString(R.string.helptext).charAt(i) == '$') {
            i++;
            x = " ";
            yv1 = (int) (((float) yv1) + textsize);
            xv1 = 0;
        }
        i++;
    }
}