Example usage for android.graphics Canvas drawText

List of usage examples for android.graphics Canvas drawText

Introduction

In this page you can find the example usage for android.graphics Canvas drawText.

Prototype

public void drawText(@NonNull String text, float x, float y, @NonNull Paint paint) 

Source Link

Document

Draw the text, with origin at (x,y), using the specified paint.

Usage

From source file:es.ondroid.mapclustering.MyClusterOptionsProvider.java

@Override
public ClusterOptions getClusterOptions(List<Marker> markers) {

    int markersCount = markers.size();
    BitmapDescriptor cachedIcon = cache.get(markersCount);
    if (cachedIcon != null) {
        return clusterOptions.icon(cachedIcon);
    }//from w w w.j a v a2s. co m

    Bitmap base;
    int i = 0;
    do {
        base = baseBitmaps[i];
    } while (markersCount >= forCounts[i++]);

    Bitmap bitmap = base.copy(Bitmap.Config.ARGB_8888, true);

    String text = String.valueOf(markersCount);
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2.0f;
    float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, x, y, paint);

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
    cache.put(markersCount, icon);

    return clusterOptions.icon(icon);
}

From source file:com.openvehicles.OVMS.ui.utils.DemoClusterOptionsProvider.java

@Override
public ClusterOptions getClusterOptions(List<Marker> markers) {

    int markersCount = markers.size();
    BitmapDescriptor cachedIcon = cache.get(markersCount);
    if (cachedIcon != null) {
        return clusterOptions.icon(cachedIcon);
    }//  w ww .  jav a2  s  .  co m

    Bitmap base;
    int i = 0;
    do {
        base = baseBitmaps[i];
    } while (markersCount >= forCounts[i++]);

    Bitmap bitmap = base.copy(Config.ARGB_8888, true);

    String text = String.valueOf(markersCount);
    paint.getTextBounds(text, 0, text.length(), bounds);
    float x = bitmap.getWidth() / 2.0f;
    float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, x, y, paint);

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
    cache.put(markersCount, icon);

    return clusterOptions.icon(icon);
}

From source file:com.openatk.rockapp.MyClusterOptionsProvider.java

@Override
public ClusterOptions getClusterOptions(List<Marker> markers) {
    int markersCount = markers.size();
    BitmapDescriptor cachedIcon = cache.get(markersCount);
    if (cachedIcon != null) {
        return clusterOptions.icon(cachedIcon);
    }//from www.j  a v  a2  s  .  c o m

    Bitmap base;
    int i = 0;
    do {
        base = baseBitmaps[i];
    } while (markersCount >= forCounts[i++]);

    Bitmap bitmap = base.copy(Config.ARGB_8888, true);

    String text = String.valueOf(markersCount);
    paint.getTextBounds(text, 0, text.length(), bounds);
    //float x = bitmap.getWidth() / 2.0f;
    //float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;
    float x = bitmap.getWidth() - (bitmap.getWidth() * 0.12f) - (bounds.width() / 2.0f);
    float y = -1.0f * bounds.top + (bitmap.getHeight() * 0.03f);

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, x, y, paint);

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
    cache.put(markersCount, icon);

    return clusterOptions.icon(icon);
}

From source file:com.hereastory.ui.MarkerClusteringOptionsProvider.java

@Override
public ClusterOptions getClusterOptions(List<Marker> markers) {

    int markersCount = markers.size();
    BitmapDescriptor cachedIcon = cache.get(markersCount);
    if (cachedIcon != null) {
        return clusterOptions.icon(cachedIcon);
    }/*from   w w  w  . j  av a 2 s . c o  m*/

    Bitmap base;
    int i = 0;
    do {
        base = baseBitmaps[i];
    } while (markersCount >= forCounts[i++]);

    Bitmap bitmap = base.copy(Config.ARGB_8888, true);

    String text = String.valueOf(markersCount);
    paint.getTextBounds(text, 0, text.length(), bounds);
    paint.setTextSize(40);
    float x = bitmap.getWidth() / 2.0f;
    float y = (bitmap.getHeight() - bounds.height()) / 2.0f - bounds.top;

    Canvas canvas = new Canvas(bitmap);
    canvas.drawText(text, x, y, paint);

    BitmapDescriptor icon = BitmapDescriptorFactory.fromBitmap(bitmap);
    cache.put(markersCount, icon);

    return clusterOptions.icon(icon);
}

From source file:com.irccloud.android.data.model.Avatar.java

public static Bitmap generateBitmap(String text, int textColor, int bgColor, boolean isDarkTheme, int size,
        boolean round) {
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    if (bitmap != null) {
        Canvas c = new Canvas(bitmap);
        Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
        p.setStyle(Paint.Style.FILL);

        if (isDarkTheme || !round) {
            p.setColor(bgColor);/*from  w w  w.  ja v  a 2 s  . com*/
            if (round)
                c.drawCircle(size / 2, size / 2, size / 2, p);
            else
                c.drawColor(bgColor);
        } else {
            float[] hsv = new float[3];
            Color.colorToHSV(bgColor, hsv);
            hsv[2] *= 0.8f;
            p.setColor(Color.HSVToColor(hsv));
            c.drawCircle(size / 2, size / 2, (size / 2) - 2, p);
            p.setColor(bgColor);
            c.drawCircle(size / 2, (size / 2) - 2, (size / 2) - 2, p);
        }
        TextPaint tp = new TextPaint(Paint.ANTI_ALIAS_FLAG);
        tp.setTextAlign(Paint.Align.CENTER);
        tp.setTypeface(font);
        tp.setTextSize((int) (size * 0.65));
        tp.setColor(textColor);
        if (isDarkTheme || !round) {
            c.drawText(text, size / 2, (size / 2) - ((tp.descent() + tp.ascent()) / 2), tp);
        } else {
            c.drawText(text, size / 2, (size / 2) - 4 - ((tp.descent() + tp.ascent()) / 2), tp);
        }

        return bitmap;
    } else {
        return null;
    }
}

From source file:net.simno.dmach.ui.view.PanView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawText(RIGHT, originX, originYR, textPaint);
    canvas.drawText(LEFT, originX, originYL, textPaint);

    float panOffset = pan - offset;
    canvas.drawRect(getMinX(), panOffset, getMaxX(), panOffset + rectHeight, shapePaint);
}

From source file:com.achillesrasquinha.biblegenerator.ImageGenerator.java

public Bitmap getBitmap(String title, String subtitle, String text) {
    Bitmap bitmap = Bitmap.createBitmap(mImageSize.x, mImageSize.y, Bitmap.Config.ARGB_8888);
    //convert bitmap to card view background color
    bitmap.eraseColor(ContextCompat.getColor(mContext, R.color.cardview_light_background));

    Canvas canvas = new Canvas(bitmap);

    //title//w  ww .  j  a  v  a  2 s  .  c o m
    mTextPaint.setTextSize(mSizeTitle);
    mTextPaint.setColor(mColor1);
    canvas.drawText(title, mX, mYTitle, mTextPaint);

    //subtitle
    mTextPaint.setTextSize(mSizeSubtitle);
    mTextPaint.setColor(mColor2);
    canvas.drawText(subtitle, mX, mYSubtitle, mTextPaint);

    //since text needs to be wrapped, using a StaticLayout
    mTextPaint.setColor(mColor3);
    //text size same as subtitle size
    StaticLayout layout = new StaticLayout(text, mTextPaint, mImageSize.x - 2 * mX,
            Layout.Alignment.ALIGN_NORMAL, 1.0f, mLineSpacing, false);
    //16dp padding as per material design guidelines
    canvas.translate(mX, mYSubtitle + mX);
    layout.draw(canvas);

    return bitmap;
}

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

/**
 * Creates and returns a bitmap from a specific text. The text is centered.
 *
 * @param context//w ww. ja va2  s  . com
 *         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:net.simno.dmach.ui.view.SettingView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(backgroundColor);//  w w  w. j  ava 2s .  c om
    if (!TextUtils.isEmpty(hText)) {
        canvas.drawText(hText, originX, originY, textPaint);
    }
    if (!TextUtils.isEmpty(vText)) {
        canvas.drawTextOnPath(vText, path, hOffset, vOffset, textPaint);
    }
    canvas.drawCircle(x, y, circleRadius, shapePaint);
}

From source file:com.android.cts.verifier.managedprovisioning.NfcTestActivity.java

/**
 * Creates a Bitmap image that contains red on white text with a specified margin.
 * @param text Text to be displayed in the image.
 * @return A Bitmap image with the above specification.
 *//*from   ww w  . j a  v  a2s  .  co m*/
private Bitmap createSampleImage(String text) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(TEXT_SIZE);
    Rect rect = new Rect();
    paint.getTextBounds(text, 0, text.length(), rect);
    int w = 2 * MARGIN + rect.right - rect.left;
    int h = 2 * MARGIN + rect.bottom - rect.top;
    Bitmap dest = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas();
    canvas.setBitmap(dest);
    paint.setColor(Color.WHITE);
    canvas.drawPaint(paint);
    paint.setColor(Color.RED);
    canvas.drawText(text, MARGIN - rect.left, MARGIN - rect.top, paint);
    return dest;
}