Example usage for android.text TextPaint ascent

List of usage examples for android.text TextPaint ascent

Introduction

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

Prototype

public float ascent() 

Source Link

Document

Return the distance above (negative) the baseline (ascent) based on the current typeface and text size.

Usage

From source file:com.miuhouse.yourcompany.student.view.widget.date.datepicker.SimpleMonthView.java

/**
 * Draws the month days./* w w w .ja  v  a  2 s  . com*/
 */
@SuppressWarnings("ConstantConditions")
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;

    final float rowHeight = mDayHeight;

    final float colWidth = mCellWidth;

    // Text is vertically centered within the row height.
    final float halfLineHeight = (p.ascent() + p.descent()) / 2f;
    //        int rowCenter = headerHeight + rowHeight / 2;
    float rowCenter = headerHeight + rowHeight / 2f;
    //mDaysInMonth303129
    for (int day = 1, col = findDayOffset(); day <= mDaysInMonth; day++) {
        final float colCenter = colWidth * col + colWidth / 2f;
        //final int colCenterRtl;
        final float colCenterRtl;
        if (SUtils.isLayoutRtlCompat(this)) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= SUtils.STATE_ENABLED;
        }

        final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day);
        final boolean isSelected = mActivatedDays.isSelected(day);
        final boolean isSelectedAndTogether = mActivatedDays.isSelectedAndTogether(day);
        if (isSelectedAndTogether) {
            if ((!disable && !isDisable) || isFill) {
                stateMask |= SUtils.STATE_ACTIVATED;
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
            }
        }
        if (!isSelected && isDayInActivatedRange) {

            stateMask |= SUtils.STATE_ACTIVATED;
            int bgShape = DRAW_RECT;
            if (mActivatedDays.isSingleDay()) {
                if (mActivatedDays.isStartOfMonth()) {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
                } else {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
                }
            } else if (mActivatedDays.isStartingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
            } else if (mActivatedDays.isEndingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
            }

            // Use height to constrain the protrusion of the arc
            boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator));

            float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator
                    : colWidth / 2f;
            switch (bgShape) {
            case DRAW_RECT_WITH_CURVE_ON_LEFT:

                int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF leftArcRect = new RectF(leftRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(leftArcRect.centerX(),
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            case DRAW_RECT_WITH_CURVE_ON_RIGHT:
                int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF rightArcRect = new RectF(rightRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(),
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            default:
                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;
        if (isDayToday) {
            if (mActivatedDays.isClick && isSelected) {
                final int[] stateSet = SUtils.resolveStateSet(stateMask);
                dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
            } else {
                dayTextColor = mDaySelectorPaint.getColor();
            }
        } else {
            final int[] stateSet = SUtils.resolveStateSet(stateMask);
            if (!mActivatedDays.isSingleDay()) {
                if (stateMask == 1) {
                    dayTextColor = mContext.getResources().getColor(R.color.textDarkfour);
                } else {
                    dayTextColor = mDayTextColor.getColorForState(stateSet, 0);
                }
            } else {
                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;
        }
        if (mDaysInMonth == day)
            mOnRangeClickListener.onRangeSelected(calendars, position);
    }
}

From source file:com.appeaser.sublimepickerlibrary.datepicker.SimpleMonthView.java

/**
 * Draws the month days.//w ww  . j  a v a 2s  .co  m
 */
@SuppressWarnings("ConstantConditions")
private void drawDays(Canvas canvas) {
    final TextPaint p = mDayPaint;
    final int headerHeight = mMonthHeight + mDayOfWeekHeight;
    //final int rowHeight = mDayHeight;
    final float rowHeight = mDayHeight;
    //final int colWidth = mCellWidth;
    final float colWidth = mCellWidth;

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

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

        int stateMask = 0;

        final boolean isDayEnabled = isDayEnabled(day);
        if (isDayEnabled) {
            stateMask |= SUtils.STATE_ENABLED;
        }

        final boolean isDayInActivatedRange = mActivatedDays.isValid() && mActivatedDays.isActivated(day);
        final boolean isSelected = mActivatedDays.isSelected(day);

        if (isSelected) {
            stateMask |= SUtils.STATE_ACTIVATED;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDaySelectorPaint);
        } else if (isDayInActivatedRange) {
            stateMask |= SUtils.STATE_ACTIVATED;

            int bgShape = DRAW_RECT;

            if (mActivatedDays.isSingleDay()) {
                if (mActivatedDays.isStartOfMonth()) {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
                } else {
                    bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
                }
            } else if (mActivatedDays.isStartingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_LEFT;
            } else if (mActivatedDays.isEndingDayOfRange(day)) {
                bgShape = DRAW_RECT_WITH_CURVE_ON_RIGHT;
            }

            // Use height to constrain the protrusion of the arc
            boolean constrainProtrusion = colWidth > (rowHeight - (2 * mPaddingRangeIndicator));
            float horDistFromCenter = constrainProtrusion ? rowHeight / 2f - mPaddingRangeIndicator
                    : colWidth / 2f;

            switch (bgShape) {
            case DRAW_RECT_WITH_CURVE_ON_LEFT:
                int leftRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int leftRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF leftArcRect = new RectF(leftRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, leftRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(leftArcRect, 90, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(leftArcRect.centerX(),
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            case DRAW_RECT_WITH_CURVE_ON_RIGHT:
                int rightRectArcLeft = (int) (colCenterRtl - horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl - horDistFromCenter) + 1
                        : (int) (colCenterRtl - horDistFromCenter);

                int rightRectArcRight = (int) (colCenterRtl + horDistFromCenter) % 2 == 1
                        ? (int) (colCenterRtl + horDistFromCenter) + 1
                        : (int) (colCenterRtl + horDistFromCenter);

                RectF rightArcRect = new RectF(rightRectArcLeft,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightRectArcRight,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator);

                canvas.drawArc(rightArcRect, 270, 180, true, mDayRangeSelectorPaint);

                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, rightArcRect.centerX(),
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            default:
                canvas.drawRect(new RectF(colCenterRtl - colWidth / 2f,
                        rowCenter - rowHeight / 2f + mPaddingRangeIndicator, colCenterRtl + colWidth / 2f,
                        rowCenter + rowHeight / 2f - mPaddingRangeIndicator), mDayRangeSelectorPaint);
                break;
            }
        }

        if (mTouchedItem == day) {
            stateMask |= SUtils.STATE_PRESSED;

            if (isDayEnabled) {
                canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, mDayHighlightPaint);
            }
        }

        final boolean isDayToday = mToday == day;
        final int dayTextColor;

        if (isDayToday && !isDayInActivatedRange) {
            dayTextColor = mDaySelectorPaint.getColor();
        } else {
            final int[] stateSet = SUtils.resolveStateSet(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.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 w w. j  a  va 2s  . co 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();
}