下面列出了android.widget.TextView#getLeft ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void selectTabWithId(int id, float progress) {
int position = idToPosition.get(id, -1);
if (position < 0) {
return;
}
if (progress < 0) {
progress = 0;
} else if (progress > 1.0f) {
progress = 1.0f;
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
TextView nextChild = (TextView) tabsContainer.getChildAt(position);
if (child != null && nextChild != null) {
animateIndicatorStartX = child.getLeft();
animateIndicatorStartWidth = child.getMeasuredWidth();
animateIndicatorToX = nextChild.getLeft();
animateIndicatorToWidth = nextChild.getMeasuredWidth();
setAnimationProgressInernal(nextChild, child, progress);
}
if (progress >= 1.0f) {
currentPosition = position;
selectedTabId = id;
}
}
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;
}
public void selectTabWithId(int id, float progress) {
int position = idToPosition.get(id, -1);
if (position < 0) {
return;
}
if (progress < 0) {
progress = 0;
} else if (progress > 1.0f) {
progress = 1.0f;
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
TextView nextChild = (TextView) tabsContainer.getChildAt(position);
if (child != null && nextChild != null) {
animateIndicatorStartX = child.getLeft();
animateIndicatorStartWidth = child.getMeasuredWidth();
animateIndicatorToX = nextChild.getLeft();
animateIndicatorToWidth = nextChild.getMeasuredWidth();
setAnimationProgressInernal(nextChild, child, progress);
}
if (progress >= 1.0f) {
currentPosition = position;
selectedTabId = id;
}
}
/**
* 指示器跟随手指进行滚动
*/
public void scroll(int position, float offset) {
if (getChildAt(position + 1) != null) {
TextView end = (TextView) getChildAt(position + 1);
Rect bound = new Rect();
end.getPaint().getTextBounds(end.getText().toString(), 0, end.getText().length(), bound);
int endLength = bound.width() + 20;
int endTranslationX = (end.getMeasuredWidth() - endLength) / 2 + end.getLeft();
TextView start = (TextView) getChildAt(position);
start.getPaint().getTextBounds(start.getText().toString(), 0, start.getText().length(), bound);
int startLength = bound.width() + 20;
int startTranslationX = (start.getMeasuredWidth() - startLength) / 2 + start.getLeft();
mTranslationX = startTranslationX + (endTranslationX - startTranslationX) * offset;
float value = (endLength - startLength) * offset;
mPath.reset();
mPath.moveTo(0, 0);
mPath.lineTo(value + startLength, 0);
mPath.lineTo(value + startLength, lineHeight);
mPath.lineTo(0, value + startLength);
mPath.close();
}
invalidate();
}
private void calculateIndicatorRect(Rect rect) {
ViewGroup currentTab = (ViewGroup)tabsContainer.getChildAt(currentPosition);
TextView category_text = (TextView) currentTab.findViewById(R.id.category_text);
float left = (float) (currentTab.getLeft() + category_text.getLeft());
float width = ((float) category_text.getWidth()) + left;
if (currentPositionOffset > 0f && currentPosition < tabCount - 1) {
ViewGroup nextTab = (ViewGroup)tabsContainer.getChildAt(currentPosition + 1);
TextView next_category_text = (TextView) nextTab.findViewById(R.id.category_text);
float next_left = (float) (nextTab.getLeft() + next_category_text.getLeft());
left = left * (1.0f - currentPositionOffset) + next_left * currentPositionOffset;
width = width * (1.0f - currentPositionOffset) + currentPositionOffset * (((float) next_category_text.getWidth()) + next_left);
}
rect.set(((int) left) + getPaddingLeft(), getPaddingTop() + currentTab.getTop() + category_text.getTop(),
((int) width) + getPaddingLeft(), currentTab.getTop() + getPaddingTop() + category_text.getTop() + category_text.getHeight());
}
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;
}
private void scrollToChild(int position) {
if (tabCount == 0 || scrollingToChild == position) {
return;
}
scrollingToChild = position;
TextView child = (TextView) tabsContainer.getChildAt(position);
if (child == null) {
return;
}
int currentScrollX = getScrollX();
int left = child.getLeft();
int width = child.getMeasuredWidth();
if (left < currentScrollX) {
smoothScrollTo(left, 0);
} else if (left + width > currentScrollX + getWidth()) {
smoothScrollTo(left + width, 0);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (prevLayoutWidth != r - l) {
prevLayoutWidth = r - l;
scrollingToChild = -1;
if (animatingIndicator) {
AndroidUtilities.cancelRunOnUIThread(animationRunnable);
animatingIndicator = false;
setEnabled(true);
if (delegate != null) {
delegate.onPageScrolled(1.0f);
}
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
if (child != null) {
indicatorWidth = getChildWidth(child);
indicatorX = child.getLeft() + (child.getMeasuredWidth() - indicatorWidth) / 2;
}
}
}
private void scrollToChild(int position) {
if (tabCount == 0 || scrollingToChild == position) {
return;
}
scrollingToChild = position;
TextView child = (TextView) tabsContainer.getChildAt(position);
if (child == null) {
return;
}
int currentScrollX = getScrollX();
int left = child.getLeft();
int width = child.getMeasuredWidth();
if (left < currentScrollX) {
smoothScrollTo(left, 0);
} else if (left + width > currentScrollX + getWidth()) {
smoothScrollTo(left + width, 0);
}
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (prevLayoutWidth != r - l) {
prevLayoutWidth = r - l;
scrollingToChild = -1;
if (animatingIndicator) {
AndroidUtilities.cancelRunOnUIThread(animationRunnable);
animatingIndicator = false;
setEnabled(true);
if (delegate != null) {
delegate.onPageScrolled(1.0f);
}
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
if (child != null) {
indicatorWidth = getChildWidth(child);
indicatorX = child.getLeft() + (child.getMeasuredWidth() - indicatorWidth) / 2;
}
}
}
@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++;
}
}
}
public void selectTabWithId(int id, float progress) {
int position = idToPosition.get(id, -1);
if (position < 0) {
return;
}
if (progress < 0) {
progress = 0;
} else if (progress > 1.0f) {
progress = 1.0f;
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
TextView nextChild = (TextView) tabsContainer.getChildAt(position);
if (child != null && nextChild != null) {
animateIndicatorStartWidth = getChildWidth(child);
animateIndicatorStartX = child.getLeft() + (child.getMeasuredWidth() - animateIndicatorStartWidth) / 2;
animateIndicatorToWidth = getChildWidth(nextChild);
animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
setAnimationProgressInernal(nextChild, child, progress);
if (progress >= 1f) {
child.setTag(unactiveTextColorKey);
nextChild.setTag(activeTextColorKey);
}
scrollToChild(tabsContainer.indexOfChild(nextChild));
}
if (progress >= 1.0f) {
currentPosition = position;
selectedTabId = id;
}
}
public void selectTabWithId(int id, float progress) {
int position = idToPosition.get(id, -1);
if (position < 0) {
return;
}
if (progress < 0) {
progress = 0;
} else if (progress > 1.0f) {
progress = 1.0f;
}
TextView child = (TextView) tabsContainer.getChildAt(currentPosition);
TextView nextChild = (TextView) tabsContainer.getChildAt(position);
if (child != null && nextChild != null) {
animateIndicatorStartWidth = getChildWidth(child);
animateIndicatorStartX = child.getLeft() + (child.getMeasuredWidth() - animateIndicatorStartWidth) / 2;
animateIndicatorToWidth = getChildWidth(nextChild);
animateIndicatorToX = nextChild.getLeft() + (nextChild.getMeasuredWidth() - animateIndicatorToWidth) / 2;
setAnimationProgressInernal(nextChild, child, progress);
if (progress >= 1f) {
child.setTag(unactiveTextColorKey);
nextChild.setTag(activeTextColorKey);
}
scrollToChild(tabsContainer.indexOfChild(nextChild));
}
if (progress >= 1.0f) {
currentPosition = position;
selectedTabId = id;
}
}
/** Clicks between two titles in a <code>ViewPager</code> title strip */
public static ViewAction clickBetweenTwoTitles(final String title1, final String title2) {
return new GeneralClickAction(
Tap.SINGLE,
new CoordinatesProvider() {
@Override
public float[] calculateCoordinates(View view) {
PagerTitleStrip pagerStrip = (PagerTitleStrip) view;
// Get the screen position of the pager strip
final int[] viewScreenPosition = new int[2];
pagerStrip.getLocationOnScreen(viewScreenPosition);
// Get the left / right of the first title
int title1Left = 0, title1Right = 0, title2Left = 0, title2Right = 0;
final int childCount = pagerStrip.getChildCount();
for (int i = 0; i < childCount; i++) {
final View child = pagerStrip.getChildAt(i);
if (child instanceof TextView) {
final TextView textViewChild = (TextView) child;
final CharSequence childText = textViewChild.getText();
if (title1.equals(childText)) {
title1Left = textViewChild.getLeft();
title1Right = textViewChild.getRight();
} else if (title2.equals(childText)) {
title2Left = textViewChild.getLeft();
title2Right = textViewChild.getRight();
}
}
}
if (title1Right < title2Left) {
// Title 1 is to the left of title 2
return new float[] {
viewScreenPosition[0] + (title1Right + title2Left) / 2,
viewScreenPosition[1] + pagerStrip.getHeight() / 2
};
} else {
// The assumption here is that PagerTitleStrip prevents titles
// from overlapping, so if we get here it means that title 1
// is to the right of title 2
return new float[] {
viewScreenPosition[0] + (title2Right + title1Left) / 2,
viewScreenPosition[1] + pagerStrip.getHeight() / 2
};
}
}
},
Press.FINGER,
0,
0);
}