android.text.TextPaint Java Examples

The following examples show how to use android.text.TextPaint. 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: BlogDateAdapter.java    From LoveTalkClient with Apache License 2.0 6 votes vote down vote up
TextView getTextView() {
	//设置TextView的样式
	AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
			ViewGroup.LayoutParams.WRAP_CONTENT, 64);
	TextView textView = new TextView(
			mContext);
	textView.setLayoutParams(lp);
	textView.setGravity(Gravity.CENTER_VERTICAL);
	//设置TextView的Padding值
	textView.setPadding(16, -10, 0, 32);
	//设置TextView的字体大小
	textView.setTextSize(14);
	//设置TextView的字体颜色
	textView.setTextColor(Color.WHITE);
	//设置字体加粗
	TextPaint txt = textView.getPaint();
	txt.setFakeBoldText(true);
	return textView;
}
 
Example #2
Source File: StyleSpan.java    From Ruisi with Apache License 2.0 6 votes vote down vote up
@Override
    public void updateDrawState(TextPaint tp) {
        if (color != AttrParser.COLOR_NONE) {
            tp.setColor(color);
        }

//        if (bgClolr >= 0) {
//            tp.bgColor = bgClolr;
//        }

        if (fontSize > 0) {
            if (fontSize > TEXT_SIZE.length) {
                fontSize = TEXT_SIZE.length;
            }
            tp.setTextSize(tp.getTextSize() * TEXT_SIZE[fontSize - 1]);
        }
    }
 
Example #3
Source File: LearnMoreTextView.java    From mollyim-android with GNU General Public License v3.0 6 votes vote down vote up
private void init() {
  setMovementMethod(LinkMovementMethod.getInstance());

  ClickableSpan clickable = new ClickableSpan() {
    @Override
    public void updateDrawState(@NonNull TextPaint ds) {
      super.updateDrawState(ds);
      ds.setUnderlineText(false);
      ds.setColor(ThemeUtil.getThemedColor(getContext(), R.attr.colorAccent));
    }

    @Override
    public void onClick(@NonNull View widget) {
      if (linkListener != null) {
        linkListener.onClick(widget);
      }
    }
  };

  link = new SpannableString(getContext().getString(R.string.LearnMoreTextView_learn_more));
  link.setSpan(clickable, 0, link.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

  visible = true;
}
 
Example #4
Source File: FuzzyTextClock.java    From AmazfitAPKs with Apache License 2.0 6 votes vote down vote up
@Override
public void init(Service service) {

    this.background = service.getResources().getColor(R.color.malvarez_background);
    this.leftFuzzyText = 160;

    this.textFont = new TextPaint(TextPaint.ANTI_ALIAS_FLAG);
    this.textFont.setTypeface(ResourceManager.getTypeFace(service.getResources(), ResourceManager.Font.BEBAS_NEUE));
    this.textFont.setTextSize(service.getResources().getDimension(R.dimen.drbourbon_fuzzytext_font_size));
    this.textFont.setColor(service.getResources().getColor(R.color.malvarez_time_colour));
    this.textFont.setTextAlign(Paint.Align.CENTER);

    hours = service.getResources().getStringArray(R.array.hours);
    phrases = service.getResources().getStringArray(R.array.phrases);
    digits = service.getResources().getStringArray(R.array.digits);

    Rect bounds = new Rect();
    String sampleTime = "12:58";
    textFont.getTextBounds(sampleTime, 0, sampleTime.length(), bounds);

    this.fontHeight = bounds.height();

}
 
Example #5
Source File: RoundedBackgroundSpan.java    From SimpleText with Apache License 2.0 6 votes vote down vote up
@Override
public void draw(@NonNull Canvas canvas, CharSequence text,
                 int start, int end, float x, int top, int y, int bottom, @NonNull Paint paint) {
  boolean isLeftEdge = x == 0.0f;

  if (backgroundColor != 0) {
    int extra1Dp = (int) (((TextPaint) paint).density * 1 + 0.5f);
    float width = paint.measureText(text.subSequence(start, end).toString());
    int newTop = (int) (bottom - paint.getFontSpacing() - paint.descent()) + 2 * extra1Dp;
    int newBottom = bottom - extra1Dp;
    int newLeft = (int) (isLeftEdge ? x : x - radius);
    int newRight = (int) (isLeftEdge ? x + width + 2 * radius : x + width + radius);
    RectF rect = new RectF(newLeft, newTop, newRight, newBottom);
    paint.setColor(backgroundColor);
    canvas.drawRoundRect(rect, radius, radius, paint);
  }

  if (textColor != 0) {
    float textX = isLeftEdge ? x + radius : x;
    paint.setColor(textColor);
    canvas.drawText(text, start, end, textX, y, paint);
  }
}
 
Example #6
Source File: ConfirmDialog.java    From fanfouapp-opensource with Apache License 2.0 6 votes vote down vote up
private void init() {
    setContentView(R.layout.dialog_confirm);

    this.mTitleView = (TextView) findViewById(R.id.title);
    final TextPaint tp = this.mTitleView.getPaint();
    tp.setFakeBoldText(true);
    this.mTitleView.setText(this.mTitle);

    this.mTextView = (TextView) findViewById(R.id.text);
    this.mTextView.setText(this.mText);

    this.mButton1 = (Button) findViewById(R.id.button1);
    this.mButton1.setOnClickListener(this);

    this.mButton2 = (Button) findViewById(R.id.button2);
    this.mButton2.setOnClickListener(this);

}
 
Example #7
Source File: TextStyleSpan.java    From Telegram-FOSS with GNU General Public License v2.0 6 votes vote down vote up
public void applyStyle(TextPaint p) {
    Typeface typeface = getTypeface();
    if (typeface != null) {
        p.setTypeface(typeface);
    }
    if ((flags & FLAG_STYLE_UNDERLINE) != 0) {
        p.setFlags(p.getFlags() | Paint.UNDERLINE_TEXT_FLAG);
    } else {
        p.setFlags(p.getFlags() &~ Paint.UNDERLINE_TEXT_FLAG);
    }
    if ((flags & FLAG_STYLE_STRIKE) != 0) {
        p.setFlags(p.getFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    } else {
        p.setFlags(p.getFlags() &~ Paint.STRIKE_THRU_TEXT_FLAG);
    }
}
 
Example #8
Source File: ForgetIphonePasswordFragment.java    From FimiX8-RE with MIT License 6 votes vote down vote up
private SpannableString getEmailVerficationSpannableString() {
    String str1 = this.mContext.getString(R.string.login_email_send_hint1);
    String str2 = this.mContext.getString(R.string.login_send_email_hint2);
    SpannableString spannableString = new SpannableString(str1 + str2);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), 0, str1.length(), 33);
    spannableString.setSpan(new ForegroundColorSpan(this.mContext.getResources().getColor(R.color.register_agreement)), str1.length() + str2.length(), str1.length() + str2.length(), 33);
    spannableString.setSpan(new ClickableSpan() {
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setColor(ForgetIphonePasswordFragment.this.getResources().getColor(R.color.register_agreement_click));
            ds.setUnderlineText(false);
        }

        public void onClick(View widget) {
        }
    }, str1.length(), str1.length() + str2.length(), 33);
    return spannableString;
}
 
Example #9
Source File: WallpaperCheckBoxView.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public WallpaperCheckBoxView(Context context, boolean check) {
    super(context);
    rect = new RectF();

    if (check) {
        drawBitmap = Bitmap.createBitmap(AndroidUtilities.dp(18), AndroidUtilities.dp(18), Bitmap.Config.ARGB_4444);
        drawCanvas = new Canvas(drawBitmap);
    }

    textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    textPaint.setTextSize(AndroidUtilities.dp(14));
    textPaint.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));

    checkPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    checkPaint.setStyle(Paint.Style.STROKE);
    checkPaint.setStrokeWidth(AndroidUtilities.dp(2));
    checkPaint.setColor(0);
    checkPaint.setStrokeCap(Paint.Cap.ROUND);
    checkPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    eraserPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    eraserPaint.setColor(0);
    eraserPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

    backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
 
Example #10
Source File: TextDecorationSpan.java    From ucar-weex-core with Apache License 2.0 6 votes vote down vote up
@Override
public void updateDrawState(TextPaint tp) {
  switch (mTextDecoration) {
    case LINETHROUGH:
      tp.setUnderlineText(false);
      tp.setStrikeThruText(true);
      break;
    case UNDERLINE:
      tp.setUnderlineText(true);
      tp.setStrikeThruText(false);
      break;
    case NONE:
      tp.setUnderlineText(false);
      tp.setStrikeThruText(false);
      break;
  }
}
 
Example #11
Source File: SuggestionStripLayoutHelper.java    From AOSP-Kayboard-7.1.2 with Apache License 2.0 6 votes vote down vote up
private static int getTextWidth(@Nullable final CharSequence text, final TextPaint paint) {
    if (TextUtils.isEmpty(text)) {
        return 0;
    }
    final int length = text.length();
    final float[] widths = new float[length];
    final int count;
    final Typeface savedTypeface = paint.getTypeface();
    try {
        paint.setTypeface(getTextTypeface(text));
        count = paint.getTextWidths(text, 0, length, widths);
    } finally {
        paint.setTypeface(savedTypeface);
    }
    int width = 0;
    for (int i = 0; i < count; i++) {
        width += Math.round(widths[i] + 0.5f);
    }
    return width;
}
 
Example #12
Source File: AboutFragment.java    From easyweather with MIT License 6 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    getActivity().setTheme(R.style.DayTheme);
    if (MyApplication.nightMode2()) {
        initNightView(R.layout.night_mode_overlay);
    }

    View view = inflater.inflate(R.layout.fragment_about, container, false);
    TextView tv = (TextView) view.findViewById(R.id.link);
    String textStr = "https://github.com/byhieg/easyweather";
    tv.setAutoLinkMask(Linkify.WEB_URLS);
    tv.setText(textStr);
    Spannable s = (Spannable) tv.getText();
    s.setSpan(new UnderlineSpan() {
        @Override
        public void updateDrawState(TextPaint ds) {
            ds.setColor(ds.linkColor);
            ds.setUnderlineText(false);
        }
    }, 0, textStr.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return view;
}
 
Example #13
Source File: TextStyleSpan.java    From Telegram with GNU General Public License v2.0 6 votes vote down vote up
public void applyStyle(TextPaint p) {
    Typeface typeface = getTypeface();
    if (typeface != null) {
        p.setTypeface(typeface);
    }
    if ((flags & FLAG_STYLE_UNDERLINE) != 0) {
        p.setFlags(p.getFlags() | Paint.UNDERLINE_TEXT_FLAG);
    } else {
        p.setFlags(p.getFlags() &~ Paint.UNDERLINE_TEXT_FLAG);
    }
    if ((flags & FLAG_STYLE_STRIKE) != 0) {
        p.setFlags(p.getFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
    } else {
        p.setFlags(p.getFlags() &~ Paint.STRIKE_THRU_TEXT_FLAG);
    }
}
 
Example #14
Source File: PhysicalWebOptInActivity.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private SpannableString getDescriptionText() {
    return SpanApplier.applySpans(
            getString(R.string.physical_web_optin_description),
            new SpanInfo("<learnmore>", "</learnmore>", new ClickableSpan() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(Intent.ACTION_VIEW,
                            Uri.parse(PHYSICAL_WEB_LEARN_MORE_URL));
                    // Add the SESSION extra to indicate we want a Chrome custom tab. This
                    // allows the help page to open in the same task as the opt-in activity so
                    // they can share a back stack.
                    String session = null;
                    intent.putExtra(EXTRA_CUSTOM_TABS_SESSION, session);
                    PhysicalWebOptInActivity.this.startActivity(intent);
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    // Color links but do not underline them.
                    ds.setColor(ds.linkColor);
                }
            }));
}
 
Example #15
Source File: IconDrawable.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
private IconDrawable(Context context, @NonNull IconState state) {
    iconState = state;
    paint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
    // We have already confirmed that a typeface exists for this icon during
    // validation, so we can ignore the null pointer warning.
    //noinspection ConstantConditions
    paint.setTypeface(Iconify.findTypefaceOf(state.icon).getTypeface(context));
    paint.setStyle(state.style);
    paint.setTextAlign(Paint.Align.CENTER);
    paint.setUnderlineText(false);
    color = state.colorStateList.getColorForState(StateSet.WILD_CARD, DEFAULT_COLOR);
    paint.setColor(color);
    updateTintFilter();
    setModulatedAlpha();
    paint.setDither(iconState.dither);
    text = String.valueOf(iconState.icon.character());
    if (SDK_INT < LOLLIPOP && iconState.bounds != null) {
        setBounds(iconState.bounds);
    }
}
 
Example #16
Source File: CurveView.java    From CurveView with GNU General Public License v3.0 5 votes vote down vote up
private int getTextOffsetY(TextPaint paint, int gravity) {
    int height = (int) (paint.getFontMetrics().descent - paint.getFontMetrics().ascent);
    int offset = (int) (paint.getFontMetrics().descent + paint.getFontMetrics().ascent) / 2;
    if ((gravity & Gravity.CENTER_VERTICAL) != 0) {
        offset += height / 2;
    } else if ((gravity & Gravity.BOTTOM) != 0) {
        offset += height;
    }
    return offset;
}
 
Example #17
Source File: FloatingLabelSpinner.java    From FloatingLabelSpinner with Apache License 2.0 5 votes vote down vote up
public FloatingLabelSpinner(Context context, AttributeSet attrs, int defStyleAttr, int mode) {
    super(context, attrs, defStyleAttr, mode);
    final int anti_alias_flag = Paint.ANTI_ALIAS_FLAG;
    labelPaint = new TextPaint(anti_alias_flag);
    dividerPaint = new Paint(anti_alias_flag);
    iconPaint = new Paint(anti_alias_flag);
    errorPaint = new TextPaint(anti_alias_flag);
    touch_slop = (short) ViewConfiguration.get(context).getScaledTouchSlop();
    init(context, attrs);
}
 
Example #18
Source File: MessageCursorAdapter.java    From fanfouapp-opensource with Apache License 2.0 5 votes vote down vote up
private void setTextStyle(final ViewHolder holder) {
    final int fontSize = getFontSize();
    holder.contentText.setTextSize(fontSize);
    holder.nameText.setTextSize(fontSize);
    holder.dateText.setTextSize(fontSize - 4);
    final TextPaint tp = holder.nameText.getPaint();
    tp.setFakeBoldText(true);

    if (this.autoLink) {
        holder.contentText.setAutoLinkMask(Linkify.ALL);
    }
}
 
Example #19
Source File: URLSpanReplacement.java    From Telegram-FOSS with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void updateDrawState(TextPaint p) {
    int color = p.getColor();
    super.updateDrawState(p);
    if (style != null) {
        style.applyStyle(p);
        p.setUnderlineText(p.linkColor == color);
    }
}
 
Example #20
Source File: QBadgeView.java    From BottomNavigationBar with Apache License 2.0 5 votes vote down vote up
private void init() {
    setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    mBadgeTextRect = new RectF();
    mBadgeBackgroundRect = new RectF();
    mDragPath = new Path();
    mBadgeCenter = new PointF();
    mDragCenter = new PointF();
    mRowBadgeCenter = new PointF();
    mControlPoint = new PointF();
    mInnertangentPoints = new ArrayList<>();
    mBadgeTextPaint = new TextPaint();
    mBadgeTextPaint.setAntiAlias(true);
    mBadgeTextPaint.setSubpixelText(true);
    mBadgeTextPaint.setFakeBoldText(true);
    mBadgeTextPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    mBadgeBackgroundPaint = new Paint();
    mBadgeBackgroundPaint.setAntiAlias(true);
    mBadgeBackgroundPaint.setStyle(Paint.Style.FILL);
    mBadgeBackgroundBorderPaint = new Paint();
    mBadgeBackgroundBorderPaint.setAntiAlias(true);
    mBadgeBackgroundBorderPaint.setStyle(Paint.Style.STROKE);
    mColorBackground = 0xFFE84E40;
    mColorBadgeText = 0xFFFFFFFF;
    mBadgeTextSize = DisplayUtil.dp2px(getContext(), 11);
    mBadgePadding = DisplayUtil.dp2px(getContext(), 5);
    mBadgeNumber = 0;
    mBadgeGravity = Gravity.END | Gravity.TOP;
    mGravityOffsetX = DisplayUtil.dp2px(getContext(), 1);
    mGravityOffsetY = DisplayUtil.dp2px(getContext(), 1);
    mFinalDragDistance = DisplayUtil.dp2px(getContext(), 90);
    mShowShadow = true;
    mDrawableBackgroundClip = false;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        setTranslationZ(1000);
    }
}
 
Example #21
Source File: SizeAdjustingTextView.java    From SizeAdjustingTextView with GNU General Public License v2.0 5 votes vote down vote up
private float findNewTextSize(int width, int height, CharSequence text) {
	TextPaint textPaint = new TextPaint(getPaint());

	float targetTextSize = textPaint.getTextSize();

	int textHeight = getTextHeight(text, textPaint, width, targetTextSize);
	while(textHeight > height && targetTextSize > mMinTextSize) {
		targetTextSize = Math.max(targetTextSize - 1, mMinTextSize);
		textHeight = getTextHeight(text, textPaint, width, targetTextSize);
	}
	return targetTextSize;
}
 
Example #22
Source File: OBSectionController.java    From GLEXP-Team-onebillion with Apache License 2.0 5 votes vote down vote up
public static float baselineOffsetForText(String tx, Typeface ty, float textsize)
{
    TextPaint tp = new TextPaint();
    tp.setTextSize(textsize);
    tp.setTypeface(ty);
    tp.setColor(Color.BLACK);
    SpannableString ss = new SpannableString(tx);
    StaticLayout sl = new StaticLayout(ss,tp,4000, Layout.Alignment.ALIGN_NORMAL,1,0,false);
    return sl.getLineBaseline(0);
}
 
Example #23
Source File: TypefaceResourceSpan.java    From md2tv with MIT License 5 votes vote down vote up
@Override
public void updateDrawState(TextPaint tp) {
    Typeface old=tp.getTypeface();
    if ( old != null && !old.isBold() && tf_.isBold() ) {
        tp.setFakeBoldText(true);
    }
    if ( old != null && !old.isItalic() && tf_.isItalic() ) {
        tp.setTextSkewX(-0.25f);
    }
    tp.setTypeface(tf_);
}
 
Example #24
Source File: HackerNewsItem.java    From materialistic with Apache License 2.0 5 votes vote down vote up
@NonNull
private SpannableString createAuthorSpannable(boolean authorLink) {
    SpannableString bySpannable = new SpannableString(AUTHOR_SEPARATOR + by);
    if (!authorLink) {
        return bySpannable;
    }
    bySpannable.setSpan(new StyleSpan(Typeface.BOLD),
            AUTHOR_SEPARATOR.length(), bySpannable.length(),
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    ClickableSpan clickableSpan = new ClickableSpan() {
        @Override
        public void onClick(View view) {
            view.getContext().startActivity(new Intent(Intent.ACTION_VIEW)
                    .setData(AppUtils.createUserUri(getBy())));
        }

        @Override
        public void updateDrawState(TextPaint ds) {
            super.updateDrawState(ds);
            ds.setUnderlineText(false);
        }
    };
    bySpannable.setSpan(clickableSpan,
            AUTHOR_SEPARATOR.length(), bySpannable.length(),
            Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
    return bySpannable;
}
 
Example #25
Source File: URLSpanBotCommand.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void updateDrawState(TextPaint ds) {
    super.updateDrawState(ds);
    if (currentType == 2) {
        ds.setColor(0xffffffff);
    } else if (currentType == 1) {
        ds.setColor(Theme.getColor(enabled ? Theme.key_chat_messageLinkOut : Theme.key_chat_messageTextOut));
    } else {
        ds.setColor(Theme.getColor(enabled ? Theme.key_chat_messageLinkIn : Theme.key_chat_messageTextIn));
    }
    ds.setUnderlineText(false);
}
 
Example #26
Source File: NewsFeedBadgeDrawable.java    From kaif-android with Apache License 2.0 5 votes vote down vote up
public NewsFeedBadgeDrawable(Resources resources) {
  this.resources = resources;
  icon = ResourcesCompat.getDrawable(resources, R.drawable.ic_notifications_white, null);

  textPaint = new TextPaint();
  textPaint.setTextSize((int) (11.0f * resources.getDisplayMetrics().density + 0.5f));
  textPaint.setColor(resources.getColor(android.R.color.white));
  textPaint.setAntiAlias(true);

  circlePaint = new Paint();
  circlePaint.setColor(0xffff0000);
  circlePaint.setAntiAlias(true);

  changeCount(0);
}
 
Example #27
Source File: IconBorderTextSpan.java    From tysq-android with GNU General Public License v3.0 5 votes vote down vote up
/**
     * draw
     *
     * @param text   完整文本
     * @param start  setSpan里设置的start
     * @param end    setSpan里设置的start
     * @param x
     * @param top    当前span所在行的上方y
     * @param y      y其实就是metric里baseline的位置
     * @param bottom 当前span所在行的下方y(包含了行间距),会和下一行的top重合
     * @param paint  使用此span的画笔
     */
    @Override
    public void draw(@NonNull Canvas canvas,
                     CharSequence text,
                     int start,
                     int end,
                     float x,
                     int top,
                     int y,
                     int bottom,
                     @NonNull Paint paint) {
        //画背景
        Paint bgPaint = new Paint();
        bgPaint.setColor(mContext.getResources().getColor(mBgColorResId));
//        bgPaint.setStyle(Paint.Style.FILL);
        bgPaint.setStyle(Paint.Style.STROKE);
        bgPaint.setAntiAlias(true);
        Paint.FontMetrics metrics = paint.getFontMetrics();

        float textHeight = metrics.descent - metrics.ascent;
        //算出背景开始画的y坐标
        float bgStartY = y + (textHeight - mBgHeight) / 2 + metrics.ascent;

        //画背景
        RectF bgRect = new RectF(x, bgStartY, x + mBgWidth, bgStartY + mBgHeight);
        canvas.drawRoundRect(bgRect, mRadius, mRadius, bgPaint);

        //把字画在背景中间
        TextPaint textPaint = new TextPaint();
//        textPaint.setColor(mContext.getResources().getColor(mTextColorResId));
        textPaint.setColor(mContext.getResources().getColor(mBgColorResId));
        textPaint.setTextSize(mTextSize);
        textPaint.setAntiAlias(true);
        textPaint.setTextAlign(Paint.Align.CENTER);  //这个只针对x有效
        Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
        float textRectHeight = fontMetrics.bottom - fontMetrics.top;
        canvas.drawText(mText, x + mBgWidth / 2, bgStartY + (mBgHeight - textRectHeight) / 2 - fontMetrics.top, textPaint);
    }
 
Example #28
Source File: AutofitTextView.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
private static int getLineCount(CharSequence text, TextPaint paint, float size, float width,
                                DisplayMetrics displayMetrics) {
    paint.setTextSize(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, size,
            displayMetrics));
    StaticLayout layout = new StaticLayout(text, paint, (int)width,
            Layout.Alignment.ALIGN_NORMAL, 1.0f, 0.0f, true);
    return layout.getLineCount();
}
 
Example #29
Source File: HelperLogMessage.java    From iGap-Android with GNU Affero General Public License v3.0 5 votes vote down vote up
private static void insertClickSpanLink(SpannableStringBuilder strBuilder, String message, final boolean isUser, final long id) {

        if (message.length() == 0) {
            return;
        }

        strBuilder.append(message);

        if (id != 0) {
            ClickableSpan clickableSpan = new ClickableSpan() {
                @Override
                public void onClick(View widget) {

                    if (isUser) {

                        if (id > 0) {
                            gotToUserRoom(id);
                        }
                    } else {
                        if (id > 0) {
                            goToRoom(id);
                        }
                    }
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    if (G.isDarkTheme) {
                        ds.linkColor = Color.parseColor(G.textTitleTheme);
                    } else {
                        ds.linkColor = Color.DKGRAY;
                    }

                    super.updateDrawState(ds);
                }
            };
            strBuilder.setSpan(clickableSpan, strBuilder.length() - message.length(), strBuilder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        }

    }
 
Example #30
Source File: URLSpanUserMention.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void updateDrawState(TextPaint ds) {
    super.updateDrawState(ds);
    if (currentType == 2) {
        ds.setColor(0xffffffff);
    } else if (currentType == 1) {
        ds.setColor(Theme.getColor(Theme.key_chat_messageLinkOut));
    } else {
        ds.setColor(Theme.getColor(Theme.key_chat_messageLinkIn));
    }

    ds.setUnderlineText(false);
}