Example usage for android.text TextPaint descent

List of usage examples for android.text TextPaint descent

Introduction

In this page you can find the example usage for android.text TextPaint descent.

Prototype

public float descent() 

Source Link

Document

Return the distance below (positive) the baseline (descent) based on the current typeface and text size.

Usage

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);/*ww w.  j a v a 2s .c  o  m*/
            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:com.ecuamobi.deckwallet.util.Renderer.java

public static void printQR(final Activity context, final String addressUri) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override//from   www.j a  va  2 s .c om
        protected Bitmap doInBackground(Void... params) {
            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            int textWidth = getTextWidth(addressUri, textPaint);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            Bitmap bmp = Bitmap.createBitmap(textWidth + bitmapMargin * 2,
                    addressQrCodeBitmap.getHeight() + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int y = bitmapMargin + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(addressUri, bitmap);
            }

        }
    }.execute();

}

From source file:com.ecuamobi.deckwallet.util.Renderer.java

static void printWallet(final Activity context, final String label, final String addressUri,
        final String privateKey) {
    new AsyncTask<Void, Void, Bitmap>() {

        @Override// w  ww.j  av  a 2  s .c o m
        protected Bitmap doInBackground(Void... params) {

            TextPaint textPaint = new TextPaint();
            textPaint.setAntiAlias(true);
            textPaint.setColor(0xFF000000);
            final int bitmapMargin = 100;//big margin is to prevent possible clipping
            final int textHeight = 28;
            final int spaceBetweenQrCodes = 60;
            textPaint.setTextSize(textHeight);
            textPaint.setTextAlign(Paint.Align.CENTER);
            final int qrCodePadding = (int) (textPaint.descent() * 2);
            Rect bounds = new Rect();
            textPaint.getTextBounds(privateKey, 0, privateKey.length(), bounds);
            int textWidth = getTextWidth(privateKey, textPaint);
            ArrayList<String> labelLinesRelaxed = wrap(label, textWidth, false, textPaint);
            for (String titleLine : labelLinesRelaxed) {
                textWidth = Math.max(textWidth, getTextWidth(titleLine, textPaint));
            }
            textWidth = Math.max(textWidth, getTextWidth(addressUri, textPaint));
            QRCode privateKeyQrCode = QRCode.getMinimumQRCode(privateKey, ErrorCorrectLevel.M);
            Bitmap privateKeyQrCodeBitmap = privateKeyQrCode.createImage(textWidth);
            QRCode addressQrCode = QRCode.getMinimumQRCode(addressUri, ErrorCorrectLevel.M);
            Bitmap addressQrCodeBitmap = addressQrCode.createImage(textWidth);
            ArrayList<String> labelLines = wrap(label, textWidth, true, textPaint);
            Bitmap bmp = Bitmap.createBitmap(
                    textWidth * 2 + bitmapMargin * 2 + spaceBetweenQrCodes, privateKeyQrCodeBitmap.getHeight()
                            + textHeight * (labelLines.size() + 1) + qrCodePadding * 2 + bitmapMargin * 2,
                    Bitmap.Config.RGB_565);
            Canvas canvas = new Canvas(bmp);
            Paint paint = new Paint();
            paint.setStyle(Paint.Style.FILL);
            paint.setARGB(0xFF, 0xFF, 0xFF, 0xFF);
            paint.setAntiAlias(false);
            canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), paint);

            int centerXForAddress = bitmapMargin + textWidth / 2;
            int centerXForPrivateKey = bitmapMargin + textWidth + spaceBetweenQrCodes + textWidth / 2;
            int y = (int) (bitmapMargin - textPaint.ascent());
            for (int i = 0; i < labelLines.size(); i++) {
                canvas.drawText(labelLines.get(i), centerXForPrivateKey, y + i * textHeight, textPaint);
            }
            y = bitmapMargin + labelLines.size() * textHeight + qrCodePadding;
            Paint qrCodePaint = new Paint();
            qrCodePaint.setAntiAlias(false);
            qrCodePaint.setDither(false);
            canvas.drawBitmap(addressQrCodeBitmap, centerXForAddress - addressQrCodeBitmap.getWidth() / 2, y,
                    qrCodePaint);
            canvas.drawBitmap(privateKeyQrCodeBitmap,
                    centerXForPrivateKey - privateKeyQrCodeBitmap.getWidth() / 2, y, qrCodePaint);
            y += qrCodePadding - textPaint.ascent();
            canvas.drawText(addressUri, centerXForAddress, y + addressQrCodeBitmap.getHeight(), textPaint);
            canvas.drawText(privateKey, centerXForPrivateKey, y + privateKeyQrCodeBitmap.getHeight(),
                    textPaint);
            return bmp;
        }

        @Override
        protected void onPostExecute(final Bitmap bitmap) {
            if (bitmap != null) {
                //DEBUG
                //                    android.app.AlertDialog.Builder builder = new android.app.AlertDialog.Builder(context);
                //                    android.widget.ImageView view = new android.widget.ImageView(context);
                //                    view.setImageBitmap(bitmap);
                //                    builder.setView(view);
                //                    builder.setPositiveButton(android.R.string.ok, null);
                //                    builder.show();

                PrintHelper printHelper = new PrintHelper(context);
                printHelper.setScaleMode(PrintHelper.SCALE_MODE_FIT);
                printHelper.printBitmap(label, bitmap);
            }

        }
    }.execute();
}

From source file:com.grarak.kerneladiutor.elements.SplashView.java

private void draw(Canvas canvas, int x, int y, int radius) {
    if (radius > 0)
        canvas.drawCircle(x / 2, y / 2, radius, mPaintCircle);
    matrix.postRotate(rotate);//from  w w  w. j a  va 2s .c o m
    Bitmap iconRotate = Bitmap.createBitmap(icon, 0, 0, icon.getWidth(), icon.getHeight(), matrix, false);
    canvas.drawBitmap(iconRotate, x / 2 - iconRotate.getWidth() / 2, y / 2 - iconRotate.getHeight() / 2,
            mPaintCircle);

    TextPaint textPaint = new TextPaint();
    textPaint.setTypeface(Typeface.create(Typeface.DEFAULT, Typeface.BOLD));
    textPaint.setColor(textColor);
    textPaint.setAntiAlias(true);
    textPaint.setTextAlign(Paint.Align.CENTER);
    textPaint.setTextSize(textSize);
    float textHeight = textPaint.descent() - textPaint.ascent();
    float textOffset = (textHeight / 2) - textPaint.descent();

    canvas.drawText(getResources().getString(R.string.root_waiting), x / 2, y - textOffset - y / 4, textPaint);
}

From source file:com.raspi.chatapp.util.Notification.java

private Bitmap getLargeIcon(int bgColor, char letter, float width, boolean round) {
    Bitmap b = Bitmap.createBitmap((int) width, (int) width, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);

    RectF mInnerRectF = new RectF();
    mInnerRectF.set(0, 0, width, width);
    mInnerRectF.offset(0, 0);//from   w  w  w  .  j av a2  s .c  om

    Paint mBgPaint = new Paint();
    mBgPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBgPaint.setStyle(Paint.Style.FILL);
    mBgPaint.setColor(bgColor);

    TextPaint mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(Color.WHITE);
    mTitleTextPaint.setTextSize(width * 0.8f);

    float centerX = mInnerRectF.centerX();
    float centerY = mInnerRectF.centerY();

    int xPos = (int) centerX;
    int yPos = (int) (centerY - (mTitleTextPaint.descent() + mTitleTextPaint.ascent()) / 2);

    if (round)
        c.drawOval(mInnerRectF, mBgPaint);
    else
        c.drawRect(mInnerRectF, mBgPaint);
    c.drawText(String.valueOf(letter), xPos, yPos, mTitleTextPaint);

    return b;
}

From source file:org.ciasaboark.tacere.manager.NotificationManagerWrapper.java

private Bitmap createMarkerIcon(Drawable backgroundImage, String text, int width, int height) {

    Bitmap canvasBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    // Create a canvas, that will draw on to canvasBitmap.
    Canvas imageCanvas = new Canvas(canvasBitmap);

    // Draw the image to our canvas
    backgroundImage.draw(imageCanvas);//from   ww  w  .  j  av  a2  s.  c o m

    // Set up the paint for use with our Canvas
    TextPaint textPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG | TextPaint.LINEAR_TEXT_FLAG);
    textPaint.setTextAlign(TextPaint.Align.CENTER);
    textPaint.setTypeface(Typeface.DEFAULT);
    textPaint.setTextSize(100f);
    textPaint.setColor(context.getResources().getColor(android.R.color.white));

    int xPos = (imageCanvas.getWidth() / 2);
    int yPos = (int) ((imageCanvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2));
    Rect r = new Rect();
    textPaint.getTextBounds(text, 0, text.length(), r);
    //        yPos += (Math.abs(r.height()))/2;

    // Draw the text on top of our image
    imageCanvas.drawText(text, xPos, yPos, textPaint);

    // Combine background and text to a LayerDrawable
    LayerDrawable layerDrawable = new LayerDrawable(
            new Drawable[] { backgroundImage, new BitmapDrawable(canvasBitmap) });
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    layerDrawable.setBounds(0, 0, width, height);
    layerDrawable.draw(new Canvas(newBitmap));
    return newBitmap;
}

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);
    }// ww  w .ja v  a  2 s .c o  m
}

From source file:com.tr4android.support.extension.picker.date.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;
        if (ViewCompatUtils.isLayoutRtl(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {// www  . j a v a  2 s.com
            colCenterRtl = colCenter;
        }

        final int dayOfWeek = (col + mWeekStart) % DAYS_IN_WEEK;
        final String label = getDayOfWeekLabel(dayOfWeek);
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}

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

/**
 * Draws the month days.//from   w  w w .j  av  a2  s .  co m
 */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;

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

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl = colCenter;
        //            if (isLayoutRtl()) {
        //                colCenterRtl = mPaddedWidth - colCenter;
        //            } else {
        //                colCenterRtl = colCenter;
        //            }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        //            if (isDayEnabled) {
        //                stateMask |= StateSet.VIEW_STATE_ENABLED;
        //            }

        final boolean isDayActivated = mActivatedDay == day;
        if (isDayActivated) {
            //                stateMask |= StateSet.VIEW_STATE_ACTIVATED;

            // Adjust the circle to be centered on the row.
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
        } else if (mTouchedItem == day) {
            //                stateMask |= StateSet.VIEW_STATE_PRESSED;

            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        int dayTextColor = 0;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            //                final int[] stateSet = StateSet.get(stateMask);
            //                dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}

From source file:com.tr4android.support.extension.picker.date.SimpleMonthView.java

/**
 * Draws the month days.//  w  ww .ja  v  a 2 s.c o m
 */
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    final int rowHeight = mDayHeight;
    final int colWidth = mCellWidth;

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

    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final int colCenter = colWidth * col + colWidth / 2;
        final int colCenterRtl;
        if (ViewCompatUtils.isLayoutRtl(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int state = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        final boolean isDayActivated = mActivatedDay == day;
        if (isDayActivated) {
            state = VIEW_STATE_SELECTED;

            // Adjust the circle to be centered on the row.
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
        } else if (mTouchedItem == day) {
            state = VIEW_STATE_PRESSED;

            if (isDayEnabled) {
                // Adjust the circle to be centered on the row.
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday && !isDayActivated) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = buildState(isDayEnabled, state);
            dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
        }
        p.setColor(dayTextColor);

        canvas.drawText(mDayFormatter.format(day), colCenterRtl, rowCenter - halfLineHeight, p);

        col++;

        if (col == DAYS_IN_WEEK) {
            col = 0;
            rowCenter += rowHeight;
        }
    }
}