Java Code Examples for android.text.TextPaint.descent()

The following are Jave code examples for showing how to use descent() of the android.text.TextPaint class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: DMS   File: SwitchMultiButton.java   Source Code and License Vote up 6 votes
/**
 * define paints
 */
private void initPaint() {
    // round rectangle paint
    mStrokePaint = new Paint();
    mStrokePaint.setColor(mSelectedColor);
    mStrokePaint.setStyle(Paint.Style.STROKE);
    mStrokePaint.setAntiAlias(true);
    mStrokePaint.setStrokeWidth(mStrokeWidth);
    // selected paint
    mFillPaint = new Paint();
    mFillPaint.setColor(mSelectedColor);
    mFillPaint.setStyle(Paint.Style.FILL_AND_STROKE);
    mStrokePaint.setAntiAlias(true);
    // selected text paint
    mSelectedTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mSelectedTextPaint.setTextSize(mTextSize);
    mSelectedTextPaint.setColor(mSelectedColor);
    mStrokePaint.setAntiAlias(true);
    // unselected text paint
    mUnselectedTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mUnselectedTextPaint.setTextSize(mTextSize);
    mUnselectedTextPaint.setColor(0xffffffff);
    mStrokePaint.setAntiAlias(true);
    mTextHeightOffset = -(mSelectedTextPaint.ascent() + mSelectedTextPaint.descent()) * 0.5f;
}
 
Example 2
Project: DateTimePicker   File: SimpleMonthView.java   Source Code and License Vote up 6 votes
/**
 * Returns the row (0 indexed) closest to previouslyFocusedRect or center if null.
 */
private int findClosestRow(@Nullable Rect previouslyFocusedRect) {
    if (previouslyFocusedRect == null) {
        return 3;
    } else {
        int centerY = previouslyFocusedRect.centerY();

        final TextPaint p = mDayPaint;
        final int headerHeight = mMonthHeight + mDayOfWeekHeight;
        final int rowHeight = mDayHeight;

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

        centerY -= rowCenter - halfLineHeight;
        int row = Math.round(centerY / (float) rowHeight);
        final int maxDay = findDayOffset() + mDaysInMonth;
        final int maxRows = (maxDay / DAYS_IN_WEEK) - ((maxDay % DAYS_IN_WEEK == 0) ? 1 : 0);

        row = mathConstrain(row, 0, maxRows);
        return row;
    }
}
 
Example 3
Project: DateTimePicker   File: SimpleMonthView.java   Source Code and License Vote up 6 votes
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 (isLayoutRtl()) {
            colCenterRtl = mPaddedWidth - colCenter;
        } else {
            colCenterRtl = colCenter;
        }

        final String label = mDayOfWeekLabels[col];
        canvas.drawText(label, colCenterRtl, rowCenter - halfLineHeight, p);
    }
}
 
Example 4
Project: RLibrary   File: RExTextView.java   Source Code and License Vote up 6 votes
/**
         * 根据文本大小, 自动设置图片的高度
         */
        public static Drawable initDrawable(Context context, @DrawableRes int resourceId, float textSize) {
            Drawable drawable = ContextCompat.getDrawable(context, resourceId);
            int height = drawable.getIntrinsicHeight();
            int width = drawable.getIntrinsicWidth();

            TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
            textPaint.setTextSize(textSize);
            float textHeight = textPaint.descent() - textPaint.ascent(); //(int) RTextPaint.getTextHeight(textPaint);

//            if (textHeight > height) {
//                int offset = textHeight - height + textPaint.getFontMetricsInt().descent / 2;
//                InsetDrawable insetDrawable = new InsetDrawable(drawable, 0, offset, 0, 0);
//                insetDrawable.setBounds(0, 0, width, textHeight);
//                return insetDrawable;
//            } else {
//                drawable.setBounds(0, 0, width, height);
//                return drawable;
//            }

            //drawable.setBounds(0, 0, width, (int) Math.max(height, textHeight));
            drawable.setBounds(0, 0, width, (int) -textPaint.ascent()/*(int) Math.max(height, textHeight)*/);
            return drawable;
        }
 
Example 5
Project: Quran   File: JuzView.java   Source Code and License Vote up 4 votes
public JuzView(Context context, int type, String overlayText) {
  final Resources resources = context.getResources();
  final int circleColor = ContextCompat.getColor(context, R.color.accent_color);
  final int circleBackground = ContextCompat.getColor(context, R.color.accent_color_dark);

  mCirclePaint = new Paint();
  mCirclePaint.setStyle(Paint.Style.FILL);
  mCirclePaint.setColor(circleColor);
  mCirclePaint.setAntiAlias(true);

  mCircleBackgroundPaint = new Paint();
  mCircleBackgroundPaint.setStyle(Paint.Style.FILL);
  mCircleBackgroundPaint.setColor(circleBackground);
  mCircleBackgroundPaint.setAntiAlias(true);

  mOverlayText = overlayText;
  if (!TextUtils.isEmpty(mOverlayText)) {
    final int textColor = ContextCompat.getColor(context, R.color.header_background);
    final int textSize =
        resources.getDimensionPixelSize(R.dimen.juz_overlay_text_size);
    mOverlayTextPaint = new TextPaint();
    mOverlayTextPaint.setAntiAlias(true);
    mOverlayTextPaint.setColor(textColor);
    mOverlayTextPaint.setTextSize(textSize);
    mOverlayTextPaint.setTextAlign(Paint.Align.CENTER);

    final float textHeight =
        mOverlayTextPaint.descent() - mOverlayTextPaint.ascent();
    mTextOffset = (textHeight / 2) - mOverlayTextPaint.descent();
  }

  final int percentage;
  switch (type) {
    case TYPE_JUZ:
      percentage = 100;
      break;
    case TYPE_THREE_QUARTERS:
      percentage = 75;
      break;
    case TYPE_HALF:
      percentage = 50;
      break;
    case TYPE_QUARTER:
      percentage = 25;
      break;
    default:
      percentage = 0;
  }
  mPercentage = percentage;
}
 
Example 6
Project: TrendChartView   File: TrendChartView.java   Source Code and License Vote up 4 votes
private void initPaint() {
    mPathEffect = new DashPathEffect(new float[]{dp2, dp2}, 1);
    mGradeAxisPaint = new Paint();
    mGradeAxisPaint.reset();
    mGradeAxisPaint.setStyle(Paint.Style.STROKE);
    mGradeAxisPaint.setStrokeWidth(1);
    mGradeAxisPaint.setColor(isDebug ? Color.parseColor("#f00000") : Color.parseColor("#16ffffff"));
    mGradeAxisPaint.setAntiAlias(true);
    mGradeAxisPaint.setPathEffect(mPathEffect);

    mChartPaint = new Paint();
    mChartPaint.setStyle(Paint.Style.FILL);
    mChartPaint.setStrokeWidth(4);
    mChartPaint.setAntiAlias(true);

    mChartTestLinePaint = new Paint();
    mChartTestLinePaint.setStyle(Paint.Style.STROKE);
    mChartTestLinePaint.setStrokeWidth(dp2px(1));
    mChartTestLinePaint.setColor(Color.parseColor("#DF6A56"));
    mChartTestLinePaint.setAntiAlias(true);
    mChartTestLinePaint.setAlpha(0);
    if (isDebug) {
        mChartTestLinePaint.setAlpha(255);
    }

    mChartIndicatorPaint = new Paint();
    mChartIndicatorPaint.setStyle(Paint.Style.STROKE);
    mChartIndicatorPaint.setStrokeWidth(dp2px(1));

    mWhitePaint = new Paint();
    mWhitePaint.setStyle(Paint.Style.FILL);
    mWhitePaint.setColor(Color.parseColor("#ffffff"));

    mWhiteTextPaint = new TextPaint();
    mWhiteTextPaint.setColor(Color.parseColor("#ffffff"));
    mWhiteTextPaint.setTextSize(dp2px(11));
    mWhiteTextPaint.setAntiAlias(true);

    mWhiteTextPaintHeight = mWhiteTextPaint.descent() + mWhiteTextPaint.ascent();

    mDarkTextPaint = new TextPaint(mWhiteTextPaint);
    mDarkTextPaint.setAlpha(127);

    mPathIndicatorLine = new Path();
    mPathIndicatorLine.setFillType(Path.FillType.WINDING);

    mPathGradLine = new Path();
    mRectIndicator = new RectF();
}
 
Example 7
Project: DateTimePicker   File: SimpleMonthView.java   Source Code and License Vote up 4 votes
/**
 * Draws the month days.
 */
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 (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;
        final boolean isDayHighlighted = mHighlightedDay == day;
        if (isDayActivated) {
            stateMask |= StateSet.VIEW_STATE_ACTIVATED;

            // Adjust the circle to be centered on the row.
            final Paint paint = isDayHighlighted ? mDayHighlightSelectorPaint :
                    mDaySelectorPaint;
            canvas.drawCircle(colCenterRtl, rowCenter, mDaySelectorRadius, paint);
        } else if (isDayHighlighted) {
            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;
        final int dayTextColor;
        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;
        }
    }
}