Java Code Examples for android.text.TextPaint#setTextSize()

The following examples show how to use android.text.TextPaint#setTextSize() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AutoResizeTextView.java    From watchlist with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the text size of a clone of the view's {@link TextPaint} object
 * and uses a {@link StaticLayout} instance to measure the height of the text.
 *
 * @param source
 * @param availableWidthPixels
 * @param textSizePixels
 * @return the height of the text when placed in a view
 * with the specified width
 * and when the text has the specified size.
 */
private int getTextHeightPixels(
        CharSequence source,
        int availableWidthPixels,
        float textSizePixels) {
    // Make a copy of the original TextPaint object
    // since the object gets modified while measuring
    // (see also the docs for TextView.getPaint()
    // which states to access it read-only)
    TextPaint textPaintCopy = new TextPaint(getPaint());
    textPaintCopy.setTextSize(textSizePixels);

    // Measure using a StaticLayout instance
    StaticLayout staticLayout = new StaticLayout(
            source,
            textPaintCopy,
            availableWidthPixels,
            Layout.Alignment.ALIGN_NORMAL,
            mLineSpacingMultiplier,
            mLineSpacingExtra,
            true);

    return staticLayout.getHeight();
}
 
Example 2
Source File: RecipientEditTextView.java    From ChipsLibrary with Apache License 2.0 6 votes vote down vote up
private MoreImageSpan createMoreSpan(final int count)
{
final String moreText=String.format(mMoreItem.getText().toString(),count);
final TextPaint morePaint=new TextPaint(getPaint());
morePaint.setTextSize(mMoreItem.getTextSize());
morePaint.setColor(mMoreItem.getCurrentTextColor());
final int width=(int)morePaint.measureText(moreText)+mMoreItem.getPaddingLeft()+mMoreItem.getPaddingRight();
final int height=getLineHeight();
final Bitmap drawable=Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);
final Canvas canvas=new Canvas(drawable);
int adjustedHeight=height;
final Layout layout=getLayout();
if(layout!=null)
  adjustedHeight-=layout.getLineDescent(0);
canvas.drawText(moreText,0,moreText.length(),0,adjustedHeight,morePaint);
final Drawable result=new BitmapDrawable(getResources(),drawable);
result.setBounds(0,0,width,height);
return new MoreImageSpan(result);
}
 
Example 3
Source File: PieChartRenderer.java    From NetKnight with Apache License 2.0 6 votes vote down vote up
public PieChartRenderer(PieChart chart, ChartAnimator animator,
                        ViewPortHandler viewPortHandler) {
    super(animator, viewPortHandler);
    mChart = chart;

    mHolePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mHolePaint.setColor(Color.WHITE);
    mHolePaint.setStyle(Style.FILL);

    mTransparentCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mTransparentCirclePaint.setColor(Color.WHITE);
    mTransparentCirclePaint.setStyle(Style.FILL);
    mTransparentCirclePaint.setAlpha(105);

    mCenterTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mCenterTextPaint.setColor(Color.BLACK);
    mCenterTextPaint.setTextSize(Utils.convertDpToPixel(12f));

    mValuePaint.setTextSize(Utils.convertDpToPixel(13f));
    mValuePaint.setColor(Color.WHITE);
    mValuePaint.setTextAlign(Align.CENTER);

    mValueLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    mValueLinePaint.setStyle(Style.STROKE);
}
 
Example 4
Source File: SharedLinkCell.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public SharedLinkCell(Context context) {
    super(context);

    titleTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    titleTextPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    titleTextPaint.setColor(Theme.getColor(Theme.key_windowBackgroundWhiteBlackText));

    descriptionTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);

    titleTextPaint.setTextSize(AndroidUtilities.dp(16));
    descriptionTextPaint.setTextSize(AndroidUtilities.dp(16));

    setWillNotDraw(false);
    linkImageView = new ImageReceiver(this);
    letterDrawable = new LetterDrawable();

    checkBox = new CheckBox(context, R.drawable.round_check2);
    checkBox.setVisibility(INVISIBLE);
    checkBox.setColor(Theme.getColor(Theme.key_checkbox), Theme.getColor(Theme.key_checkboxCheck));
    addView(checkBox, LayoutHelper.createFrame(22, 22, (LocaleController.isRTL ? Gravity.RIGHT : Gravity.LEFT) | Gravity.TOP, LocaleController.isRTL ? 0 : 44, 44, LocaleController.isRTL ? 44 : 0, 0));
}
 
Example 5
Source File: b.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public b(DynamicDetailChartOld dynamicdetailchartold)
{
    c = dynamicdetailchartold;
    super();
    d = 0;
    value = 24;
    offset = d;
    f = new TextPaint(1);
    f.setColor(0x4dffffff);
    f.setTextSize(9F * mDensity);
    g = new Paint(1);
    g.setColor(0x1affffff);
    h = new Paint(1);
    h.setColor(0x33ffffff);
    h.setStrokeWidth(1.0F);
    h.setStyle(android.graphics.Paint.Style.STROKE);
}
 
Example 6
Source File: SpanFormatter.java    From NFCard with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void updateDrawState(TextPaint ds) {
	ds.setTextSize(size);
	ds.setColor(color);

	if (face != null) {
		ds.setTypeface(face);
	} else if (bold) {
		Typeface tf = ds.getTypeface();

		if (tf != null) {
			int style = tf.getStyle() | Typeface.BOLD;
			tf = Typeface.create(tf, style);
			ds.setTypeface(tf);

			style &= ~tf.getStyle();

			if ((style & Typeface.BOLD) != 0) {
				ds.setFakeBoldText(true);
			}
		}
	}
}
 
Example 7
Source File: AdCircleProgress.java    From circle-progress-ad-android with GNU General Public License v3.0 5 votes vote down vote up
protected void initPainters() {
    if (showText) {
        textPaint = new TextPaint();
        textPaint.setColor(textColor);
        textPaint.setTextSize(textSize);
        textPaint.setAntiAlias(true);

        innerBottomTextPaint = new TextPaint();
        innerBottomTextPaint.setColor(innerBottomTextColor);
        innerBottomTextPaint.setTextSize(innerBottomTextSize);
        innerBottomTextPaint.setAntiAlias(true);
    }

    finishedPaint = new Paint();

    //finishedPaint.setShader(new SweepGradient(0, 0,  Color.BLUE, Color.YELLOW));
    if (gradientColorOne != 0 && gradientColorTwo != 0) {
        finishedPaint.setShader(new LinearGradient(0, 0, 0, getHeight(), gradientColorOne, gradientColorTwo, Shader.TileMode.MIRROR));
    } else {
        finishedPaint.setColor(finishedStrokeColor);
    }

    finishedPaint.setStyle(Paint.Style.STROKE);
    finishedPaint.setAntiAlias(true);
    finishedPaint.setStrokeWidth(finishedStrokeWidth);

    unfinishedPaint = new Paint();
    unfinishedPaint.setColor(unfinishedStrokeColor);
    unfinishedPaint.setStyle(Paint.Style.STROKE);
    unfinishedPaint.setAntiAlias(true);
    unfinishedPaint.setStrokeWidth(unfinishedStrokeWidth);

    innerCirclePaint = new Paint();
    innerCirclePaint.setColor(innerBackgroundColor);
    innerCirclePaint.setAntiAlias(true);

}
 
Example 8
Source File: AbsoluteSizeSpan.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void updateMeasureState(@NonNull TextPaint ds) {
    if (mDip) {
        ds.setTextSize(mSize * ds.density);
    } else {
        ds.setTextSize(mSize);
    }
}
 
Example 9
Source File: ChatBaseCell.java    From Yahala-Messenger with MIT License 5 votes vote down vote up
private void init() {
    if (backgroundDrawableIn == null) {
        backgroundDrawableIn = getResources().getDrawable(R.drawable.msg_in);
        backgroundDrawableInSelected = getResources().getDrawable(R.drawable.msg_in_selected);
        backgroundDrawableOut = getResources().getDrawable(R.drawable.msg_out);
        backgroundDrawableOutSelected = getResources().getDrawable(R.drawable.msg_out_selected);
        backgroundMediaDrawableIn = getResources().getDrawable(R.drawable.msg_in_photo);
        backgroundMediaDrawableInSelected = getResources().getDrawable(R.drawable.msg_in_photo_selected);
        backgroundMediaDrawableOut = getResources().getDrawable(R.drawable.msg_out_photo);
        backgroundMediaDrawableOutSelected = getResources().getDrawable(R.drawable.msg_out_photo_selected);
        checkDrawable = getResources().getDrawable(R.drawable.msg_check);
        halfCheckDrawable = getResources().getDrawable(R.drawable.msg_halfcheck);
        clockDrawable = getResources().getDrawable(R.drawable.msg_clock);
        checkMediaDrawable = getResources().getDrawable(R.drawable.msg_check_w);
        halfCheckMediaDrawable = getResources().getDrawable(R.drawable.msg_halfcheck_w);
        clockMediaDrawable = getResources().getDrawable(R.drawable.msg_clock_photo);
        errorDrawable = getResources().getDrawable(R.drawable.msg_warning);
        mediaBackgroundDrawable = getResources().getDrawable(R.drawable.phototime);

        timePaintIn = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        timePaintIn.setTextSize(OSUtilities.dp(12));
        timePaintIn.setColor(0xff959595);

        timePaintOut = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        timePaintOut.setTextSize(OSUtilities.dp(12));
        timePaintOut.setColor(0xff92b1bc);

        timeMediaPaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        timeMediaPaint.setTextSize(OSUtilities.dp(12));
        timeMediaPaint.setColor(0xffffffff);

        namePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        namePaint.setTextSize(OSUtilities.dp(15));

        forwardNamePaint = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
        forwardNamePaint.setTextSize(OSUtilities.dp(14));
    }
}
 
Example 10
Source File: TypefaceSpan.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void updateMeasureState(TextPaint p) {
    if (typeface != null) {
        p.setTypeface(typeface);
    }
    if (textSize != 0) {
        p.setTextSize(textSize);
    }
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
 
Example 11
Source File: TypefaceSpan.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void updateMeasureState(TextPaint p) {
    if (typeface != null) {
        p.setTypeface(typeface);
    }
    if (textSize != 0) {
        p.setTextSize(textSize);
    }
    p.setFlags(p.getFlags() | Paint.SUBPIXEL_TEXT_FLAG);
}
 
Example 12
Source File: RoundedLetterView.java    From RoundedLetterView with Apache License 2.0 5 votes vote down vote up
private void init(AttributeSet attrs, int defStyle) {
    final TypedArray a = getContext().obtainStyledAttributes(
            attrs, R.styleable.RoundedLetterView, defStyle, 0);

    if(a.hasValue(R.styleable.RoundedLetterView_rlv_titleText)){
        mTitleText = a.getString(R.styleable.RoundedLetterView_rlv_titleText);
    }

    mTitleColor = a.getColor(R.styleable.RoundedLetterView_rlv_titleColor,DEFAULT_TITLE_COLOR);
    mBackgroundColor = a.getColor(R.styleable.RoundedLetterView_rlv_backgroundColorValue,DEFAULT_BACKGROUND_COLOR);

    mTitleSize = a.getDimension(R.styleable.RoundedLetterView_rlv_titleSize,DEFAULT_TITLE_SIZE);
    a.recycle();

    //Title TextPaint
    mTitleTextPaint = new TextPaint();
    mTitleTextPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mTitleTextPaint.setTypeface(mFont);
    mTitleTextPaint.setTextAlign(Paint.Align.CENTER);
    mTitleTextPaint.setLinearText(true);
    mTitleTextPaint.setColor(mTitleColor);
    mTitleTextPaint.setTextSize(mTitleSize);

    //Background Paint
    mBackgroundPaint = new Paint();
    mBackgroundPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    mBackgroundPaint.setStyle(Paint.Style.FILL);
    mBackgroundPaint.setColor(mBackgroundColor);

    mInnerRectF = new RectF();
}
 
Example 13
Source File: HorScrollSelectedView.java    From WidgetCase with Apache License 2.0 5 votes vote down vote up
private void initPaint() {
    mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mTextPaint.setTextSize(mTextSize);
    mTextPaint.setColor(mTextColor);
    mSelectedPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    mSelectedPaint.setTextSize(mSelectedTextSize);
    mSelectedPaint.setColor(mSelectedTextColor);
}
 
Example 14
Source File: AutoResizeTextView.java    From juicessh-performancemonitor with Apache License 2.0 5 votes vote down vote up
private int getTextHeight(CharSequence source, TextPaint paint, int width, float textSize) {
    // Update the text paint object
    paint.setTextSize(textSize);
    // Measure using a static layout
    StaticLayout layout = new StaticLayout(source, paint, width, Alignment.ALIGN_NORMAL, mSpacingMult, mSpacingAdd, true);
    return layout.getHeight();
}
 
Example 15
Source File: BookPieChartView.java    From kAndroid with Apache License 2.0 4 votes vote down vote up
private void initPaint() {
    mPieChartPaint = new Paint();
    //是否开启抗锯齿
    mPieChartPaint.setAntiAlias(true);
    //防抖动
    mPieChartPaint.setDither(true);
    //画笔样式 STROKE 只绘制图形轮廓(描边) FILL 只绘制图形内容 FILL_AND_STROKE 既绘制轮廓也绘制内容
    mPieChartPaint.setStyle(Paint.Style.FILL);
    //画笔宽度
    mPieChartPaint.setStrokeWidth(mPieChartWidth);
    ///笔刷样式
    // 当画笔样式为STROKE或FILL_OR_STROKE时,
    mPieChartPaint.setStrokeCap(Paint.Cap.SQUARE);
    // 设置笔刷的图形样式,如圆形样式Cap.ROUND,或方形样式Cap.SQUARE
    mPieChartPaint.setColor(Color.RED);

    //数字
    mDataPaint = new TextPaint();
    mDataPaint.setDither(true);
    mDataPaint.setAntiAlias(true);
    mDataPaint.setTextSize(mDataSize);
    mDataPaint.setColor(mDataColor);
    //从中间向两边绘制,不需要再次计算文字
    mDataPaint.setTextAlign(Paint.Align.CENTER);

    //单位
    mUnitPaint = new TextPaint();
    mUnitPaint.setDither(true);
    mUnitPaint.setAntiAlias(true);
    mUnitPaint.setTextSize(mUnitSize);
    mUnitPaint.setColor(mUnitColor);
    //从中间向两边绘制,不需要再次计算文字
    mUnitPaint.setTextAlign(Paint.Align.CENTER);

    mPointingPaint = new Paint();
    mPointingPaint.setDither(true);
    mPointingPaint.setAntiAlias(true);
    mPointingPaint.setColor(mPointingColor);
    //从中间向两边绘制,不需要再次计算文字
    mPointingPaint.setStrokeWidth(mPointingWidth);
}
 
Example 16
Source File: AutofitHelper.java    From DarkCalculator with MIT License 4 votes vote down vote up
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
private static void autofit(TextView view, TextPaint paint, float minTextSize, float maxTextSize,
                            int maxLines, float precision) {
    if (maxLines <= 0 || maxLines == Integer.MAX_VALUE) {
        // Don't auto-size since there's no limit on lines.
        return;
    }

    int targetWidth = view.getWidth() - view.getPaddingLeft() - view.getPaddingRight();
    if (targetWidth <= 0) {
        return;
    }

    CharSequence text = view.getText();
    TransformationMethod method = view.getTransformationMethod();
    if (method != null) {
        text = method.getTransformation(text, view);
    }

    Context context = view.getContext();
    Resources r = Resources.getSystem();
    DisplayMetrics displayMetrics;

    float size = maxTextSize;
    float high = size;
    float low = 0;

    if (context != null) {
        r = context.getResources();
    }
    displayMetrics = r.getDisplayMetrics();

    paint.set(view.getPaint());
    paint.setTextSize(size);

    if ((maxLines == 1 && paint.measureText(text, 0, text.length()) > targetWidth)
            || getLineCount(text, paint, size, targetWidth, displayMetrics) > maxLines) {
        size = getAutofitTextSize(text, paint, targetWidth, maxLines, low, high, precision,
                displayMetrics);
    }

    if (size < minTextSize) {
        size = minTextSize;
    }

    view.setTextSize(TypedValue.COMPLEX_UNIT_PX, size);
}
 
Example 17
Source File: DividerSpan.java    From Pix-Art-Messenger with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void updateDrawState(TextPaint tp) {
    tp.setTextSize(tp.getTextSize() * PROPORTION);
}
 
Example 18
Source File: TooManyCommunitiesHintCell.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
public TooManyCommunitiesHintCell(Context context) {
    super(context);

    imageView = new ImageView(context);
    imageView.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_nameMessage_threeLines), PorterDuff.Mode.MULTIPLY));

    headerTextView = new TextView(context);
    headerTextView.setTextColor(Theme.getColor(Theme.key_chats_nameMessage_threeLines));
    headerTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
    headerTextView.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    headerTextView.setGravity(Gravity.CENTER);
    addView(headerTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 52, 75, 52, 0));

    messageTextView = new TextView(context);
    messageTextView.setTextColor(Theme.getColor(Theme.key_chats_message));
    messageTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
    messageTextView.setGravity(Gravity.CENTER);
    addView(messageTextView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.LEFT, 36, 110, 36, 0));

    TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setColor(Color.WHITE);
    textPaint.setTextSize(AndroidUtilities.dp(12));
    textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    String s = "500";
    imageLayout = new FrameLayout(context) {

        RectF rect = new RectF();


        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            paint.setColor(Theme.getColor(Theme.key_windowBackgroundWhiteRedText));

            canvas.save();
            canvas.translate(getMeasuredWidth() - textPaint.measureText(s) - AndroidUtilities.dp(8), AndroidUtilities.dpf2(7f));
            rect.set(0, 0, textPaint.measureText(s), textPaint.getTextSize());
            rect.inset(-AndroidUtilities.dp(6), -AndroidUtilities.dp(3));
            float r = (textPaint.getTextSize()) / 2f + AndroidUtilities.dp(3);
            canvas.drawRoundRect(rect, r, r, paint);
            canvas.drawText(s, 0, textPaint.getTextSize() - AndroidUtilities.dpf2(2f), textPaint);
            canvas.restore();
        }
    };
    imageLayout.setWillNotDraw(false);
    imageLayout.addView(imageView, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL));
    addView(imageLayout, LayoutHelper.createFrame(LayoutHelper.WRAP_CONTENT, LayoutHelper.WRAP_CONTENT, Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 12, 0, 6));
    headerTextView.setText(LocaleController.getString("TooManyCommunities", R.string.TooManyCommunities));
    imageView.setImageResource(R.drawable.groups_limit1);
}
 
Example 19
Source File: Clock.java    From Clock-view with Apache License 2.0 4 votes vote down vote up
private void drawNumericClock(Canvas canvas) {

        if (showBorder) {
            drawCustomBorder(canvas);
        }

        if (clockBackground != null) {
            Paint paint = new Paint();
            paint.setAntiAlias(true);

            Bitmap bitmap = ((BitmapDrawable) clockBackground).getBitmap();
            RectF rectF = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);

            Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
            Canvas tCanvas = new Canvas(output);
            switch (borderStyle) {
                case rectangle:
                    tCanvas.drawRect(defaultRectF, paint);
                    break;

                case circle:
                    tCanvas.drawCircle(centerX, centerY, radius, paint);
                    break;

                case rounded_rectangle:
                    float rx = radius - (radius * (100 - borderRadiusRx)) / 100;
                    float ry = radius - (radius * (100 - borderRadiusRy)) / 100;
                    tCanvas.drawRoundRect(defaultRectF, rx, ry, paint);
                    break;
            }

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            tCanvas.drawBitmap(bitmap, null, rectF, paint);
            canvas.drawBitmap(output, null, rectF, new Paint());
        }

        TextPaint textPaint = new TextPaint();
        textPaint.setAntiAlias(true);
        textPaint.setTypeface(valuesFont);

        textPaint.setTextSize(size * 0.22f);
        textPaint.setColor(this.valuesColor);

        SpannableStringBuilder spannableString = new SpannableStringBuilder();

        int amPm = mCalendar.get(Calendar.AM_PM);
        String minute = String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.MINUTE));
        String second = String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.SECOND));

        if (this.numericShowSeconds) {
            if (this.numericFormat == NumericFormat.hour_12) {
                spannableString.append(String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.HOUR)));
                spannableString.append(":");
                spannableString.append(minute);
                spannableString.append(".");
                spannableString.append(second);
                spannableString.append(amPm == Calendar.AM ? "AM" : "PM");
                spannableString.setSpan(new RelativeSizeSpan(0.3f), spannableString.toString().length() - 2, spannableString.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // se superscript percent
            } else {
                spannableString.append(String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.HOUR_OF_DAY)));
                spannableString.append(":");
                spannableString.append(minute);
                spannableString.append(".");
                spannableString.append(second);
            }
        } else {
            if (this.numericFormat == NumericFormat.hour_12) {
                spannableString.append(String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.HOUR)));
                spannableString.append(":");
                spannableString.append(minute);
                spannableString.append(amPm == Calendar.AM ? "AM" : "PM");
                spannableString.setSpan(new RelativeSizeSpan(0.4f), spannableString.toString().length() - 2, spannableString.toString().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // se superscript percent
            } else {
                spannableString.append(String.format(Locale.getDefault(), "%02d", mCalendar.get(Calendar.HOUR_OF_DAY)));
                spannableString.append(":");
                spannableString.append(minute);
            }
        }

        StaticLayout layout = new StaticLayout(spannableString, textPaint, canvas.getWidth(), Layout.Alignment.ALIGN_CENTER, 1, 1, true);
        canvas.translate(centerX - layout.getWidth() / 2, centerY - layout.getHeight() / 2);
        layout.draw(canvas);

    }
 
Example 20
Source File: FabOverlapTextView.java    From android-proguards with Apache License 2.0 4 votes vote down vote up
private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FabOverlapTextView);

    float defaultTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
            DEFAULT_TEXT_SIZE_SP, getResources().getDisplayMetrics());

    setFabOverlapGravity(a.getInt(R.styleable.FabOverlapTextView_fabGravity,
            Gravity.BOTTOM | Gravity.RIGHT));
    setFabOverlapHeight(a.getDimensionPixelSize(R.styleable
            .FabOverlapTextView_fabOverlayHeight, 0));
    setFabOverlapWidth(a.getDimensionPixelSize(R.styleable
            .FabOverlapTextView_fabOverlayWidth, 0));

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textAppearance)) {
        final int textAppearance = a.getResourceId(
                R.styleable.FabOverlapTextView_android_textAppearance,
                android.R.style.TextAppearance);
        TypedArray atp = getContext().obtainStyledAttributes(textAppearance,
                R.styleable.FontTextAppearance);
        paint.setColor(atp.getColor(R.styleable.FontTextAppearance_android_textColor,
                Color.BLACK));
        paint.setTextSize(atp.getDimensionPixelSize(
                R.styleable.FontTextAppearance_android_textSize, (int) defaultTextSize));
        if (atp.hasValue(R.styleable.FontTextAppearance_font)) {
            paint.setTypeface(FontUtil.get(getContext(),
                    atp.getString(R.styleable.FontTextAppearance_font)));
        }
        atp.recycle();
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_font)) {
        setFont(a.getString(R.styleable.FabOverlapTextView_font));
    }

    if (a.hasValue(R.styleable.FabOverlapTextView_android_textColor)) {
        setTextColor(a.getColor(R.styleable.FabOverlapTextView_android_textColor, 0));
    }
    if (a.hasValue(R.styleable.FabOverlapTextView_android_textSize)) {
        setTextSize(a.getDimensionPixelSize(R.styleable.FabOverlapTextView_android_textSize,
                (int) defaultTextSize));
    }

    lineHeightHint = a.getDimensionPixelSize(R.styleable.FabOverlapTextView_lineHeightHint, 0);
    unalignedTopPadding = getPaddingTop();
    unalignedBottomPadding = getPaddingBottom();

    breakStrategy = a.getInt(R.styleable.FabOverlapTextView_android_breakStrategy,
            Layout.BREAK_STRATEGY_BALANCED);

    a.recycle();
}