android.widget.TextView#getWidth ( )源码实例Demo

下面列出了android.widget.TextView#getWidth ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: cannonball-android   文件: PoemBuilderActivity.java
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;
}
 
源代码2 项目: QiQuYing   文件: CategoryTabStrip.java
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());

}
 
源代码3 项目: cannonball-android   文件: PoemBuilderActivity.java
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;
}
 
源代码4 项目: Bright   文件: PictureFragment.java
@Override
public Bitmap transform(Bitmap source) {
    for (int i = 0; i < labels.length; ++i) {
        TextView label = labels[i];

        int x = (int) label.getX();
        int y = (int) label.getY();
        int width = label.getWidth();
        int height = label.getHeight();

        Bitmap dest = Bitmap.createBitmap(source, x, y, width, height);
        luminances[i] = bright.brightness(dest);
        dest.recycle();
    }
    return source;
}
 
源代码5 项目: animation-samples   文件: TextResize.java
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;
}
 
源代码6 项目: animation-samples   文件: TextResize.java
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();
}
 
源代码7 项目: RichText   文件: ImageTarget.java
/**
 * 获取可用宽度
 *
 * @return width
 */
int getRealWidth() {
    TextView tv = textViewWeakReference.get();
    if (tv == null) {
        return 0;
    }
    return tv.getWidth() - tv.getPaddingRight() - tv.getPaddingLeft();
}
 
源代码8 项目: openboard   文件: SuggestionStripLayoutHelper.java
public void layoutImportantNotice(final View importantNoticeStrip,
        final String importantNoticeTitle) {
    final TextView titleView = 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);
}
 
源代码9 项目: Markdown   文件: StyleBuilderImpl.java
private int getTextViewRealWidth() {
    TextView textView = textViewWeakReference.get();
    if (textView != null) {
        return textView.getWidth() - textView.getPaddingRight() - textView.getPaddingLeft();
    }
    return 0;
}
 
源代码10 项目: medical-data-android   文件: RegisterActivity.java
/**
 * 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);
}
 
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);
}
 
源代码12 项目: android-dialer   文件: ViewUtil.java
public static void resizeText(TextView textView, int originalTextSize, int minTextSize) {
  final Paint paint = textView.getPaint();
  final int width = textView.getWidth();
  if (width == 0) {
    return;
  }
  textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, originalTextSize);
  float ratio = width / paint.measureText(textView.getText().toString());
  if (ratio <= 1.0f) {
    textView.setTextSize(
        TypedValue.COMPLEX_UNIT_PX, Math.max(minTextSize, originalTextSize * ratio));
  }
}
 
源代码13 项目: medical-data-android   文件: ProfileActivity.java
/**
 * 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 already 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);
}
 
源代码14 项目: RichText   文件: RichTextConfig.java
@Override
public Drawable getDrawable(ImageHolder holder, RichTextConfig config, TextView textView) {
    ColorDrawable drawable = new ColorDrawable(Color.LTGRAY);
    int width = textView.getWidth() ;
    drawable.setBounds(0, 0, width, width / 2);
    HANDLER.obtainMessage(SET_BOUNDS, Pair.create(drawable, textView)).sendToTarget();
    return drawable;
}
 
源代码15 项目: Silence   文件: ViewUtil.java
public static CharSequence ellipsize(@Nullable CharSequence text, @NonNull TextView view) {
  if (TextUtils.isEmpty(text) || view.getWidth() == 0 || view.getEllipsize() != TruncateAt.END) {
    return text;
  } else {
    return TextUtils.ellipsize(text,
                               view.getPaint(),
                               view.getWidth() - view.getPaddingRight() - view.getPaddingLeft(),
                               TruncateAt.END);
  }
}
 
源代码16 项目: android-instant-apps   文件: TextResize.java
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();
}
 
源代码17 项目: VideoOS-Android-SDK   文件: TextUtil.java
/**
 * 改变外框大小,适应文字
 * 如果当前文字只有一行,则缩到外框适应文字大小
 * 如果文字有多行,则宽度不变,缩小高度到适应文字大小
 * 文字外框的位置不变
 *
 * @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);
        }
    }
}
 
源代码18 项目: VideoOS-Android-SDK   文件: TextUtil.java
/**
 * Re-sizes the textSize of the TextView so that the text fits within the bounds of the View.
 */
static void adjustTextSize(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);
}
 
源代码19 项目: DarkCalculator   文件: AutofitHelper.java
/**
 * 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);
}
 
源代码20 项目: revolution-irc   文件: ChatSelectTouchListener.java
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;
}
 
 方法所在类
 同类方法