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:it.andreale.mdatetimepicker.date.MonthPageAdapter.java

protected void drawText(CharSequence text, float x, float y, Canvas c) {
    String string = String.valueOf(text);
    float textX = x - (getTextWidth(string) / 2);
    float textY = y + (getTextHeight(string) / 2);
    c.drawText(string, textX, textY, mPaint);
}

From source file:com.busdrone.android.ui.VehicleMarkerRenderer.java

private Bitmap render(int color, String text) {
    TextPaint textPaint = new TextPaint();
    textPaint.setColor(Color.WHITE);
    textPaint.setStyle(Paint.Style.FILL);
    textPaint.setAntiAlias(true);/*w  ww. j  a  va  2  s  .c o m*/
    textPaint.setTextSize(mTextSize);

    Rect textBounds = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), textBounds);

    int width = mPadding + textBounds.width() + mPadding;
    int height = mPadding + textBounds.height() + mPadding;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setColor(color);
    canvas.drawRoundRect(new RectF(0, 0, width, height), mCornerRadius, mCornerRadius, paint);

    canvas.drawText(text, (width / 2f) - (textBounds.width() / 2f), (height / 2f) + (textBounds.height() / 2f),
            textPaint);

    return bitmap;
}

From source file:com.huahcoding.metrojam.BackTrackActivity.java

/**
 * This is where we can add markers or lines, add listeners or move the camera. In this case, we
 * just add a marker near Africa./*from  ww  w. j  a v  a  2  s  .com*/
 * <p>
 * This should only be called once and when we are sure that {@link #mMap} is not null.
 */
private void setUpMap() {
    mMap.getUiSettings().setZoomControlsEnabled(false);

    // Show Lima
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(chileLatLng, 15));

    mMap.setOnMapLongClickListener(this);
    mMap.setMyLocationEnabled(true);
    mMap.setTrafficEnabled(true);

    // Instantiates a new Polyline object and adds points to define a rectangle
    PolylineOptions rectOptions = new PolylineOptions();
    double[] route = RouteTestData.getChilePoints3();
    for (int i = 0; i < route.length - 1; i += 2) {
        LatLng ll = new LatLng(route[i], route[i + 1]);
        if (i > 1) {
            double lat = (route[i] + route[i - 2]) / 2.0;
            double lng = (route[i + 1] + route[i + 1 - 2]) / 2.0;
            double dist = distance(route[i], route[i + 1], route[i - 2], route[i + 1 - 2]);
            String pretty = getPrettyDistance(dist);

            LatLng ll2 = new LatLng(lat, lng);
            String strText = pretty;
            Rect boundsText = new Rect();
            Paint textPaint = new Paint();
            textPaint.setTextSize(18);
            textPaint.getTextBounds(strText, 0, strText.length(), boundsText);

            textPaint.setColor(Color.RED);

            Bitmap.Config conf = Bitmap.Config.ARGB_8888;
            Bitmap bmpText = Bitmap.createBitmap(boundsText.width() * 3, boundsText.height(), conf);

            Canvas canvasText = new Canvas(bmpText);
            canvasText.drawText(strText, canvasText.getWidth() / 2, canvasText.getHeight(), textPaint);

            MarkerOptions markerOptions = new MarkerOptions().position(ll2)
                    .icon(BitmapDescriptorFactory.fromBitmap(bmpText)).anchor(0.5f, 1);
            mMap.addMarker(markerOptions);
        }
        rectOptions.add(ll);
    }
    //         rectOptions.
    // Get back the mutable Polyline
    mMap.addPolyline(rectOptions);
}

From source file:io.plaidapp.ui.widget.CutoutTextView.java

private void createBitmap() {
    if (cutout != null && !cutout.isRecycled()) {
        cutout.recycle();//from ww w . j  a v  a2 s.c  o  m
    }
    cutout = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
    cutout.setHasAlpha(true);
    Canvas cutoutCanvas = new Canvas(cutout);
    cutoutCanvas.drawColor(foregroundColor);

    // this is the magic  Clear mode punches out the bitmap
    textPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    cutoutCanvas.drawText(text, textX, textY, textPaint);
}

From source file:com.androidmapsextensions.DefaultClusterOptionsProvider.java

private void drawText(Canvas canvas, String text, int iconSize) {
    int x = Math.round((iconSize - bounds.width()) / 2 - bounds.left - shadowOffsetX / 2);
    int y = Math.round((iconSize - bounds.height()) / 2 - bounds.top - shadowOffsetY / 2);
    canvas.drawText(text, x, y, textPaint);
}

From source file:color.kidpaint.com.kidpaintcolor.tools.implementation.TextTool.java

public void createAndSetBitmap() {
    float textDescent = mTextPaint.descent();
    float textAscent = mTextPaint.ascent();

    float upperBoxEdge = mToolPosition.y - mBoxHeight / 2.0f;
    float textHeight = textDescent - textAscent;
    mBoxHeight = textHeight * mMultilineText.length + 2 * mBoxOffset;
    mToolPosition.y = upperBoxEdge + mBoxHeight / 2.0f;

    float maxTextWidth = 0;
    for (String str : mMultilineText) {
        float textWidth = mTextPaint.measureText(str);
        if (textWidth > maxTextWidth) {
            maxTextWidth = textWidth;/*  ww  w.  j  av  a 2 s  .  c o m*/
        }
    }
    mBoxWidth = maxTextWidth + 2 * mBoxOffset;

    Bitmap bitmap = Bitmap.createBitmap((int) mBoxWidth, (int) mBoxHeight, Bitmap.Config.ARGB_8888);
    Canvas drawCanvas = new Canvas(bitmap);

    for (int i = 0; i < mMultilineText.length; i++) {
        drawCanvas.drawText(mMultilineText[i], mBoxOffset, mBoxOffset - textAscent + textHeight * i,
                mTextPaint);
    }

    mDrawingBitmap = bitmap;
}

From source file:com.hgdendi.contactslist.common.FloatingBarItemDecoration.java

private void drawTitleArea(Canvas c, int left, int right, View child, RecyclerView.LayoutParams params,
        int position) {
    final int rectBottom = child.getTop() - params.topMargin;
    c.drawRect(left, rectBottom - mTitleHeight, right, rectBottom, mBackgroundPaint);
    c.drawText(mList.get(position), child.getPaddingLeft() + mTextStartMargin,
            rectBottom - (mTitleHeight - mTextHeight) / 2 - mTextBaselineOffset, mTextPaint);
}

From source file:com.cocosw.accessory.views.layout.CollapsingTitleLayout.java

private void ensureExpandedTexture() {
    if (mExpandedTitleTexture != null)
        return;/*from   ww  w .j a v a 2 s  .  com*/

    mExpandedTitleTexture = Bitmap.createBitmap(mTextPaintBounds.width(), mTextPaintBounds.height(),
            Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(mExpandedTitleTexture);
    c.drawText(mTitleToDraw, -mTextPaintBounds.left, -mTextPaintBounds.top, mTextPaint);

    if (mTexturePaint == null) {
        // Make sure we have a paint
        mTexturePaint = new Paint();
        mTexturePaint.setAntiAlias(true);
        mTexturePaint.setFilterBitmap(true);
    }
}

From source file:com.askjeffreyliu.camera2barcode.CameraActivity.java

@Subscribe //(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MultiResultEvent event) {
    if (event != null && event.results != null && event.results.length > 0) {
        mGraphicOverlay.clear();/*from ww  w . java  2s  .c  o  m*/
        handler.removeCallbacks(runnable);

        for (int i = 0; i < event.results.length; i++) {
            final Result r = event.results[i];
            ArrayList<Point> pointArrayList = new ArrayList<>();
            for (int j = 0; j < r.getResultPoints().length; j++) {
                float x, y;
                try {
                    x = r.getResultPoints()[j].getX();
                    y = r.getResultPoints()[j].getY();
                } catch (NullPointerException e) {
                    return;
                }

                final float scaledX = x / event.width * mGraphicOverlay.getWidth();
                final float scaledY = y / event.height * mGraphicOverlay.getHeight();
                pointArrayList.add(new Point((int) scaledX, (int) scaledY));
            }
            final Rect rect = Utils.createRect(pointArrayList, r.getBarcodeFormat() == BarcodeFormat.QR_CODE);

            mGraphicOverlay.add(new GraphicOverlay.Graphic(mGraphicOverlay) {
                @Override
                public void draw(Canvas canvas) {
                    canvas.drawRect(rect, paint);
                    canvas.drawText(r.getText(), rect.left, rect.bottom, textPaint);
                }
            });

            CameraActivity.this.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    getPageSetOverlay(false);
                }
            });
            handler.postDelayed(runnable, 100);
        }
    }
}

From source file:com.squalala.tariki.custom.RadiusMarkerClusterer.java

@Override
public Marker buildClusterMarker(StaticCluster cluster, MapView mapView) {
    Marker m = new Marker(mapView);
    m.setPosition(cluster.getPosition());
    m.setInfoWindow(null);//from w  ww .  ja va2  s  .com
    m.setAnchor(mAnchorU, mAnchorV);

    if (cluster.getSize() >= 500) {
        setIcon(drawableSources[4]);
    } else if (cluster.getSize() >= 250 && cluster.getSize() < 500) {
        setIcon(drawableSources[3]);
    } else if (cluster.getSize() >= 100 && cluster.getSize() < 250) {
        setIcon(drawableSources[3]);
    } else if (cluster.getSize() >= 50 && cluster.getSize() < 100) {
        setIcon(drawableSources[2]);
    } else if (cluster.getSize() >= 10 && cluster.getSize() < 50) {
        setIcon(drawableSources[1]);
    } else if (cluster.getSize() < 10) {
        setIcon(drawableSources[0]);
    }

    Bitmap finalIcon = Bitmap.createBitmap(mClusterIcon.getWidth(), mClusterIcon.getHeight(),
            mClusterIcon.getConfig());
    Canvas iconCanvas = new Canvas(finalIcon);
    iconCanvas.drawBitmap(mClusterIcon, 0, 0, null);
    String text = "" + cluster.getSize();

    int textHeight = (int) (mTextPaint.descent() + mTextPaint.ascent());
    iconCanvas.drawText(text, mTextAnchorU * finalIcon.getWidth(),
            mTextAnchorV * finalIcon.getHeight() - textHeight / 2, mTextPaint);
    m.setIcon(new BitmapDrawable(mapView.getContext().getResources(), finalIcon));

    return m;
}