下面列出了android.widget.TextView#getResources ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void displayNameAndIcon(TextView textView, ImageView imageView) {
textView.setTag(conv.getConvId());
if (conv instanceof PrivateConversation) {
UserInfoManager.getInstance().getUserInfo(((PrivateConversation) conv).getTargetUid(), userInfo -> {
// text view haven't changed
if (textView.getTag().equals(conv.getConvId())) {
textView.setText(userInfo.getName());
new URLImageViewCacheLoader(userInfo.getPhotoUrl(), imageView)
.roundImage(true).execute();
}
});
} else {
if (imageView.getTag() instanceof Job)
((Job) imageView.getTag()).cancel(null);
Resources r = textView.getResources();
textView.setText(((GroupConversation) conv).getName());
imageView.setImageDrawable(((GroupConversation) conv).getRoundIconDrawable(r));
}
}
private void setTimeDay(TextView textView, String day) {
final Resources resources = textView.getResources();
final String suffix = resources.getString(R.string.txt_day);
final int dayLen = day.length();
SpannableStringBuilder sBuilder = new SpannableStringBuilder();
sBuilder.append(day) // Bold this
.append('\n') // Default TextView font.
.append(suffix); // Default TextView font.
// Create the Typeface you want to apply to certain text
CalligraphyTypefaceSpan typefaceSpan = new CalligraphyTypefaceSpan(TypefaceUtils.load(resources.getAssets(), "fonts/Hero.otf"));
sBuilder.setSpan(typefaceSpan, 0, dayLen, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Font
sBuilder.setSpan(new AbsoluteSizeSpan(resources.getDimensionPixelSize(R.dimen.font_24)),
0, dayLen, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
// Color
sBuilder.setSpan(new ForegroundColorSpan(resources.getColor(R.color.cyan_700)),
0, dayLen, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(sBuilder, TextView.BufferType.SPANNABLE);
}
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation<? super Bitmap> glideAnimation) {
TextView tv = textView.get();
if (tv != null) {
BitmapDrawable bitmapDrawable = new BitmapDrawable(tv.getResources(), bitmap);
// image span doesn't handle scaling so we manually set bounds
if (bitmap.getWidth() > tv.getWidth()) {
float aspectRatio = (float) bitmap.getHeight() / (float) bitmap.getWidth();
bitmapDrawable.setBounds(0, 0, tv.getWidth(), (int) (aspectRatio * tv.getWidth()));
} else {
bitmapDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
}
ImageSpan span = new ImageSpan(bitmapDrawable);
// add the image span and remove our marker
SpannableStringBuilder ssb = new SpannableStringBuilder(tv.getText());
int start = ssb.getSpanStart(loadingSpan);
int end = ssb.getSpanEnd(loadingSpan);
if (start >= 0 && end >= 0) {
ssb.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
ssb.removeSpan(loadingSpan);
// animate the change
TransitionManager.beginDelayedTransition((ViewGroup) tv.getParent());
tv.setText(ssb);
}
}
/**
* Assign custom phrase in TextView to describe the number of days remaining until the birthday
* @param textView View used to display text
* @param numberDaysRemaining Number of days left
*/
public static void assignDaysRemainingInTextView(TextView textView, int numberDaysRemaining) {
Resources resources = textView.getResources();
if(numberDaysRemaining == 0) {
textView.setText(resources.getString(R.string.dialog_select_birthday_zero_day_left));
} else if(numberDaysRemaining == 1){
textView.setText(resources.getString(R.string.dialog_select_birthday_one_day_left));
} else{
textView.setText(resources.getString(R.string.dialog_select_birthday_number_days_left, numberDaysRemaining));
}
}
public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs,
final int defStyle, final ArrayList<TextView> wordViews,
final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) {
mWordViews = wordViews;
mDividerViews = dividerViews;
mDebugInfoViews = debugInfoViews;
final TextView wordView = wordViews.get(0);
final View dividerView = dividerViews.get(0);
mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight();
dividerView.measure(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mDividerWidth = dividerView.getMeasuredWidth();
final Resources res = wordView.getResources();
mSuggestionsStripHeight = res.getDimensionPixelSize(
R.dimen.config_suggestions_strip_height);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView);
mSuggestionStripOptions = a.getInt(
R.styleable.SuggestionStripView_suggestionStripOptions, 0);
mAlphaObsoleted = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaObsoleted, 1.0f);
mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0);
mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0);
mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0);
mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0);
mSuggestionsCountInStrip = a.getInt(
R.styleable.SuggestionStripView_suggestionsCountInStrip,
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
mCenterSuggestionWeight = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_centerSuggestionPercentile,
DEFAULT_CENTER_SUGGESTION_PERCENTILE);
mMaxMoreSuggestionsRow = a.getInt(
R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
a.recycle();
mMoreSuggestionsHint = getMoreSuggestionsHint(res,
res.getDimension(R.dimen.config_more_suggestions_hint_text_size),
mColorAutoCorrect);
mCenterPositionInStrip = mSuggestionsCountInStrip / 2;
// Assuming there are at least three suggestions. Also, note that the suggestions are
// laid out according to script direction, so this is left of the center for LTR scripts
// and right of the center for RTL scripts.
mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1;
mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
R.dimen.config_more_suggestions_bottom_gap);
mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
R.dimen.config_more_suggestions_row_height);
}
public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs,
final int defStyle, final ArrayList<TextView> wordViews,
final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) {
mWordViews = wordViews;
mDividerViews = dividerViews;
mDebugInfoViews = debugInfoViews;
final TextView wordView = wordViews.get(0);
final View dividerView = dividerViews.get(0);
mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight();
dividerView.measure(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mDividerWidth = dividerView.getMeasuredWidth();
final Resources res = wordView.getResources();
mSuggestionsStripHeight = res.getDimensionPixelSize(
R.dimen.config_suggestions_strip_height);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView);
mSuggestionStripOptions = a.getInt(
R.styleable.SuggestionStripView_suggestionStripOptions, 0);
mAlphaObsoleted = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaObsoleted, 1.0f);
mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0);
mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0);
mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0);
mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0);
mSuggestionsCountInStrip = a.getInt(
R.styleable.SuggestionStripView_suggestionsCountInStrip,
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
mCenterSuggestionWeight = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_centerSuggestionPercentile,
DEFAULT_CENTER_SUGGESTION_PERCENTILE);
mMaxMoreSuggestionsRow = a.getInt(
R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
a.recycle();
mMoreSuggestionsHint = getMoreSuggestionsHint(res,
res.getDimension(R.dimen.config_more_suggestions_hint_text_size),
mColorAutoCorrect);
mCenterPositionInStrip = mSuggestionsCountInStrip / 2;
// Assuming there are at least three suggestions. Also, note that the suggestions are
// laid out according to script direction, so this is left of the center for LTR scripts
// and right of the center for RTL scripts.
mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1;
mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
R.dimen.config_more_suggestions_bottom_gap);
mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
R.dimen.config_more_suggestions_row_height);
}
public SuggestionStripLayoutHelper(final Context context, final AttributeSet attrs,
final int defStyle, final ArrayList<TextView> wordViews,
final ArrayList<View> dividerViews, final ArrayList<TextView> debugInfoViews) {
mWordViews = wordViews;
mDividerViews = dividerViews;
mDebugInfoViews = debugInfoViews;
final TextView wordView = wordViews.get(0);
final View dividerView = dividerViews.get(0);
mPadding = wordView.getCompoundPaddingLeft() + wordView.getCompoundPaddingRight();
dividerView.measure(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
mDividerWidth = dividerView.getMeasuredWidth();
final Resources res = wordView.getResources();
mSuggestionsStripHeight = res.getDimensionPixelSize(
R.dimen.config_suggestions_strip_height);
final TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SuggestionStripView, defStyle, R.style.SuggestionStripView);
mSuggestionStripOptions = a.getInt(
R.styleable.SuggestionStripView_suggestionStripOptions, 0);
mAlphaObsoleted = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_alphaObsoleted, 1.0f);
mColorValidTypedWord = a.getColor(R.styleable.SuggestionStripView_colorValidTypedWord, 0);
mColorTypedWord = a.getColor(R.styleable.SuggestionStripView_colorTypedWord, 0);
mColorAutoCorrect = a.getColor(R.styleable.SuggestionStripView_colorAutoCorrect, 0);
mColorSuggested = a.getColor(R.styleable.SuggestionStripView_colorSuggested, 0);
mSuggestionsCountInStrip = a.getInt(
R.styleable.SuggestionStripView_suggestionsCountInStrip,
DEFAULT_SUGGESTIONS_COUNT_IN_STRIP);
mCenterSuggestionWeight = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_centerSuggestionPercentile,
DEFAULT_CENTER_SUGGESTION_PERCENTILE);
mMaxMoreSuggestionsRow = a.getInt(
R.styleable.SuggestionStripView_maxMoreSuggestionsRow,
DEFAULT_MAX_MORE_SUGGESTIONS_ROW);
mMinMoreSuggestionsWidth = ResourceUtils.getFraction(a,
R.styleable.SuggestionStripView_minMoreSuggestionsWidth, 1.0f);
a.recycle();
mMoreSuggestionsHint = getMoreSuggestionsHint(res,
res.getDimension(R.dimen.config_more_suggestions_hint_text_size),
mColorAutoCorrect);
mCenterPositionInStrip = mSuggestionsCountInStrip / 2;
// Assuming there are at least three suggestions. Also, note that the suggestions are
// laid out according to script direction, so this is left of the center for LTR scripts
// and right of the center for RTL scripts.
mTypedWordPositionWhenAutocorrect = mCenterPositionInStrip - 1;
mMoreSuggestionsBottomGap = res.getDimensionPixelOffset(
R.dimen.config_more_suggestions_bottom_gap);
mMoreSuggestionsRowHeight = res.getDimensionPixelSize(
R.dimen.config_more_suggestions_row_height);
}
public TitleChanger(TextView title) {
this.title = title;
Resources res = title.getResources();
animDelay = DEFAULT_ANIMATION_DELAY;
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
translate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}
public TitleChanger(TextView title) {
this.title = title;
Resources res = title.getResources();
animDelay = DEFAULT_ANIMATION_DELAY;
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
translate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}
public TitleChanger(TextView title) {
this.title = title;
Resources res = title.getResources();
animDelay = DEFAULT_ANIMATION_DELAY;
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
yTranslate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}
public TitleChanger(TextView title) {
this.title = title;
Resources res = title.getResources();
animDelay = DEFAULT_ANIMATION_DELAY;
animDuration = res.getInteger(android.R.integer.config_shortAnimTime) / 2;
translate = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, DEFAULT_Y_TRANSLATION_DP, res.getDisplayMetrics()
);
}