use of android.text.TextPaint in project weiciyuan by qii.
the class DMUserListAdapter method buildHolder.
private DMViewHolder buildHolder(View convertView) {
DMViewHolder holder = new DMViewHolder();
holder.username = ViewUtility.findViewById(convertView, R.id.username);
TextPaint tp = holder.username.getPaint();
tp.setFakeBoldText(true);
holder.content = ViewUtility.findViewById(convertView, R.id.content);
holder.time = ViewUtility.findViewById(convertView, R.id.time);
holder.avatar = (TimeLineAvatarImageView) convertView.findViewById(R.id.avatar);
return holder;
}
use of android.text.TextPaint in project Genius-Android by qiujuer.
the class EditText method initHintTitleText.
private void initHintTitleText() {
if (isShowTitle()) {
// Set up a default TextPaint object
if (mTitlePaint == null) {
mTitlePaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTitlePaint.density = getResources().getDisplayMetrics().density;
mTitlePaint.setTextAlign(Paint.Align.LEFT);
mTitlePaint.setTypeface(getTypeface());
}
// Add Watcher
if (mTextWatcher == null) {
mTextWatcher = new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
checkShowTitle(s, false);
}
};
addTextChangedListener(mTextWatcher);
}
// try show
Editable editable = getEditableText();
checkShowTitle(editable, false);
} else {
if (mTextWatcher != null) {
removeTextChangedListener(mTextWatcher);
mTextWatcher = null;
}
mTitlePaint = null;
mCurTitleProperty = null;
mAnimator = null;
}
}
use of android.text.TextPaint in project MaterialEditText by rengwuxian.
the class MaterialMultiAutoCompleteTextView method onDraw.
@Override
protected void onDraw(@NonNull Canvas canvas) {
int startX = getScrollX() + (iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding));
int endX = getScrollX() + (iconRightBitmaps == null ? getWidth() : getWidth() - iconOuterWidth - iconPadding);
int lineStartY = getScrollY() + getHeight() - getPaddingBottom();
// draw the icon(s)
paint.setAlpha(255);
if (iconLeftBitmaps != null) {
Bitmap icon = iconLeftBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
int iconLeft = startX - iconPadding - iconOuterWidth + (iconOuterWidth - icon.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
canvas.drawBitmap(icon, iconLeft, iconTop, paint);
}
if (iconRightBitmaps != null) {
Bitmap icon = iconRightBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
int iconRight = endX + iconPadding + (iconOuterWidth - icon.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
canvas.drawBitmap(icon, iconRight, iconTop, paint);
}
// draw the clear button
if (hasFocus() && showClearButton && !TextUtils.isEmpty(getText()) && isEnabled()) {
paint.setAlpha(255);
int buttonLeft;
if (isRTL()) {
buttonLeft = startX;
} else {
buttonLeft = endX - iconOuterWidth;
}
Bitmap clearButtonBitmap = clearButtonBitmaps[0];
buttonLeft += (iconOuterWidth - clearButtonBitmap.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - clearButtonBitmap.getHeight()) / 2;
canvas.drawBitmap(clearButtonBitmap, buttonLeft, iconTop, paint);
}
// draw the underline
if (!hideUnderline) {
lineStartY += bottomSpacing;
if (!isInternalValid()) {
// not valid
paint.setColor(errorColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
} else if (!isEnabled()) {
// disabled
paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x44000000);
float interval = getPixel(1);
for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) {
canvas.drawRect(startX + xOffset, lineStartY, startX + xOffset + interval, lineStartY + getPixel(1), paint);
}
} else if (hasFocus()) {
// focused
paint.setColor(primaryColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
} else {
// normal
paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x1E000000);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint);
}
}
textPaint.setTextSize(bottomTextSize);
Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
float relativeHeight = -textMetrics.ascent - textMetrics.descent;
float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;
// draw the characters counter
if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
textPaint.setColor(isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
String charactersCounterText = getCharactersCounterText();
canvas.drawText(charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint);
}
// draw the bottom text
if (textLayout != null) {
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) {
// error text or helper text
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000));
canvas.save();
if (isRTL()) {
canvas.translate(endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding);
} else {
canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
}
textLayout.draw(canvas);
canvas.restore();
}
}
// draw the floating label
if (floatingLabelEnabled && !TextUtils.isEmpty(floatingLabelText)) {
textPaint.setTextSize(floatingLabelTextSize);
// calculate the text color
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction * (isEnabled() ? 1 : 0), floatingLabelTextColor != -1 ? floatingLabelTextColor : (baseColor & 0x00ffffff | 0x44000000), primaryColor));
// calculate the horizontal position
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
int floatingLabelStartX;
if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL()) {
floatingLabelStartX = (int) (endX - floatingLabelWidth);
} else if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
floatingLabelStartX = startX;
} else {
floatingLabelStartX = startX + (int) (getInnerPaddingLeft() + (getWidth() - getInnerPaddingLeft() - getInnerPaddingRight() - floatingLabelWidth) / 2);
}
// calculate the vertical position
int distance = floatingLabelPadding;
int floatingLabelStartY = (int) (innerPaddingTop + floatingLabelTextSize + floatingLabelPadding - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction) + getScrollY());
// calculate the alpha
int alpha = ((int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction * (isEnabled() ? 1 : 0) + 0.26f) * (floatingLabelTextColor != -1 ? 1 : Color.alpha(floatingLabelTextColor) / 256f)));
textPaint.setAlpha(alpha);
// draw the floating label
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, floatingLabelStartY, textPaint);
}
// draw the bottom ellipsis
if (hasFocus() && singleLineEllipsis && getScrollX() != 0) {
paint.setColor(isInternalValid() ? primaryColor : errorColor);
float startY = lineStartY + bottomSpacing;
int ellipsisStartX;
if (isRTL()) {
ellipsisStartX = endX;
} else {
ellipsisStartX = startX;
}
int signum = isRTL() ? -1 : 1;
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize * 5 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize * 9 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
}
// draw the original things
super.onDraw(canvas);
}
use of android.text.TextPaint in project MaterialEditText by rengwuxian.
the class MaterialEditText method onDraw.
@Override
protected void onDraw(@NonNull Canvas canvas) {
int startX = getScrollX() + (iconLeftBitmaps == null ? 0 : (iconOuterWidth + iconPadding)) + getPaddingLeft();
int endX = getScrollX() + (iconRightBitmaps == null ? getWidth() : getWidth() - iconOuterWidth - iconPadding) - getPaddingRight();
int lineStartY = getScrollY() + getHeight() - getPaddingBottom();
// draw the icon(s)
paint.setAlpha(255);
if (iconLeftBitmaps != null) {
Bitmap icon = iconLeftBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
int iconLeft = startX - iconPadding - iconOuterWidth + (iconOuterWidth - icon.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
canvas.drawBitmap(icon, iconLeft, iconTop, paint);
}
if (iconRightBitmaps != null) {
Bitmap icon = iconRightBitmaps[!isInternalValid() ? 3 : !isEnabled() ? 2 : hasFocus() ? 1 : 0];
int iconRight = endX + iconPadding + (iconOuterWidth - icon.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - icon.getHeight()) / 2;
canvas.drawBitmap(icon, iconRight, iconTop, paint);
}
// draw the clear button
if (hasFocus() && showClearButton && !TextUtils.isEmpty(getText()) && isEnabled()) {
paint.setAlpha(255);
int buttonLeft;
if (isRTL()) {
buttonLeft = startX;
} else {
buttonLeft = endX - iconOuterWidth;
}
Bitmap clearButtonBitmap = clearButtonBitmaps[0];
buttonLeft += (iconOuterWidth - clearButtonBitmap.getWidth()) / 2;
int iconTop = lineStartY + bottomSpacing - iconOuterHeight + (iconOuterHeight - clearButtonBitmap.getHeight()) / 2;
canvas.drawBitmap(clearButtonBitmap, buttonLeft, iconTop, paint);
}
// draw the underline
if (!hideUnderline) {
lineStartY += bottomSpacing;
if (!isInternalValid()) {
// not valid
paint.setColor(errorColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
} else if (!isEnabled()) {
// disabled
paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x44000000);
float interval = getPixel(1);
for (float xOffset = 0; xOffset < getWidth(); xOffset += interval * 3) {
canvas.drawRect(startX + xOffset, lineStartY, startX + xOffset + interval, lineStartY + getPixel(1), paint);
}
} else if (hasFocus()) {
// focused
paint.setColor(primaryColor);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(2), paint);
} else {
// normal
paint.setColor(underlineColor != -1 ? underlineColor : baseColor & 0x00ffffff | 0x1E000000);
canvas.drawRect(startX, lineStartY, endX, lineStartY + getPixel(1), paint);
}
}
textPaint.setTextSize(bottomTextSize);
Paint.FontMetrics textMetrics = textPaint.getFontMetrics();
float relativeHeight = -textMetrics.ascent - textMetrics.descent;
float bottomTextPadding = bottomTextSize + textMetrics.ascent + textMetrics.descent;
// draw the characters counter
if ((hasFocus() && hasCharactersCounter()) || !isCharactersCountValid()) {
textPaint.setColor(isCharactersCountValid() ? (baseColor & 0x00ffffff | 0x44000000) : errorColor);
String charactersCounterText = getCharactersCounterText();
canvas.drawText(charactersCounterText, isRTL() ? startX : endX - textPaint.measureText(charactersCounterText), lineStartY + bottomSpacing + relativeHeight, textPaint);
}
// draw the bottom text
if (textLayout != null) {
if (tempErrorText != null || ((helperTextAlwaysShown || hasFocus()) && !TextUtils.isEmpty(helperText))) {
// error text or helper text
textPaint.setColor(tempErrorText != null ? errorColor : helperTextColor != -1 ? helperTextColor : (baseColor & 0x00ffffff | 0x44000000));
canvas.save();
if (isRTL()) {
canvas.translate(endX - textLayout.getWidth(), lineStartY + bottomSpacing - bottomTextPadding);
} else {
canvas.translate(startX + getBottomTextLeftOffset(), lineStartY + bottomSpacing - bottomTextPadding);
}
textLayout.draw(canvas);
canvas.restore();
}
}
// draw the floating label
if (floatingLabelEnabled && !TextUtils.isEmpty(floatingLabelText)) {
textPaint.setTextSize(floatingLabelTextSize);
// calculate the text color
textPaint.setColor((Integer) focusEvaluator.evaluate(focusFraction * (isEnabled() ? 1 : 0), floatingLabelTextColor != -1 ? floatingLabelTextColor : (baseColor & 0x00ffffff | 0x44000000), primaryColor));
// calculate the horizontal position
float floatingLabelWidth = textPaint.measureText(floatingLabelText.toString());
int floatingLabelStartX;
if ((getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL()) {
floatingLabelStartX = (int) (endX - floatingLabelWidth);
} else if ((getGravity() & Gravity.LEFT) == Gravity.LEFT) {
floatingLabelStartX = startX;
} else {
floatingLabelStartX = startX + (int) (getInnerPaddingLeft() + (getWidth() - getInnerPaddingLeft() - getInnerPaddingRight() - floatingLabelWidth) / 2);
}
// calculate the vertical position
int distance = floatingLabelPadding;
int floatingLabelStartY = (int) (innerPaddingTop + floatingLabelTextSize + floatingLabelPadding - distance * (floatingLabelAlwaysShown ? 1 : floatingLabelFraction) + getScrollY());
// calculate the alpha
int alpha = ((int) ((floatingLabelAlwaysShown ? 1 : floatingLabelFraction) * 0xff * (0.74f * focusFraction * (isEnabled() ? 1 : 0) + 0.26f) * (floatingLabelTextColor != -1 ? 1 : Color.alpha(floatingLabelTextColor) / 256f)));
textPaint.setAlpha(alpha);
// draw the floating label
canvas.drawText(floatingLabelText.toString(), floatingLabelStartX, floatingLabelStartY, textPaint);
}
// draw the bottom ellipsis
if (hasFocus() && singleLineEllipsis && getScrollX() != 0) {
paint.setColor(isInternalValid() ? primaryColor : errorColor);
float startY = lineStartY + bottomSpacing;
int ellipsisStartX;
if (isRTL()) {
ellipsisStartX = endX;
} else {
ellipsisStartX = startX;
}
int signum = isRTL() ? -1 : 1;
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize * 5 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
canvas.drawCircle(ellipsisStartX + signum * bottomEllipsisSize * 9 / 2, startY + bottomEllipsisSize / 2, bottomEllipsisSize / 2, paint);
}
// draw the original things
super.onDraw(canvas);
}
use of android.text.TextPaint in project MaterialEditText by rengwuxian.
the class MaterialAutoCompleteTextView method adjustBottomLines.
/**
* @return True, if adjustments were made that require the view to be invalidated.
*/
private boolean adjustBottomLines() {
// Bail out if we have a zero width; lines will be adjusted during next layout.
if (getWidth() == 0) {
return false;
}
int destBottomLines;
textPaint.setTextSize(bottomTextSize);
if (tempErrorText != null || helperText != null) {
Layout.Alignment alignment = (getGravity() & Gravity.RIGHT) == Gravity.RIGHT || isRTL() ? Layout.Alignment.ALIGN_OPPOSITE : (getGravity() & Gravity.LEFT) == Gravity.LEFT ? Layout.Alignment.ALIGN_NORMAL : Layout.Alignment.ALIGN_CENTER;
textLayout = new StaticLayout(tempErrorText != null ? tempErrorText : helperText, textPaint, getWidth() - getBottomTextLeftOffset() - getBottomTextRightOffset() - getPaddingLeft() - getPaddingRight(), alignment, 1.0f, 0.0f, true);
destBottomLines = Math.max(textLayout.getLineCount(), minBottomTextLines);
} else {
destBottomLines = minBottomLines;
}
if (bottomLines != destBottomLines) {
getBottomLinesAnimator(destBottomLines).start();
}
bottomLines = destBottomLines;
return true;
}
Aggregations