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:org.jraf.android.piclabel.app.form.FormActivity.java

private void drawText(Canvas canvas) {
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setAntiAlias(true);/*from ww  w  .j  a v a 2s. co m*/
    paint.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/" + getSelectedFontName()));

    int textSize = canvas.getHeight() / 35;
    paint.setTextSize(textSize);
    int margin = textSize / 5;

    // Measure date/time
    String dateTime = mEdtDateTime.getText().toString();
    Rect boundsDateTime = new Rect();
    paint.getTextBounds(dateTime, 0, dateTime.length(), boundsDateTime);

    // Measure location
    String location = mEdtLocation.getText().toString();
    Rect boundsLocation = new Rect();
    paint.getTextBounds(location, 0, location.length(), boundsLocation);

    int totalWidth = boundsDateTime.width() + textSize * 2 + boundsLocation.width();
    if (totalWidth > canvas.getWidth()) {
        // Draw on 2 lines

        // Draw a rectangle
        paint.setColor(Color.argb(180, 0, 0, 0));
        canvas.drawRect(0, 0, canvas.getWidth(), -boundsDateTime.top + boundsDateTime.bottom
                + -boundsLocation.top + boundsLocation.bottom + margin * 3, paint);

        // Draw date/time
        paint.setColor(Color.WHITE);
        canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint);

        // Draw location
        canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin,
                margin + -boundsDateTime.top + boundsDateTime.bottom + margin + -boundsLocation.top, paint);

    } else {
        // Draw on 1 line

        // Draw a rectangle
        paint.setColor(Color.argb(180, 0, 0, 0));
        canvas.drawRect(0, 0, canvas.getWidth(),
                margin + Math.max(boundsDateTime.height(), boundsLocation.height()) + margin, paint);

        // Draw date/time
        paint.setColor(Color.WHITE);
        canvas.drawText(dateTime, margin, margin + -boundsDateTime.top, paint);

        // Draw location
        canvas.drawText(location, canvas.getWidth() - boundsLocation.right - boundsLocation.left - margin,
                margin + -boundsLocation.top, paint);
    }
}

From source file:com.android.launcher3.Utilities.java

public static Bitmap getNotificationBadgeIcon(Context context, Bitmap appIcon, int number) {
    Paint paint;//from  w w w.  j  av  a  2  s  .c o m
    Paint circlePaint;
    String text = "" + String.valueOf(number);
    Bitmap canvasBitmap = Bitmap.createBitmap(appIcon.getWidth(), appIcon.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(canvasBitmap);
    paint = new Paint();
    circlePaint = new Paint();

    paint.setColor(Color.WHITE);
    paint.setTextSize(45);
    paint.setAntiAlias(true);
    paint.setTextAlign(Paint.Align.CENTER);

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);

    circlePaint.setColor(ContextCompat.getColor(context, R.color.colorPrimary));
    circlePaint.setAntiAlias(true);

    canvas.drawCircle(appIcon.getWidth() / 4, appIcon.getHeight() / 4, 45, circlePaint);
    canvas.drawText(text, appIcon.getWidth() / 4, appIcon.getHeight() / 3, paint);

    return overlay(appIcon, canvasBitmap);
}

From source file:com.andexert.calendarlistview.library.SimpleMonthView.java

private void drawMonthDayLabels(Canvas canvas) {
    int y = MONTH_HEADER_SIZE - (MONTH_DAY_LABEL_TEXT_SIZE / 2);
    int dayWidthHalf = (mWidth - mPadding * 2) / (mNumDays * 2);

    for (int i = 0; i < mNumDays; i++) {
        int calendarDay = (i + mWeekStart) % mNumDays;
        int x = (2 * i + 1) * dayWidthHalf + mPadding;
        mDayLabelCalendar.set(Calendar.DAY_OF_WEEK, calendarDay);
        String numberDay = mDateFormatSymbols.getShortWeekdays()[mDayLabelCalendar.get(Calendar.DAY_OF_WEEK)]
                .toUpperCase(Locale.getDefault());
        if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        } else if (numberDay.equals("")) {
            numberDay = "";
        }/*  ww  w  . j a  v  a  2 s .  c  om*/
        canvas.drawText(numberDay, x, y, mMonthDayLabelPaint);
    }
}

From source file:connect.app.com.connect.calendar.SimpleMonthView.java

private void drawDaysOfWeek(Canvas canvas) {
    final TextPaint p = mDayOfWeekPaint;
    final int headerHeight = mMonthHeight;
    final int rowHeight = mDayOfWeekHeight;
    final int colWidth = mCellWidth;

    // Text is vertically centered within the day of week height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    final int rowCenter = headerHeight + rowHeight / 2;

    for (int col = 0; col < DAYS_IN_WEEK; col++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl = colCenter;
        //            if (isLayoutRtl()) {
        //                colCenterRtl = mPaddedWidth - colCenter;
        //            } else {
        //                colCenterRtl = colCenter;
        //            }

        final int dayOfWeek = (col + mWeekStart) % DAYS_IN_WEEK;
        final String label = getDayOfWeekLabel(dayOfWeek);
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }//from ww  w  .  jav a  2  s . c o m
}

From source file:au.com.zacher.popularmovies.activity.layout.CollapsingTitleLayout.java

@Override
public void draw(@NonNull Canvas canvas) {
    final int saveCount = canvas.save();

    final int toolbarHeight = this.mToolbar.getHeight();
    canvas.clipRect(0, 0, canvas.getWidth(),
            interpolate(canvas.getHeight(), toolbarHeight, this.mScrollOffset));

    // Now call super and let it draw the background, etc
    super.draw(canvas);

    if (this.mTitleToDraw != null) {
        float x = this.mTextLeft;
        float y = this.mTextTop;

        final float ascent = this.mTextPaint.ascent() * this.mScale;
        final float descent = this.mTextPaint.descent() * this.mScale;
        final float h = descent - ascent;

        if (DEBUG_DRAW) {
            // Just a debug tool, which drawn a Magneta rect in the text bounds
            canvas.drawRect(this.mTextLeft, y - h + descent, this.mTextRight, y + descent, DEBUG_DRAW_PAINT);
        }/*ww w  .  j  av a 2  s .  c  om*/

        if (this.mUseTexture) {
            y = y - h + descent;
        }

        if (this.mScale != 1f) {
            canvas.scale(this.mScale, this.mScale, x, y);
        }

        if (this.mUseTexture && this.mExpandedTitleTexture != null) {
            // If we should use a texture, draw it instead of text
            canvas.drawBitmap(this.mExpandedTitleTexture, x, y, this.mTexturePaint);
        } else {
            canvas.drawText(this.mTitleToDraw, x, y, this.mTextPaint);
        }
    }

    canvas.restoreToCount(saveCount);
}

From source file:org.appspot.apprtc.util.ThumbnailsCacheManager.java

public static void LoadImage(final String url, LoadImageCallback callback, final Resources resources,
        final String displayname, final boolean rounded, boolean fromCache) {
    final LoadImageCallback mCallback = callback;
    final Handler uiHandler = new Handler();
    final int FG_COLOR = 0xFFFAFAFA;
    final String name = displayname;

    if (fromCache) {
        Bitmap bitmap = ThumbnailsCacheManager.getBitmapFromDiskCache(url);

        if (bitmap != null) {
            if (rounded) {
                RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory.create(resources, bitmap);
                roundedBitmap.setCircular(true);
                mCallback.onImageLoaded(roundedBitmap.getBitmap());
            } else {
                mCallback.onImageLoaded(bitmap);
            }/*from w w  w . j  av  a  2 s. c o  m*/
            return;
        }
    }

    AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection("GET", url, "",
            new AsyncHttpURLConnection.AsyncHttpEvents() {
                @Override
                public void onHttpError(String errorMessage) {
                    Log.d("LoadImage", errorMessage);
                }

                @Override
                public void onHttpComplete(String response) {
                    int size = 256;
                    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    final String trimmedName = name == null ? "" : name.trim();
                    drawTile(canvas, trimmedName, 0, 0, size, size);
                    ThumbnailsCacheManager.addBitmapToCache(url, bitmap);
                    onHttpComplete(bitmap);
                }

                @Override
                public void onHttpComplete(final Bitmap response) {
                    if (mCallback != null) {
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (rounded) {
                                    RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory
                                            .create(resources, response);
                                    roundedBitmap.setCircular(true);
                                    mCallback.onImageLoaded(roundedBitmap.getBitmap());
                                } else {
                                    mCallback.onImageLoaded(response);
                                }

                            }

                        });

                    }
                }

                private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top,
                        int right, int bottom) {
                    letter = letter.toUpperCase(Locale.getDefault());
                    Paint tilePaint = new Paint(), textPaint = new Paint();
                    tilePaint.setColor(tileColor);
                    textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
                    textPaint.setColor(FG_COLOR);
                    textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
                    textPaint.setTextSize((float) ((right - left) * 0.8));
                    Rect rect = new Rect();

                    canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
                    textPaint.getTextBounds(letter, 0, 1, rect);
                    float width = textPaint.measureText(letter);
                    canvas.drawText(letter, (right + left) / 2 - width / 2,
                            (top + bottom) / 2 + rect.height() / 2, textPaint);
                    return true;
                }

                private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
                    if (name != null) {
                        final String letter = getFirstLetter(name);
                        final int color = ThumbnailsCacheManager.getColorForName(name);
                        drawTile(canvas, letter, color, left, top, right, bottom);
                        return true;
                    }
                    return false;
                }

            });
    httpConnection.setBitmap();
    httpConnection.send();
}

From source file:de.uni_weimar.mheinz.androidtouchscope.display.HandleView.java

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (mIsOn) {/*  www.j  a  v  a 2s.c  o  m*/
        mShapeDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        mShapeDrawable.getPaint().setColor(mColor);
        mShapeDrawable.getPaint().setShadowLayer(2, 2, 2, Color.GRAY);
        mShapeDrawable.draw(canvas);
    } else {
        /*mShapeDrawable.getPaint().setStyle(Paint.Style.FILL);
        mShapeDrawable.getPaint().setColor(Color.WHITE);
        mShapeDrawable.getPaint().setShadowLayer(2,2,2,Color.GRAY);
        mShapeDrawable.draw(canvas);
        mShapeDrawable.getPaint().setStyle(Paint.Style.STROKE);
        mShapeDrawable.getPaint().setColor(Color.BLACK);
        mShapeDrawable.draw(canvas);*/
        mShapeDrawable.getPaint().setStyle(Paint.Style.FILL_AND_STROKE);
        mShapeDrawable.getPaint().setColor(Color.LTGRAY);
        mShapeDrawable.getPaint().setShadowLayer(2, 2, 2, Color.GRAY);
        mShapeDrawable.draw(canvas);
    }

    PointF center = getCircleCenter();
    mMainTextPaint.getTextBounds(mMainText, 0, mMainText.length(), mTextBounds);
    if (mOrientation == HandleDirection.RIGHT)
        canvas.drawText(mMainText, center.x + 5, center.y + mTextBounds.height() / 2 - 1, mMainTextPaint);
    else if (mOrientation == HandleDirection.LEFT)
        canvas.drawText(mMainText, center.x - 5, center.y + mTextBounds.height() / 2 - 2, mMainTextPaint);
    else
        canvas.drawText(mMainText, center.x, center.y + mTextBounds.height() / 2, mMainTextPaint);

    if (mPressDrawable != null) {
        if (mOrientation == HandleDirection.RIGHT) {
            mPressDrawable.setBounds(2, (int) (center.y - HANDLE_BREADTH / 2), HANDLE_BREADTH - 3,
                    (int) center.y + HANDLE_BREADTH / 2);
            mPressDrawable.draw(canvas);
        } else if (mOrientation == HandleDirection.LEFT) {
            canvas.save();
            canvas.rotate(180, HANDLE_LENGTH / 2, mHandlePos);
            mPressDrawable.setBounds(1, (int) (center.y - HANDLE_BREADTH / 2), HANDLE_BREADTH - 5,
                    (int) center.y + HANDLE_BREADTH / 2);
            mPressDrawable.draw(canvas);
            canvas.restore();
        }
    }
}

From source file:com.example.nitish.welcomapp.widgetpt.PeriodicTableView.java

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawRect(0, 0, getRight(), getBottom(), mBgPaint);
    mRect.top = (int) (mBlockSize * 1.3) + mContentRect.top + mContentOffset.y;
    mRect.left = mBlockSize * 4 + mContentRect.left + mContentOffset.x;
    mRect.bottom = mRect.top + mBlockSize * 2;
    mRect.right = mRect.left + mBlockSize * 8;
    mLegend.drawLegend(canvas, mRect);/*  w  ww .  j  av  a2  s  . c  o  m*/

    writeHeaders(canvas);
    writeTitle(canvas);

    for (PeriodicTableBlock block : mPeriodicTableBlocks) {
        findBlockPosition(block);

        if (!isBlockVisible(mRect)) {
            continue;
        }

        mBlockPaint.setColor(block.color);

        canvas.drawRect(mRect, mBlockPaint);

        canvas.drawText(block.element.symbol, mRect.left + mBlockSize / 2,
                mRect.bottom - (int) (mBlockSize / 2.8), mSymbolPaint);

        canvas.drawText(String.valueOf(block.element.number), mRect.left + mBlockSize / 20,
                mRect.top + mNumberPaint.getTextSize(), mNumberPaint);

        canvas.drawText(block.subtext, mRect.left + mBlockSize / 2, mRect.bottom - mBlockSize / 20,
                mSmallTextPaint);
    }

    if (mBlockSelected != null) {
        mSelectedPaint.setStrokeWidth(mBlockSize / 10);
        findBlockPosition(mBlockSelected);
        canvas.drawRect(mRect, mSelectedPaint);
    }

    drawEdgeEffects(canvas);
}

From source file:org.appspot.apprtc.util.ThumbnailsCacheManager.java

public static void LoadMenuImage(final String url, MenuItem menuItem, String displayname, final boolean rounded,
        Resources resources, boolean fromCache) {
    final Resources mResources = resources;
    final WeakReference<MenuItem> menuItemImage = new WeakReference<MenuItem>(menuItem);
    final Handler uiHandler = new Handler();
    final int FG_COLOR = 0xFFFAFAFA;
    final String name = displayname;

    if (fromCache) {
        Bitmap bitmap = ThumbnailsCacheManager.getBitmapFromDiskCache(url);

        if (bitmap != null) {
            if (rounded) {
                RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory.create(mResources, bitmap);
                roundedBitmap.setCircular(true);
                menuItemImage.get().setIcon(roundedBitmap);
            } else {
                menuItemImage.get().setIcon(new BitmapDrawable(bitmap));
            }//w w  w.  ja  v  a 2  s.c  o m
            return;
        }
    }

    AsyncHttpURLConnection httpConnection = new AsyncHttpURLConnection("GET", url, "",
            new AsyncHttpURLConnection.AsyncHttpEvents() {
                @Override
                public void onHttpError(String errorMessage) {
                    Log.d("LoadImage", errorMessage);
                }

                @Override
                public void onHttpComplete(String response) {
                    int size = 96;
                    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
                    Canvas canvas = new Canvas(bitmap);
                    final String trimmedName = name == null ? "" : name.trim();
                    drawTile(canvas, trimmedName, 0, 0, size, size);
                    ThumbnailsCacheManager.addBitmapToCache(url, bitmap);
                    onHttpComplete(bitmap);
                }

                @Override
                public void onHttpComplete(final Bitmap response) {
                    if (menuItemImage != null && menuItemImage.get() != null) {
                        uiHandler.post(new Runnable() {
                            @Override
                            public void run() {
                                if (rounded) {
                                    RoundedBitmapDrawable roundedBitmap = RoundedBitmapDrawableFactory
                                            .create(mResources, response);
                                    roundedBitmap.setCircular(true);
                                    menuItemImage.get().setIcon(roundedBitmap);
                                } else {
                                    menuItemImage.get().setIcon(new BitmapDrawable(response));
                                }
                            }

                        });

                    }
                }

                private boolean drawTile(Canvas canvas, String letter, int tileColor, int left, int top,
                        int right, int bottom) {
                    letter = letter.toUpperCase(Locale.getDefault());
                    Paint tilePaint = new Paint(), textPaint = new Paint();
                    tilePaint.setColor(tileColor);
                    textPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
                    textPaint.setColor(FG_COLOR);
                    textPaint.setTypeface(Typeface.create("sans-serif-light", Typeface.NORMAL));
                    textPaint.setTextSize((float) ((right - left) * 0.8));
                    Rect rect = new Rect();

                    canvas.drawRect(new Rect(left, top, right, bottom), tilePaint);
                    textPaint.getTextBounds(letter, 0, 1, rect);
                    float width = textPaint.measureText(letter);
                    canvas.drawText(letter, (right + left) / 2 - width / 2,
                            (top + bottom) / 2 + rect.height() / 2, textPaint);
                    return true;
                }

                private boolean drawTile(Canvas canvas, String name, int left, int top, int right, int bottom) {
                    if (name != null) {
                        final String letter = getFirstLetter(name);
                        final int color = ThumbnailsCacheManager.getColorForName(name);
                        drawTile(canvas, letter, color, left, top, right, bottom);
                        return true;
                    }
                    return false;
                }

            });
    httpConnection.setBitmap();
    httpConnection.send();
}

From source file:de.vanita5.twittnuker.view.ColorPickerView.java

private void drawAlphaPanel(final Canvas canvas) {

    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null)
        return;//from w w w  .ja  v a2 s. com

    final RectF rect = mAlphaRect;

    if (BORDER_WIDTH_PX > 0) {
        mBorderPaint.setColor(mBorderColor);
        canvas.drawRect(rect.left - BORDER_WIDTH_PX, rect.top - BORDER_WIDTH_PX, rect.right + BORDER_WIDTH_PX,
                rect.bottom + BORDER_WIDTH_PX, mBorderPaint);
    }

    mAlphaPattern.draw(canvas);

    final float[] hsv = new float[] { mHue, mSat, mVal };
    final int color = Color.HSVToColor(hsv);
    final int acolor = Color.HSVToColor(0, hsv);

    mAlphaShader = new LinearGradient(rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);

    mAlphaPaint.setShader(mAlphaShader);

    canvas.drawRect(rect, mAlphaPaint);

    if (mAlphaSliderText != null && mAlphaSliderText != "") {
        canvas.drawText(mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }

    final float rectWidth = 4 * mDensity / 2;

    final Point p = alphaToPoint(mAlpha);

    final RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;

    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);

}