Example usage for android.graphics Paint setTextAlign

List of usage examples for android.graphics Paint setTextAlign

Introduction

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

Prototype

public void setTextAlign(Align align) 

Source Link

Document

Set the paint's text alignment.

Usage

From source file:eu.iescities.pilot.rovereto.roveretoexplorer.map.MapManager.java

private static Bitmap writeOnStoryMarker(Context mContext, int drawableId, String text) {
    float scale = mContext.getResources().getDisplayMetrics().density;

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(scale * 14);/*  ww w . j  a v  a 2 s.c o  m*/
    paint.setAntiAlias(true);
    paint.setARGB(255, 255, 255, 255);

    Canvas canvas = new Canvas(bitmap);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2;
    float y = bitmap.getHeight() / 2 - ((paint.descent() + paint.ascent()) / 2);

    canvas.drawText(text, x, y, paint);

    return bitmap;
}

From source file:eu.trentorise.smartcampus.trentinofamiglia.map.MapManager.java

private static Bitmap writeOnMarker(Context mContext, int drawableId, String text) {
    float scale = mContext.getResources().getDisplayMetrics().density;

    Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), drawableId)
            .copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(scale * 14);// w  w w  .  j ava 2 s .  c o  m
    paint.setAntiAlias(true);
    paint.setARGB(255, 255, 255, 255);

    Canvas canvas = new Canvas(bitmap);
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2;
    float y = bitmap.getHeight() / 2 - 5;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

From source file:com.metinkale.prayerapp.vakit.WidgetService.java

private static Bitmap getIconFromMinutes(long left) {
    String text = left + "";
    Resources r = App.getContext().getResources();
    int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, r.getDisplayMetrics());
    Bitmap b = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_4444);
    Canvas c = new Canvas(b);
    Paint paint = new Paint();
    final float testTextSize = 48f;
    paint.setTextSize(testTextSize);//from  w w  w  . j av  a  2s.  c  o  m
    Rect bounds = new Rect();
    paint.getTextBounds(text.length() == 1 ? "0" + text : text, 0, text.length() == 1 ? 2 : text.length(),
            bounds);
    float desiredTextSize = testTextSize * (px * 0.9f) / bounds.width();
    paint.setTextSize(desiredTextSize);
    paint.setColor(0xFFFFFFFF);
    paint.setTextAlign(Paint.Align.CENTER);
    int yPos = (int) ((c.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    c.drawText(text, px / 2, yPos, paint);
    c.drawText(text, px / 2, yPos, paint);
    return b;
}

From source file:com.kmagic.solitaire.DrawMaster.java

/**
 * Get paint for text use/*from  w ww . j a  v a 2  s .com*/
 * @return red paint object
 */
public static Paint getTextPaint(final float fontSize, final Paint.Align align) {
    Paint paint = getPaint();
    paint.setTextSize(fontSize);
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    paint.setTextAlign(align);
    return (paint);
}

From source file:de.mrapp.android.util.BitmapUtil.java

/**
 * Creates and returns a bitmap from a specific text. The text is centered.
 *
 * @param context//from w w w .  j  a v  a 2  s . c  om
 *         The context, which should be used, as an instance of the class {@link Context}. The
 *         context may not be null
 * @param width
 *         The width of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param height
 *         The height of the bitmap, which should be created, in pixels as an {@link Integer}
 *         value
 * @param backgroundColor
 *         The background color of the bitmap, which should be created, as an {@link Integer}
 *         value
 * @param text
 *         The text, the bitmap should be created from, as an instance of the type {@link
 *         CharSequence}. The text may neither be null, nor empty
 * @param textSize
 *         The text size, which should be used, in sp as an {@link Integer} value
 * @param textColor
 *         The text color, which should be used, as an {@link Integer} value The color of the
 *         text
 * @param typeface
 *         The typeface, which should be used, as a value of the enum {@link Typeface} or null,
 *         if the default typeface should be used
 * @return The bitmap, which has been created, as an instance of the class {@link Bitmap}
 */
public static Bitmap textToBitmap(@NonNull final Context context, final int width, final int height,
        @ColorInt final int backgroundColor, @NonNull final CharSequence text, final float textSize,
        @ColorInt final int textColor, @Nullable final Typeface typeface) {
    ensureNotNull(context, "The context may not be null");
    ensureNotNull(text, "The text may not be null");
    ensureNotEmpty(text, "The text may not be empty");
    ensureAtLeast(textSize, 1, "The text size must be at least 1");
    Bitmap bitmap = colorToBitmap(width, height, backgroundColor);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(textColor);
    paint.setTextSize(textSize * getDensity(context));
    paint.setTextAlign(Align.CENTER);

    if (typeface != null) {
        paint.setTypeface(typeface);
    }

    int x = bitmap.getWidth() / 2;
    int y = (int) ((bitmap.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));
    canvas.drawText(text.toString(), x, y, paint);
    return bitmap;
}

From source file:pl.mg6.newmaps.demo.MarkersExampleActivity.java

private Bitmap prepareBitmap() {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.arrow_left);
    bitmap = bitmap.copy(Config.ARGB_8888, true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w  w  w . ja v a 2  s  .  co m*/
    paint.setColor(Color.WHITE);
    paint.setTextAlign(Align.CENTER);
    paint.setTextSize(getResources().getDimension(R.dimen.text_size));
    String text = "mg6";
    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2.0f;
    float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;
    canvas.drawText(text, x, y, paint);
    return bitmap;
}

From source file:org.cicadasong.samples.tubestatus.TubeStatus.java

protected void onDraw(Canvas canvas) {
    Paint paint = new Paint();
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setTypeface(Typeface.DEFAULT_BOLD);

    // We've centered the output vertically, so it works with the reduced canvas height in
    // widget mode.
    int y = canvas.getHeight() / 2;
    int x = canvas.getWidth() / 2;

    paint.setTypeface(Typeface.DEFAULT);
    paint.setTextSize(11);/*from  w  ww .ja va2  s.c o  m*/
    canvas.drawText(TubeLine.allLines.get(selectionIndex).name, x, y - paint.descent() - 1, paint);

    paint.setTextSize(11); // TODO dynamically adjust font size depending on length of status string?
    canvas.drawText(status, x, y + (int) -paint.ascent() + 1, paint);
}

From source file:pl.itiner.nutiteq.NutiteqMap.java

public Bitmap createMissingTileBitmap(final int tileSize, final String bitmapText, Resources res) {
    Bitmap canvasBitmap = Bitmap.createBitmap(tileSize, tileSize, Bitmap.Config.RGB_565);
    Canvas imageCanvas = new Canvas(canvasBitmap);

    Paint textPaint = new Paint();
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setTextSize(16f);/*w  w  w. j  a  v a2  s  .c o m*/

    Paint backgroundPaint = new Paint();
    backgroundPaint.setColor(Color.WHITE);
    backgroundPaint.setStrokeWidth(3);
    imageCanvas.drawRect(0, 0, tileSize, tileSize, backgroundPaint);

    imageCanvas.drawText(bitmapText, tileSize / 2, tileSize / 2, textPaint);

    BitmapDrawable finalImage = new BitmapDrawable(res, canvasBitmap);
    return finalImage.getBitmap();
}

From source file:de.treichels.hott.ui.android.html.AndroidCurveImageGenerator.java

private Bitmap getBitmap(final Curve curve, final float scale, final boolean description) {
    final boolean pitchCurve = curve.getPoint()[0].getPosition() == 0;
    final float scale1 = scale * 0.75f; // smaller images on the android
    // platform/*w w  w .java 2 s.  com*/

    final Bitmap image = Bitmap.createBitmap((int) (10 + 200 * scale1), (int) (10 + 250 * scale1),
            Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(image);

    final Paint backgroundPaint = new Paint();
    backgroundPaint.setColor(Color.WHITE);
    backgroundPaint.setStyle(Style.FILL);

    final Paint forgroundPaint = new Paint();
    forgroundPaint.setColor(Color.BLACK);
    forgroundPaint.setStyle(Style.STROKE);
    forgroundPaint.setStrokeWidth(1.0f);
    forgroundPaint.setStrokeCap(Cap.BUTT);
    forgroundPaint.setStrokeJoin(Join.ROUND);
    forgroundPaint.setStrokeMiter(0.0f);

    final Paint curvePaint = new Paint(forgroundPaint);
    curvePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    curvePaint.setStrokeWidth(2.0f);

    final Paint pointPaint = new Paint(curvePaint);
    pointPaint.setStrokeWidth(5.0f);
    pointPaint.setStyle(Style.FILL_AND_STROKE);

    final Paint helpLinePaint = new Paint(forgroundPaint);
    helpLinePaint.setColor(Color.GRAY);
    helpLinePaint.setPathEffect(new DashPathEffect(new float[] { 5.0f, 5.0f }, 2.5f));

    final Paint textPaint = new Paint(forgroundPaint);
    textPaint.setTypeface(Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD));
    textPaint.setTextSize(12.0f);
    textPaint.setTextAlign(Align.CENTER);
    textPaint.setStyle(Style.FILL);

    canvas.drawRect(0, 0, 10 + 200 * scale1, 10 + 250 * scale1, backgroundPaint);
    canvas.drawRect(5, 5, 5 + 200 * scale1, 5 + 250 * scale1, forgroundPaint);

    canvas.drawLine(5, 5 + 25 * scale1, 5 + 200 * scale1, 5 + 25 * scale1, helpLinePaint);
    canvas.drawLine(5, 5 + 225 * scale1, 5 + 200 * scale1, 5 + 225 * scale1, helpLinePaint);
    if (!pitchCurve) {
        canvas.drawLine(5, 5 + 125 * scale1, 5 + 200 * scale1, 5 + 125 * scale1, helpLinePaint);
        canvas.drawLine(5 + 100 * scale1, 5, 5 + 100 * scale1, 5 + 250 * scale1, helpLinePaint);
    }

    if (curve.getPoint() != null) {
        int numPoints = 0;
        for (final CurvePoint p : curve.getPoint()) {
            if (p.isEnabled()) {
                numPoints++;
            }
        }

        final double[] xVals = new double[numPoints];
        final double[] yVals = new double[numPoints];

        int i = 0;
        for (final CurvePoint p : curve.getPoint()) {
            if (p.isEnabled()) {
                if (i == 0) {
                    xVals[i] = pitchCurve ? 0 : -100;
                } else if (i == numPoints - 1) {
                    xVals[i] = 100;
                } else {
                    xVals[i] = p.getPosition();
                }
                yVals[i] = p.getValue();

                if (description) {
                    float x0;
                    float y0;
                    if (pitchCurve) {
                        x0 = (float) (5 + xVals[i] * 2 * scale1);
                        y0 = (float) (5 + (225 - yVals[i] * 2) * scale1);
                    } else {
                        x0 = (float) (5 + (100 + xVals[i]) * scale1);
                        y0 = (float) (5 + (125 - yVals[i]) * scale1);
                    }

                    canvas.drawPoint(x0, y0, pointPaint);
                    if (y0 < 5 + 125 * scale1) {
                        canvas.drawRect(x0 - 4, y0 + 5, x0 + 3, y0 + 18, backgroundPaint);
                        canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 + 16, textPaint);
                    } else {
                        canvas.drawRect(x0 - 4, y0 - 5, x0 + 3, y0 - 18, backgroundPaint);
                        canvas.drawText(Integer.toString(p.getNumber() + 1), x0 - 1, y0 - 7, textPaint);
                    }
                }

                i++;
            }
        }

        if (numPoints > 2 && curve.isSmoothing()) {
            final SplineInterpolator s = new SplineInterpolator();
            final PolynomialSplineFunction function = s.interpolate(xVals, yVals);

            float x0 = 5;
            float y0;
            if (pitchCurve) {
                y0 = (float) (5 + (225 - yVals[0] * 2) * scale1);
            } else {
                y0 = (float) (5 + (125 - yVals[0]) * scale1);
            }

            while (x0 < 4 + 200 * scale1) {
                final float x1 = x0 + 1;
                float y1;
                if (pitchCurve) {
                    y1 = (float) (5 + (225 - function.value((x1 - 5) / scale1 / 2) * 2) * scale1);
                } else {
                    y1 = (float) (5 + (125 - function.value((x1 - 5) / scale1 - 100)) * scale1);
                }

                canvas.drawLine(x0, y0, x1, y1, curvePaint);

                x0 = x1;
                y0 = y1;
            }
        } else {
            for (i = 0; i < numPoints - 1; i++) {
                float x0, y0, x1, y1;

                if (pitchCurve) {
                    x0 = (float) (5 + xVals[i] * 2 * scale1);
                    y0 = (float) (5 + (225 - yVals[i] * 2) * scale1);

                    x1 = (float) (5 + xVals[i + 1] * 2 * scale1);
                    y1 = (float) (5 + (225 - yVals[i + 1] * 2) * scale1);
                } else {
                    x0 = (float) (5 + (100 + xVals[i]) * scale1);
                    y0 = (float) (5 + (125 - yVals[i]) * scale1);

                    x1 = (float) (5 + (100 + xVals[i + 1]) * scale1);
                    y1 = (float) (5 + (125 - yVals[i + 1]) * scale1);
                }

                canvas.drawLine(x0, y0, x1, y1, curvePaint);
            }
        }
    }

    return image;
}

From source file:org.cocos2dx.lib.Cocos2dxBitmap.java

private static Paint newPaint(String fontName, int fontSize, int alignment) {
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);//from   w w  w  .j a  v  a2s.c  o  m
    paint.setTextSize(fontSize);
    paint.setAntiAlias(true);

    /*
     * Set type face for paint, now it support .ttf file.
     */
    if (fontName.endsWith(".ttf")) {
        try {
            //Typeface typeFace = Typeface.createFromAsset(context.getAssets(), fontName);
            Typeface typeFace = Cocos2dxTypefaces.get(context, fontName);
            paint.setTypeface(typeFace);
        } catch (Exception e) {
            Log.e("Cocos2dxBitmap", "error to create ttf type face: " + fontName);

            /*
             * The file may not find, use system font
             */
            paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
        }
    } else {
        paint.setTypeface(Typeface.create(fontName, Typeface.NORMAL));
    }

    int hAlignment = alignment & 0x0F;
    switch (hAlignment) {
    case HALIGNCENTER:
        paint.setTextAlign(Align.CENTER);
        break;

    case HALIGNLEFT:
        paint.setTextAlign(Align.LEFT);
        break;

    case HALIGNRIGHT:
        paint.setTextAlign(Align.RIGHT);
        break;

    default:
        paint.setTextAlign(Align.LEFT);
        break;
    }

    return paint;
}