下面列出了android.widget.TextView#getPaddingTop ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
}
@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);
}
}
@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);
}
}
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;
}
}
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());
}
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 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 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 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 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();
}
int getReadHeight() {
TextView tv = textViewWeakReference.get();
if (tv == null) {
return 0;
}
return tv.getHeight() - tv.getPaddingTop() - tv.getPaddingBottom();
}
private int getRealHeight() {
TextView tv = textViewWeakReference.get();
if (tv == null) {
return 0;
}
return tv.getHeight() - tv.getPaddingTop() - tv.getPaddingBottom();
}
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);
}
}
protected void transplantStyle(TextView target, int resource) {
//get styles from here
TextView tv = (TextView)View.inflate(this, resource, null);
int[] padding = {target.getPaddingLeft(), target.getPaddingTop(), target.getPaddingRight(), target.getPaddingBottom()};
target.setTextColor(tv.getTextColors().getDefaultColor());
target.setTypeface(tv.getTypeface());
target.setBackgroundDrawable(tv.getBackground());
target.setPadding(padding[0], padding[1], padding[2], padding[3]);
}
/**
* 改变外框大小,适应文字
* 如果当前文字只有一行,则缩到外框适应文字大小
* 如果文字有多行,则宽度不变,缩小高度到适应文字大小
* 文字外框的位置不变
*
* @param view
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static void adjustSize(TextView view) {
if (view != null && view.getLayoutParams() != null) {
/*
//更好的实现方式,但是ios那边不支持这种方式
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
view.setLayoutParams(layoutParams);*/
// 废弃的实现方式,使用WRAP_CONTENT最简单,且这种方式算出来的width有bug
// @Deprecated
/*int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
int width, height;
if (lineCount > 1) {//多行,宽度不变,改变高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改变宽度&高度
Paint paint = view.getPaint();
width = (int) paint.measureText(view.getText().toString()) + view.getPaddingLeft() + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}*/
int lineCount = Math.min(view.getLineCount(), getMaxLines(view));
float width, height;
if (lineCount > 1) {//多行,宽度不变,改变高度
width = view.getWidth();
height = view.getLineHeight() * lineCount + view.getPaddingTop() + view.getPaddingBottom();
} else {//一行,改变宽度&高度
width = view.getPaddingLeft() + Layout.getDesiredWidth(view.getText(), view.getPaint()) + view.getPaddingRight();
height = view.getLineHeight() + view.getPaddingTop() + view.getPaddingBottom();
}
if (view.getLayoutParams() != null) {
ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
layoutParams.width = (int) width;
layoutParams.height = (int) height;
view.setLayoutParams(layoutParams);
}
}
}