下面列出了android.widget.TextView#getHeight ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Integer pointToWordIndex(float x, float y) {
float startX = 0;
boolean reachedY = false;
final int count = words.size();
// Margin top and bottom between words, see flowlayout
final int space = getResources().getDimensionPixelSize(R.dimen.word_padding_vertical);
for (int i = 0; i < count; i++) {
final TextView word = words.get(i);
final float wordX = word.getLeft();
final float wordY = word.getTop();
if (y > wordY - space && y < (wordY + word.getHeight() + space)) {
if (x > startX && x < (wordX + Math.round(word.getWidth() / 2))) {
return i;
}
startX = (wordX + (word.getWidth() / 2));
reachedY = true;
} else if (reachedY) {
return i;
}
}
return count;
}
@MainThread
public void append(int value) {
if (index >= codes.size()) return;
setInactive(containers);
setActive(containers.get(index));
TextView codeView = codes.get(index++);
Animation translateIn = new TranslateAnimation(0, 0, codeView.getHeight(), 0);
translateIn.setInterpolator(new OvershootInterpolator());
translateIn.setDuration(500);
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(200);
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(fadeIn);
animationSet.addAnimation(translateIn);
animationSet.reset();
animationSet.setStartTime(0);
codeView.setText(String.valueOf(value));
codeView.clearAnimation();
codeView.startAnimation(animationSet);
if (index == codes.size() && listener != null) {
listener.onCodeComplete(Stream.of(codes).map(TextView::getText).collect(Collectors.joining()));
}
}
private void addText(TextView textView, String content) {
textView.append(content);
textView.append("\n");
int offset = textView.getLineCount() * textView.getLineHeight();
if (offset > textView.getHeight()) {
textView.scrollTo(0, offset - textView.getHeight());
}
}
private int getRealHeight() {
TextView tv = textViewWeakReference.get();
if (tv == null) {
return 0;
}
return tv.getHeight() - tv.getPaddingTop() - tv.getPaddingBottom();
}
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 void hideHiddenCategories() {
if (isAutohide()) {
return;
}
for (String cat : db().getCategories()) {
boolean isNewCat = cat.equals(mCategoryJustCreated);
final TextView cattab = mCategoryTabs.get(cat);
if (cattab!=null) {
if (mCategory.equals(cat)) {
cattab.setVisibility(View.VISIBLE);
} else if ((Categories.isHiddenCategory(cat) || db().isHiddenCategory(cat))
|| (!cat.equals(Categories.CAT_SEARCH) && !mCategory.equals(cat) && db().getAppCount(cat) == 0 && !isNewCat)) {
if (mAnimationDuration>0 && cattab.getVisibility() != View.GONE) {
int h = cattab.getHeight();
mStyle.animateChangingSize(cattab, h, 1, null, new Runnable() {
@Override
public void run() {
cattab.setVisibility(View.GONE);
}
});
} else {
cattab.setVisibility(View.GONE);
}
} else {
cattab.setVisibility(View.VISIBLE);
}
}
}
}
/**
* requests focus on a {@link EditText} allowing to see its title too. It is used to set focus on the
* first question with error.
*
* @param et field we want to set focus in.
* @param tv_id id of the {@link TextView} which is the title of et.
*/
private void focusFirstError(EditText et, int tv_id) {
et.clearFocus(); // requestRectangle does not work properly if et is focused
et.requestFocus();
TextView title = (TextView) findViewById(tv_id);
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) title.getLayoutParams();
Rect rect = new Rect(0, -lp.topMargin, title.getWidth(), title.getHeight());
title.requestRectangleOnScreen(rect);
}
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();
}
@Override
public void draw(Canvas canvas) {
super.draw(canvas);
calculateIndicatorRect(indicatorRect);
if(indicator != null) {
indicator.setBounds(indicatorRect);
indicator.draw(canvas);
}
int i = 0;
while (i < tabsContainer.getChildCount()) {
if (i < currentPosition - 1 || i > currentPosition + 1) {
i++;
} else {
ViewGroup tab = (ViewGroup)tabsContainer.getChildAt(i);
TextView category_text = (TextView) tab.findViewById(R.id.category_text);
if (category_text != null) {
TextDrawable textDrawable = drawables[i - currentPosition + 1];
int save = canvas.save();
calculateIndicatorRect(indicatorRect);
canvas.clipRect(indicatorRect);
textDrawable.setText(category_text.getText());
textDrawable.setTextSize(0, category_text.getTextSize());
textDrawable.setTextColor(getResources().getColor(R.color.main_color));
int left = tab.getLeft() + category_text.getLeft() + (category_text.getWidth() - textDrawable.getIntrinsicWidth()) / 2 + getPaddingLeft();
int top = tab.getTop() + category_text.getTop() + (category_text.getHeight() - textDrawable.getIntrinsicHeight()) / 2 + getPaddingTop();
textDrawable.setBounds(left, top, textDrawable.getIntrinsicWidth() + left, textDrawable.getIntrinsicHeight() + top);
textDrawable.draw(canvas);
canvas.restoreToCount(save);
}
i++;
}
}
}
/**
* Get the minimum height of the view. On versions prior to Kitkat, getMinHeight is undefined so we return the
* height of the component. Therefore, this method should be called before the component is manipulated.
* @param view The text view of which to get the minimum height
* @return The "minimum" allowable height of the view
*/
public static int getMinHeight(TextView view) {
if (Build.VERSION.SDK_INT >= 16) {
return view.getMinHeight();
} else {
return view.getHeight();
}
}
GradientRunnable(TextView textView, int[] colors, int simultaneousColors, int angle, int speed) {
this.textView = textView;
this.colors = colors;
this.angle = angle;
this.speed = speed;
final int wf = textView.getWidth();
final int hf = textView.getHeight();
gradientsPositions = getGradientsPoints(wf, hf);
currentColors = Arrays.copyOf(colors, simultaneousColors);
}
public TextRunnable(TextView textView,char rotateText) {
this.number = textView;
this.rotateText = rotateText;
cX = textView.getWidth() / 2.0f;
cY = textView.getHeight() / 2.0f;
enableRefresh = true;
}
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() +
widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = areatop + widget.getHeight() - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0) {
if (buffer.getSpanStart(FROM_BELOW) >= 0) {
selStart = selEnd = buffer.length();
}
}
if (selStart > last)
selStart = selEnd = Integer.MAX_VALUE;
if (selEnd < first)
selStart = selEnd = -1;
switch (what) {
case CLICK:
if (selStart == selEnd) {
return false;
}
ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
if (link.length != 1)
return false;
link[0].onClick(widget);
break;
case UP:
int beststart, bestend;
beststart = -1;
bestend = -1;
for (int i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if (end < selEnd || selStart == selEnd) {
if (end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case DOWN:
beststart = Integer.MAX_VALUE;
bestend = Integer.MAX_VALUE;
for (int i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if (start > selStart || selStart == selEnd) {
if (start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}
private int getInnerHeight(TextView widget) {
return widget.getHeight() - widget.getTotalPaddingTop() - widget.getTotalPaddingBottom();
}
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() +
widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = areatop + widget.getHeight() - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0) {
if (buffer.getSpanStart(FROM_BELOW) >= 0) {
selStart = selEnd = buffer.length();
}
}
if (selStart > last)
selStart = selEnd = Integer.MAX_VALUE;
if (selEnd < first)
selStart = selEnd = -1;
switch (what) {
case CLICK:
if (selStart == selEnd) {
return false;
}
ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
if (link.length != 1)
return false;
link[0].onClick(widget);
break;
case UP:
int beststart, bestend;
beststart = -1;
bestend = -1;
for (int i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if (end < selEnd || selStart == selEnd) {
if (end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case DOWN:
beststart = Integer.MAX_VALUE;
bestend = Integer.MAX_VALUE;
for (int i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if (start > selStart || selStart == selEnd) {
if (start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() + widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = areatop + widget.getHeight() - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0) {
if (buffer.getSpanStart(FROM_BELOW) >= 0) {
selStart = selEnd = buffer.length();
}
}
if (selStart > last) selStart = selEnd = Integer.MAX_VALUE;
if (selEnd < first) selStart = selEnd = -1;
switch (what) {
case CLICK:
if (selStart == selEnd) {
return false;
}
ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
if (link.length != 1) return false;
link[0].onClick(widget);
break;
case UP:
int beststart, bestend;
beststart = -1;
bestend = -1;
for (int i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if (end < selEnd || selStart == selEnd) {
if (end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case DOWN:
beststart = Integer.MAX_VALUE;
bestend = Integer.MAX_VALUE;
for (int i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if (start > selStart || selStart == selEnd) {
if (start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}
private boolean handleSelection(float x, float y, float rawX, float rawY) {
View view = mRecyclerView.findChildViewUnder(x, y);
if (view == null)
return mSelectionLongPressMode;
long id = mRecyclerView.getChildItemId(view);
int index = mRecyclerView.getChildAdapterPosition(view);
TextView textView = findTextViewIn(view);
if (textView == null)
return mSelectionLongPressMode;
textView.getLocationOnScreen(mTmpLocation);
float viewX = rawX - mTmpLocation[0];
float viewY = rawY - mTmpLocation[1];
float tViewY = Math.min(Math.max(viewY, 0), textView.getHeight() -
textView.getCompoundPaddingBottom()) - textView.getCompoundPaddingTop();
float tViewX = Math.min(Math.max(viewX, 0), textView.getWidth() -
textView.getCompoundPaddingRight()) - textView.getCompoundPaddingLeft();
mLastTouchTextId = id;
int line = textView.getLayout().getLineForVertical((int) tViewY);
mLastTouchTextOffset = textView.getLayout().getOffsetForHorizontal(line, tViewX);
mLastTouchInText = viewX >= textView.getCompoundPaddingLeft() &&
viewX <= textView.getWidth() - textView.getCompoundPaddingEnd() &&
viewY >= textView.getCompoundPaddingTop() &&
viewY <= textView.getHeight() - textView.getCompoundPaddingBottom() &&
tViewX <= textView.getLayout().getLineWidth(line);
if (mSelectionLongPressMode) {
long sel = TextSelectionHelper.getWordAt(textView.getText(), mLastTouchTextOffset,
mLastTouchTextOffset + 1);
int selStart = TextSelectionHelper.unpackTextRangeStart(sel);
int selEnd = TextSelectionHelper.unpackTextRangeEnd(sel);
int selLongPressIndex = getItemPosition(mSelectionLongPressId);
if (index > selLongPressIndex ||
(index == selLongPressIndex && selEnd >= mSelectionLongPressStart)) {
setSelection(mSelectionLongPressId, mSelectionLongPressStart,
mLastTouchTextId, selEnd);
} else {
setSelection(mLastTouchTextId, selStart,
mSelectionLongPressId, mSelectionLongPressEnd);
}
return true;
}
return false;
}
private boolean action(int what, TextView widget, Spannable buffer) {
Layout layout = widget.getLayout();
int padding = widget.getTotalPaddingTop() +
widget.getTotalPaddingBottom();
int areatop = widget.getScrollY();
int areabot = areatop + widget.getHeight() - padding;
int linetop = layout.getLineForVertical(areatop);
int linebot = layout.getLineForVertical(areabot);
int first = layout.getLineStart(linetop);
int last = layout.getLineEnd(linebot);
ClickableSpan[] candidates = buffer.getSpans(first, last, ClickableSpan.class);
int a = Selection.getSelectionStart(buffer);
int b = Selection.getSelectionEnd(buffer);
int selStart = Math.min(a, b);
int selEnd = Math.max(a, b);
if (selStart < 0) {
if (buffer.getSpanStart(FROM_BELOW) >= 0) {
selStart = selEnd = buffer.length();
}
}
if (selStart > last)
selStart = selEnd = Integer.MAX_VALUE;
if (selEnd < first)
selStart = selEnd = -1;
switch (what) {
case CLICK:
if (selStart == selEnd) {
return false;
}
ClickableSpan[] link = buffer.getSpans(selStart, selEnd, ClickableSpan.class);
if (link.length != 1)
return false;
link[0].onClick(widget);
break;
case UP:
int beststart, bestend;
beststart = -1;
bestend = -1;
for (int i = 0; i < candidates.length; i++) {
int end = buffer.getSpanEnd(candidates[i]);
if (end < selEnd || selStart == selEnd) {
if (end > bestend) {
beststart = buffer.getSpanStart(candidates[i]);
bestend = end;
}
}
}
if (beststart >= 0) {
Selection.setSelection(buffer, bestend, beststart);
return true;
}
break;
case DOWN:
beststart = Integer.MAX_VALUE;
bestend = Integer.MAX_VALUE;
for (int i = 0; i < candidates.length; i++) {
int start = buffer.getSpanStart(candidates[i]);
if (start > selStart || selStart == selEnd) {
if (start < beststart) {
beststart = start;
bestend = buffer.getSpanEnd(candidates[i]);
}
}
}
if (bestend < Integer.MAX_VALUE) {
Selection.setSelection(buffer, beststart, bestend);
return true;
}
break;
}
return false;
}