下面列出了android.widget.TextView#getCompoundDrawables ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onDestroySuccess(long sid, long aid, TextView tvAttitude) {
AttitudeContainer.sHeartContainer.remove(sid);
Drawable[] compoundDrawables = tvAttitude.getCompoundDrawables();
if (compoundDrawables[0] != null) {
Drawable drawable = mActivity.getResources().getDrawable(R.drawable.ic_like);
// 必须设置图片大小,否则不显示
drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
tvAttitude.setCompoundDrawables(drawable, null, null, null);
} else {
tvAttitude.setTextColor(ContextCompat.getColor(mActivity, R.color.retweeted_count_text_color));
}
String countStr = tvAttitude.getText().toString();
if (TextUtils.isDigitsOnly(countStr)) {
int count = Integer.parseInt(countStr) - 1;
tvAttitude.setText(NumberFormatter.formatWBCount(count, 60000));
}
}
/**
* apply compound property in textView
* position 0:left, 1:top, 2:right, 3:bottom
* - REF : drawable to load as compoundDrawable
* - BASE64 : decode as base64 and set as CompoundDrawable
*/
public static void applyCompoundDrawable(View view, DynamicProperty property, int position) {
if (view instanceof TextView) {
TextView textView = (TextView) view;
Drawable[] d = textView.getCompoundDrawables();
switch (property.type) {
case REF: {
try {
d[position] = view.getContext().getResources().getDrawable(getDrawableId(view.getContext(), property.getValueString()));
} catch (Exception e) {}
}
break;
case BASE64: {
d[position] = property.getValueBitmapDrawable();
}
break;
case DRAWABLE: {
d[position] = property.getValueGradientDrawable();
}
break;
}
textView.setCompoundDrawablesWithIntrinsicBounds(d[0], d[1], d[2], d[3]);
}
}
@BindingAdapter({"drawableTop", "tintAttr"})
public static void setDrawableTopTintedFromAttr(
@NonNull final TextView textView,
@Nullable Drawable top,
@AttrRes final int tintAttr) {
final Drawable[] drawables = textView.getCompoundDrawables();
if (top != null) {
top = DrawableUtils.getTintedDrawableFromAttrTint(textView.getContext(), top, tintAttr);
}
textView.setCompoundDrawablesWithIntrinsicBounds(
drawables[0],
top,
drawables[2],
drawables[3]);
}
private void setTextViewDrawableColor(TextView textView, int color) {
for (Drawable drawable : textView.getCompoundDrawables()) {
if (drawable != null) {
drawable.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(textView.getContext(), color), PorterDuff.Mode.SRC_IN));
}
}
}
private Drawable getStartCompoundDrawable(TextView textView) {
Drawable[] drawables = textView.getCompoundDrawables();
// Start drawable can be in the left or right index based on if the layout is rtl.
if (drawables[0] != null) {
return drawables[0];
}
return drawables[2];
}
private static List<Pair<String, Bitmap>> getTextViewDrawableBitmap(TextView textView) {
List<Pair<String, Bitmap>> bitmaps = new ArrayList<>();
try {
Drawable[] drawables = textView.getCompoundDrawables();
bitmaps.add(new Pair<>("DrawableLeft", getDrawableBitmap(drawables[0])));
bitmaps.add(new Pair<>("DrawableTop", getDrawableBitmap(drawables[1])));
bitmaps.add(new Pair<>("DrawableRight", getDrawableBitmap(drawables[2])));
bitmaps.add(new Pair<>("DrawableBottom", getDrawableBitmap(drawables[3])));
} catch (Exception e) {
e.printStackTrace();
}
return bitmaps;
}
public static void setDrawable(TextView textView, Drawable drawable, int position) {
Drawable[] drawables = textView.getCompoundDrawables();
textView.setCompoundDrawablesWithIntrinsicBounds(
(position == 0) ? drawable : drawables[0],
(position == 1) ? drawable : drawables[1],
(position == 2) ? drawable : drawables[2],
(position == 3) ? drawable : drawables[3]);
}
public static void setLeftDrawable(TextView textView, Drawable leftDrawable) {
setDrawableBounds(leftDrawable);
Drawable top = textView.getCompoundDrawables()[1];
Drawable right = textView.getCompoundDrawables()[2];
Drawable bottom = textView.getCompoundDrawables()[3];
textView.setCompoundDrawables(leftDrawable, top, right, bottom);
}
/**
* 将radiobutton的drawable动态的缩放
*
* @param percent
* @param rb
* @return
*/
public static Drawable getScaleDrawableForRadioButton(float percent, TextView rb) {
Drawable[] compoundDrawables = rb.getCompoundDrawables();
Drawable drawable = null;
for (Drawable d : compoundDrawables) {
if (d != null) {
drawable = d;
}
}
drawable.setBounds(0, 0, (int) (drawable.getIntrinsicWidth() * percent + 0.5f), (int) (drawable.getIntrinsicHeight() * percent + 0.5f));
return drawable;
}
/**
* @param keyword
*/
public RightDrawableOnTouchListener(TextView view) {
super();
final Drawable[] drawables = view.getCompoundDrawables();
if (drawables != null && drawables.length == 4)
this.drawable = drawables[2];
}
public static void setBottomDrawable(TextView textView, Drawable bottomDrawable) {
setDrawableBounds(bottomDrawable);
Drawable left = textView.getCompoundDrawables()[0];
Drawable top = textView.getCompoundDrawables()[1];
Drawable right = textView.getCompoundDrawables()[2];
textView.setCompoundDrawables(left, top, right, bottomDrawable);
}
/**
* Returns the drawable for the given text view.
*/
public static Drawable getTextViewIcon(TextView tv) {
final Drawable[] drawables = tv.getCompoundDrawables();
for (int i = 0; i < drawables.length; i++) {
if (drawables[i] != null) {
return drawables[i];
}
}
return null;
}
public TSnackbar setIconRight(@DrawableRes int drawableRes, float sizeDp) {
final TextView tv = mView.getMessageView();
Drawable drawable = ContextCompat.getDrawable(mContext, drawableRes);
if (drawable != null) {
drawable = fitDrawable(drawable, (int) convertDpToPixel(sizeDp, mContext));
} else {
throw new IllegalArgumentException("resource_id is not a valid drawable!");
}
final Drawable[] compoundDrawables = tv.getCompoundDrawables();
tv.setCompoundDrawables(compoundDrawables[0], compoundDrawables[1], drawable, compoundDrawables[3]);
return this;
}
private void setIcon(TextView view, String path) {
Drawable icon = Drawable.createFromPath(path);
Drawable checkbox = view.getCompoundDrawables()[2];
if (icon != null)
icon.setBounds(0, 0, checkbox.getIntrinsicWidth(), checkbox.getIntrinsicHeight());
view.setCompoundDrawables(icon, null, checkbox, null);
}
public static void setTopDrawable(TextView textView, Drawable topDrawable) {
setDrawableBounds(topDrawable);
Drawable left = textView.getCompoundDrawables()[0];
Drawable right = textView.getCompoundDrawables()[2];
Drawable bottom = textView.getCompoundDrawables()[3];
textView.setCompoundDrawables(left, topDrawable, right, bottom);
}
private void switchTab(int pos) {
TextView tv = getChildAtCurPos(pos);
Drawable drawable = tv.getCompoundDrawables()[2];
int level = drawable.getLevel();
if (mOnItemClickListener != null) {
mOnItemClickListener.onItemClick(tv, pos, level == 1);
}
if (mLastIndicatorPosition == pos) {
// 点击同一个条目时
tv.setTextColor(level == 0 ? mTabSelectedColor : mTabDefaultColor);
drawable.setLevel(1 - level);
return;
}
mCurrentIndicatorPosition = pos;
resetPos(mLastIndicatorPosition);
//highLightPos(pos);
tv.setTextColor(mTabSelectedColor);
tv.getCompoundDrawables()[2].setLevel(1);
mLastIndicatorPosition = pos;
}
public static Matcher<View> withCompoundDrawable(final int position) {
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override public void describeTo(Description description) {
description.appendText(
"has compound drawable resource at position " + position);
}
@Override public boolean matchesSafely(TextView textView) {
Drawable drawables[] = textView.getCompoundDrawables();
return (drawables[position] != null);
}
};
}
public static Matcher<View> withoutCompoundDrawable(final int position) {
return new BoundedMatcher<View, TextView>(TextView.class) {
@Override public void describeTo(Description description) {
description.appendText(
"does not have compound drawable at position " + position);
}
@Override public boolean matchesSafely(TextView textView) {
Drawable drawables[] = textView.getCompoundDrawables();
return (drawables[position] == null);
}
};
}
/**
* 将 TextView/RadioButton 中设置的 drawable 动态的缩放
*
* @param percent
* @param tv
* @return
*/
public static Drawable getScaleDrawableForRadioButton(float percent, TextView tv) {
Drawable[] compoundDrawables = tv.getCompoundDrawables();
Drawable drawable = null;
for (Drawable d : compoundDrawables) {
if (d != null) {
drawable = d;
}
}
return getScaleDrawable(percent, drawable);
}
private Drawable getTopDrawable(TextView v) {
Drawable d = v.getCompoundDrawables()[1];
return (d instanceof PreloadIconDrawable) ? ((PreloadIconDrawable) d).mIcon : d;
}