下面列出了android.widget.TextView#getPaddingLeft ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void chooseSize(PopupWindow pop, CharSequence text, TextView tv) {
int wid = tv.getPaddingLeft() + tv.getPaddingRight();
int ht = tv.getPaddingTop() + tv.getPaddingBottom();
/*
* Figure out how big the text would be if we laid it out to the
* full width of this view minus the border.
*/
int cap = getWidth() - wid;
if (cap < 0) {
cap = 200; // We must not be measured yet -- setFrame() will fix it.
}
Layout l = new StaticLayout(text, tv.getPaint(), cap,
Layout.Alignment.ALIGN_NORMAL, 1, 0, true);
float max = 0;
for (int i = 0; i < l.getLineCount(); i++) {
max = Math.max(max, l.getLineWidth(i));
}
/*
* Now set the popup size to be big enough for the text plus the border.
*/
pop.setWidth(wid + (int) Math.ceil(max));
pop.setHeight(ht + l.getHeight());
}
@Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
TextView author = getAuthor();
targetTextSize = author.getTextSize();
targetTextColors = author.getTextColors();
targetPadding = new Rect(author.getPaddingLeft(),
author.getPaddingTop(),
author.getPaddingRight(),
author.getPaddingBottom());
if (IntentUtil.hasAll(intent,
IntentUtil.TEXT_COLOR, IntentUtil.FONT_SIZE, IntentUtil.PADDING)) {
author.setTextColor(intent.getIntExtra(IntentUtil.TEXT_COLOR, Color.BLACK));
float textSize = intent.getFloatExtra(IntentUtil.FONT_SIZE, targetTextSize);
author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
Rect padding = intent.getParcelableExtra(IntentUtil.PADDING);
author.setPadding(padding.left, padding.top, padding.right, padding.bottom);
}
}
public static float GetLetterPositionX(@NonNull TextView aView, int aLetterIndex, boolean aClamp) {
Layout layout = aView.getLayout();
if (layout == null) {
return 0;
}
float x = layout.getPrimaryHorizontal(aLetterIndex);
x += aView.getPaddingLeft();
x -= aView.getScrollX();
if (aClamp && x > (aView.getMeasuredWidth() - aView.getPaddingRight())) {
x = aView.getMeasuredWidth() - aView.getPaddingRight();
}
if (aClamp && x < aView.getPaddingLeft()) {
x = aView.getPaddingLeft();
}
return x;
}
@Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
TextView author = getAuthor();
targetTextSize = author.getTextSize();
targetTextColors = author.getTextColors();
targetPadding = new Rect(author.getPaddingLeft(),
author.getPaddingTop(),
author.getPaddingRight(),
author.getPaddingBottom());
if (IntentUtil.INSTANCE.hasAll(intent,
IntentUtil.INSTANCE.getTEXT_COLOR(), IntentUtil.INSTANCE.getFONT_SIZE(), IntentUtil.INSTANCE.getPADDING())) {
author.setTextColor(intent.getIntExtra(IntentUtil.INSTANCE.getTEXT_COLOR(), Color.BLACK));
float textSize = intent.getFloatExtra(IntentUtil.INSTANCE.getFONT_SIZE(), targetTextSize);
author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
Rect padding = intent.getParcelableExtra(IntentUtil.INSTANCE.getPADDING());
author.setPadding(padding.left, padding.top, padding.right, padding.bottom);
}
}
private static void onCenterDraw(TextView view, Canvas canvas, Drawable drawable, int gravity) {
int drawablePadding = view.getCompoundDrawablePadding();
int ratio = 1;
float total;
switch (gravity) {
case Gravity.RIGHT:
ratio = -1;
case Gravity.LEFT:
total = view.getPaint().measureText(view.getText().toString()) + drawable.getIntrinsicWidth() + drawablePadding + view.getPaddingLeft() + view.getPaddingRight();
canvas.translate(ratio * (view.getWidth() - total) / 2, 0);
break;
case Gravity.BOTTOM:
ratio = -1;
case Gravity.TOP:
Paint.FontMetrics fontMetrics0 = view.getPaint().getFontMetrics();
total = fontMetrics0.descent - fontMetrics0.ascent + drawable.getIntrinsicHeight() + drawablePadding + view.getPaddingTop() + view.getPaddingBottom();
canvas.translate(0, ratio * (view.getHeight() - total) / 2);
break;
}
}
@Override
public void onSharedElementStart(List<String> sharedElementNames,
List<View> sharedElements,
List<View> sharedElementSnapshots) {
TextView author = getAuthor();
targetTextSize = author.getTextSize();
targetTextColors = author.getTextColors();
targetPadding = new Rect(author.getPaddingLeft(),
author.getPaddingTop(),
author.getPaddingRight(),
author.getPaddingBottom());
if (IntentUtil.hasAll(intent,
IntentUtil.TEXT_COLOR, IntentUtil.FONT_SIZE, IntentUtil.PADDING)) {
author.setTextColor(intent.getIntExtra(IntentUtil.TEXT_COLOR, Color.BLACK));
float textSize = intent.getFloatExtra(IntentUtil.FONT_SIZE, targetTextSize);
author.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
Rect padding = intent.getParcelableExtra(IntentUtil.PADDING);
author.setPadding(padding.left, padding.top, padding.right, padding.bottom);
}
}
public static float a(TextView tv) {
float f = 0.0F;
if (tv != null) {
float f1 = 0.0f;
CharSequence text = tv.getText();
if (!TextUtils.isEmpty(text)) {
f1 = tv.getPaint().measureText(text.toString());
}
int i = tv.getPaddingLeft();
int j = tv.getPaddingRight();
f = f1 + (float) (j + i);
}
return f;
}
@Override
public void dispatchMessage(Message msg) {
if (msg.what == SET_BOUNDS) {
//noinspection unchecked
Pair<Drawable, TextView> pair = (Pair<Drawable, TextView>) msg.obj;
Drawable drawable = pair.first;
TextView textView = pair.second;
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
drawable.setBounds(0, 0, width, width / 2);
}
}
/**
* 判断TextView的内容宽度是否超出其可用宽度
*
* @param tv
* @return
*/
public static boolean isOverFlowed(TextView tv, int maxWidth) {
int availableWidth = maxWidth - tv.getPaddingLeft() - tv.getPaddingRight();
Paint textViewPaint = tv.getPaint();
float textWidth = textViewPaint.measureText(tv.getText().toString());
if (textWidth > availableWidth) {
return true;
} else {
return false;
}
}
public void layoutImportantNotice(final View importantNoticeStrip,
final String importantNoticeTitle) {
final TextView titleView = (TextView)importantNoticeStrip.findViewById(
R.id.important_notice_title);
final int width = titleView.getWidth() - titleView.getPaddingLeft()
- titleView.getPaddingRight();
titleView.setTextColor(mColorAutoCorrect);
titleView.setText(importantNoticeTitle); // TextView.setText() resets text scale x to 1.0.
final float titleScaleX = getTextScaleX(importantNoticeTitle, width, titleView.getPaint());
titleView.setTextScaleX(titleScaleX);
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
@NonNull
private String autoSplitText(final TextView tv) {
final String rawText = tv.getText().toString(); //原始文本
final Paint tvPaint = tv.getPaint(); //paint,包含字体等信息
final float tvWidth = tv.getWidth() - tv.getPaddingLeft() - tv.getPaddingRight(); //控件可用宽度
//将原始文本按行拆分
String [] rawTextLines = rawText.replaceAll("\r", "").split("\n");
StringBuilder sbNewText = new StringBuilder();
for (String rawTextLine : rawTextLines) {
if (tvPaint.measureText(rawTextLine) <= tvWidth) {
//如果整行宽度在控件可用宽度之内,就不处理了
sbNewText.append(rawTextLine);
} else {
//如果整行宽度超过控件可用宽度,则按字符测量,在超过可用宽度的前一个字符处手动换行
float lineWidth = 0;
for (int cnt = 0; cnt != rawTextLine.length(); ++cnt) {
char ch = rawTextLine.charAt(cnt);
lineWidth += tvPaint.measureText(String.valueOf(ch));
if (lineWidth <= tvWidth) {
sbNewText.append(ch);
} else {
sbNewText.append("\n");
lineWidth = 0;
--cnt;
}
}
}
sbNewText.append("\n");
}
//把结尾多余的\n去掉
if (!rawText.endsWith("\n")) {
sbNewText.deleteCharAt(sbNewText.length() - 1);
}
return sbNewText.toString();
}
private void setBubble(TextView txtContent, ChatMsg chatMsg) {
Integer nOldBubble = (Integer)txtContent.getTag();
if (null == nOldBubble || nOldBubble != chatMsg.m_nBubble) {
int left = txtContent.getPaddingLeft();
int right = txtContent.getPaddingRight();
int top = txtContent.getPaddingTop();
int bottom = txtContent.getPaddingBottom();
boolean bIsUser = (ChatMsg.RIGHT == chatMsg.m_nType);
boolean bUseDefBubble = true;
if (chatMsg.m_nBubble != 0) {
//
}
if (bUseDefBubble) {
if (bIsUser) {
txtContent.setBackgroundResource(R.drawable.btn_style7);
txtContent.setTextColor(0xFFFFFFFF);
txtContent.setTextIsSelectable(true);
txtContent.setLinkTextColor(0xFF0000FF);
} else {
txtContent.setBackgroundResource(R.drawable.btn_style6);
txtContent.setTextColor(0xFF000000);
txtContent.setTextIsSelectable(true);
txtContent.setLinkTextColor(0xFF0000FF);
}
txtContent.setTag(0);
}
txtContent.setPadding(left, top, right, bottom);
}
}
public TextResizeData(TextView textView) {
this.paddingLeft = textView.getPaddingLeft();
this.paddingTop = textView.getPaddingTop();
this.paddingRight = textView.getPaddingRight();
this.paddingBottom = textView.getPaddingBottom();
this.width = textView.getWidth();
this.height = textView.getHeight();
this.gravity = textView.getGravity();
this.textColor = textView.getCurrentTextColor();
}
private static Bitmap captureTextBitmap(TextView textView) {
Drawable background = textView.getBackground();
textView.setBackground(null);
int width = textView.getWidth() - textView.getPaddingLeft() - textView.getPaddingRight();
int height = textView.getHeight() - textView.getPaddingTop() - textView.getPaddingBottom();
if (width == 0 || height == 0) {
return null;
}
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.translate(-textView.getPaddingLeft(), -textView.getPaddingTop());
textView.draw(canvas);
textView.setBackground(background);
return bitmap;
}
public void addTextTab(final int id, CharSequence text) {
int position = tabCount++;
if (position == 0 && selectedTabId == -1) {
selectedTabId = id;
}
positionToId.put(position, id);
idToPosition.put(id, position);
if (selectedTabId != -1 && selectedTabId == id) {
currentPosition = position;
prevLayoutWidth = 0;
}
TextView tab = new TextView(getContext());
tab.setWillNotDraw(false);
tab.setGravity(Gravity.CENTER);
tab.setText(text);
tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(selectorColorKey), 3));
tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
tab.setSingleLine(true);
tab.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
tab.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
tab.setOnClickListener(v -> {
int position1 = tabsContainer.indexOfChild(v);
if (position1 < 0) {
return;
}
if (position1 == currentPosition && delegate != null) {
delegate.onSamePageSelected();
return;
}
boolean scrollingForward = currentPosition < position1;
scrollingToChild = -1;
previousPosition = currentPosition;
currentPosition = position1;
selectedTabId = id;
if (animatingIndicator) {
AndroidUtilities.cancelRunOnUIThread(animationRunnable);
animatingIndicator = false;
}
animationTime = 0;
animatingIndicator = true;
animateIndicatorStartX = indicatorX;
animateIndicatorStartWidth = indicatorWidth;
TextView nextChild = (TextView) v;
animateIndicatorToWidth = getChildWidth(nextChild);
animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
setEnabled(false);
AndroidUtilities.runOnUIThread(animationRunnable, 16);
if (delegate != null) {
delegate.onPageSelected(id, scrollingForward);
}
scrollToChild(position1);
});
int tabWidth = (int) Math.ceil(tab.getPaint().measureText(text, 0, text.length())) + tab.getPaddingLeft() + tab.getPaddingRight();
allTextWidth += tabWidth;
positionToWidth.put(position, tabWidth);
tabsContainer.addView(tab, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT));
}
/**
* 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);
}
public void addTextTab(final int id, CharSequence text) {
int position = tabCount++;
if (position == 0 && selectedTabId == -1) {
selectedTabId = id;
}
positionToId.put(position, id);
idToPosition.put(id, position);
if (selectedTabId != -1 && selectedTabId == id) {
currentPosition = position;
prevLayoutWidth = 0;
}
TextView tab = new TextView(getContext());
tab.setWillNotDraw(false);
tab.setGravity(Gravity.CENTER);
tab.setText(text);
tab.setBackgroundDrawable(Theme.createSelectorDrawable(Theme.getColor(selectorColorKey), 3));
tab.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
tab.setSingleLine(true);
tab.setTypeface(AndroidUtilities.getTypeface("fonts/rmedium.ttf"));
tab.setPadding(AndroidUtilities.dp(16), 0, AndroidUtilities.dp(16), 0);
tab.setOnClickListener(v -> {
int position1 = tabsContainer.indexOfChild(v);
if (position1 < 0) {
return;
}
if (position1 == currentPosition && delegate != null) {
delegate.onSamePageSelected();
return;
}
boolean scrollingForward = currentPosition < position1;
scrollingToChild = -1;
previousPosition = currentPosition;
currentPosition = position1;
selectedTabId = id;
if (animatingIndicator) {
AndroidUtilities.cancelRunOnUIThread(animationRunnable);
animatingIndicator = false;
}
animationTime = 0;
animatingIndicator = true;
animateIndicatorStartX = indicatorX;
animateIndicatorStartWidth = indicatorWidth;
TextView nextChild = (TextView) v;
animateIndicatorToWidth = getChildWidth(nextChild);
animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
setEnabled(false);
AndroidUtilities.runOnUIThread(animationRunnable, 16);
if (delegate != null) {
delegate.onPageSelected(id, scrollingForward);
}
scrollToChild(position1);
});
int tabWidth = (int) Math.ceil(tab.getPaint().measureText(text, 0, text.length())) + tab.getPaddingLeft() + tab.getPaddingRight();
allTextWidth += tabWidth;
positionToWidth.put(position, tabWidth);
tabsContainer.addView(tab, LayoutHelper.createLinear(0, LayoutHelper.MATCH_PARENT));
}
private static CharSequence truncateImageSpan(Spannable instructionSpannable, TextView textView) {
int availableSpace = textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft();
return TextUtils.ellipsize(instructionSpannable, textView.getPaint(), availableSpace, TextUtils.TruncateAt.END);
}
/**
* 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);
}