下面列出了android.text.style.ImageSpan#ALIGN_BOTTOM 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public CharSequence getPageTitle(int position)
{
// Hax from https://guides.codepath.com/android/Google-Play-Style-Tabs-using-TabLayout#design-support-library
// Apparently a workaround for TabLayout not supporting icons.
// TODO This workaround will eventually not be necessary; switch to more legit methods when that is the case
// TODO Also remove additional hax from styles.xml
Drawable drawable = mContext.getResources().getDrawable(TAB_ICONS[position]);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
SpannableString sb = new SpannableString(" ");
sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
public void replaceEmoticons(Editable editable, int start, int count) {
if (count <= 0 || editable.length() < start + count)
return;
CharSequence s = editable.subSequence(start, start + count);
Matcher matcher = Pattern.compile("\\[[^\\[]+\\]").matcher(s);
while (matcher.find()) {
int from = start + matcher.start();
int to = start + matcher.end();
String emot = editable.subSequence(from, to).toString();
emot = emot.substring(1, emot.length() - 1) + ".png";
String key = StickerCategory.emojiMaps.get(emot);
if (!TextUtils.isEmpty(key)) {
emot = key;
}
Drawable d = ResourceUtil.getEmotDrawable(emot);
if (d != null) {
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
/**
* 重写父类的initSpanData方法
* 通过number数组得到每块数值对应的自定义MikyouBackgroundSpan对象
* 然后通过MikyouBackgroundSpan对象定义每块数值的样式包括背景,边框,边框圆角样式,然后将这些对象加入到集合中去
* 通过nonNumber数组得到每个间隔的ForegroundColorSpan对象
* 然后通过这些对象就可以定义每个间隔块的样式,因为只定义了ForegroundColorSpan所以只能定义
* 每个间隔块的字体颜色,setmGapSpanColor方式也是供外部自由定制每个间隔的样式
* 实际上还可以定义其他的Span,同理实现也是很简单的。
* */
@Override
public void initSpanData(String timeStr) {
super.initSpanData(timeStr);
vipNumbers = TimerUtils.getNumInTimerStr(timeStr);//得到每个数字注意不是每块数值,并加入数组
vipNonNumbers = TimerUtils.getNonNumInTimerStr(timeStr);//得到每个间隔字符,并加入到数组
for (int i=0;i<vipNumbers.length;i++){
for (int j=0;j<vipNumbers[i].toCharArray().length;j++){//因为需要得到每个数字所以还得遍历每块数值中的每个数字,所以需要二层循环
MikyouBackgroundSpan mSpan = new MikyouBackgroundSpan(mContext.getDrawable(mDrawableId), ImageSpan.ALIGN_BOTTOM);
initBackSpanStyle(mSpan);
mSpanList.add(mSpan);
}
}
for (int i= 0; i<vipNonNumbers.length;i++){
ForegroundColorSpan mGapSpan = new ForegroundColorSpan(mGapSpanColor);
mTextColorSpanList.add(mGapSpan);
}
}
public static void replaceEmoticons(Context context, Editable editable, int start, int count) {
if (count <= 0 || editable.length() < start + count)
return;
CharSequence s = editable.subSequence(start, start + count);
Matcher matcher = EmoUtil.getPattern().matcher(s);
while (matcher.find()) {
int from = start + matcher.start();
int to = start + matcher.end();
String emot = editable.subSequence(from, to).toString();
Drawable d = getEmotDrawable(context, emot, DEF_SCALE);
if (d != null) {
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
public static void replaceEmoticons(Context context, Editable editable, int start, int count) {
if (count <= 0 || editable.length() < start + count)
return;
CharSequence s = editable.subSequence(start, start + count);
Matcher matcher = EmojiManager.getPattern().matcher(s);
while (matcher.find()) {
int from = start + matcher.start();
int to = start + matcher.end();
String emot = editable.subSequence(from, to).toString();
Drawable d = getEmotDrawable(context, emot, SMALL_SCALE);
if (d != null) {
ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
editable.setSpan(span, from, to, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
}
}
private static void appendDrawable(final Context context, final SpannableStringBuilder builder, final int drawable, int tint) {
final Drawable divider = AppCompatResources.getDrawable(context, drawable)
.mutate();
final int color = (tint == -1) ? R.attr.colorForegroundSecondaryLight : tint;
divider.setColorFilter(ThemeUtil.getColor(context, color), PorterDuff.Mode.SRC_ATOP);
divider.setBounds(0, 0, divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
final ImageSpan span = new ImageSpan(divider, ImageSpan.ALIGN_BOTTOM);
builder.setSpan(span, builder.length() - 1, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
private void appendDrawable(final SpannableStringBuilder builder, final int drawable, int tint) {
final Drawable divider = AppCompatResources.getDrawable(mContext, drawable)
.mutate();
final int color = (tint == -1) ? R.attr.colorForegroundSecondaryLight : tint;
divider.setColorFilter(ThemeUtil.getColor(mContext, color), PorterDuff.Mode.SRC_ATOP);
divider.setBounds(0, 0, divider.getIntrinsicWidth(), divider.getIntrinsicHeight());
final ImageSpan span = new ImageSpan(divider, ImageSpan.ALIGN_BOTTOM);
builder.setSpan(span, builder.length() - 1, builder.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
@Override
public CharSequence getPageTitle(int position) {
SpannableString sb = new SpannableString(sections.get(position) + " ");
if (fragments.size() > position && (fragments.get(position).currentNumTables() > 1)) {
Drawable image = context.getResources().getDrawable(R.drawable.ic_arrow_down_white);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, sb.length() - 1, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
return sb;
}
@BindingAdapter(value={"textDrawable", "textString"})
public static void setSpannableString(@NonNull TextView textView, @NonNull Drawable drawable, String text) {
SpannableString spannableString = new SpannableString(text);
drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
ImageSpan span = new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
spannableString.setSpan(span, spannableString.toString().indexOf("@"), spannableString.toString().indexOf("@")+1, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);
}
private SpannableString getSpannableIcon(int icon, String replacement) {
SpannableString spannableIcon = new SpannableString(replacement);
try {
Drawable image = ContextCompat.getDrawable(this, icon);
if (image != null) {
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
spannableIcon.setSpan(imageSpan, 0, replacement.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
} catch (Resources.NotFoundException e) {
e.printStackTrace();
}
return spannableIcon;
}
private int getImageSpanAlignment() {
switch (mImageSpanAlignment) {
case IMAGE_SPAN_ALIGNMENT_BASELINE:
return ImageSpan.ALIGN_BASELINE;
case IMAGE_SPAN_ALIGNMENT_BOTTOM:
return ImageSpan.ALIGN_BOTTOM;
default:
return ImageSpan.ALIGN_BOTTOM;
}
}
private int getImageSpanAlignment() {
switch (mImageSpanAlignment) {
case IMAGE_SPAN_ALIGNMENT_BASELINE:
return ImageSpan.ALIGN_BASELINE;
case IMAGE_SPAN_ALIGNMENT_BOTTOM:
return ImageSpan.ALIGN_BOTTOM;
default:
return ImageSpan.ALIGN_BOTTOM;
}
}
/**
* 重写父类的initSpanData方法
* 通过number数组得到每块数值对应的自定义MikyouBackgroundSpan对象
* 然后通过MikyouBackgroundSpan对象定义每块数值的样式包括背景,边框,边框圆角样式,然后将这些对象加入到集合中去
* 通过nonNumber数组得到每个间隔的ForegroundColorSpan对象
* 然后通过这些对象就可以定义每个间隔块的样式,因为只定义了ForegroundColorSpan所以只能定义
* 每个间隔块的字体颜色,setmGapSpanColor方式也是供外部自由定制每个间隔的样式
* 实际上还可以定义其他的Span,同理实现也是很简单的。
* */
@Override
public void initSpanData(String timeStr) {
super.initSpanData(timeStr);
for (int i = 0; i<numbers.length;i++){
MikyouBackgroundSpan mBackSpan = new MikyouBackgroundSpan(mContext.getDrawable(mDrawableId), ImageSpan.ALIGN_BOTTOM);
initBackSpanStyle(mBackSpan);
mBackSpanList.add(mBackSpan);
}
for (int i= 0; i<nonNumbers.length;i++){
ForegroundColorSpan mGapSpan = new ForegroundColorSpan(mGapSpanColor);
mTextColorSpanList.add(mGapSpan);
}
}
public CharSequence getPageIcon(int position, int filterColor) {
Drawable image = ResourcesCompat.getDrawable(getResources(), mFragmentIconList.get(position), null);
image.setColorFilter(filterColor, PorterDuff.Mode.MULTIPLY);
image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
SpannableString sb = new SpannableString(" ");
ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
sb.setSpan(imageSpan, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
return sb;
}
public VerticalImageSpan(Drawable drawable) {
super(drawable, ImageSpan.ALIGN_BOTTOM);
}
public VerticalImageSpan(Drawable drawable) {
super(drawable, ImageSpan.ALIGN_BOTTOM);
}
/**
* Create an ImageSpan for the given icon drawable. This also sets the image size and colour.
* Works best with a white, square icon because of the colouring and resizing.
*
* @param context The Android Context.
* @param drawableResId A drawable resource Id.
* @param size The desired size (i.e. width and height) of the image icon in pixels.
* Use the lineHeight of the TextView to make the image inline with the
* surrounding text.
* @param colour The colour (careful: NOT a resource Id) to apply to the image.
* @return An ImageSpan, aligned with the bottom of the text.
*/
private ImageSpan makeImageSpan(Context context, int drawableResId, int size, int colour) {
final Drawable drawable = context.getResources().getDrawable(drawableResId);
drawable.mutate();
drawable.setColorFilter(colour, PorterDuff.Mode.MULTIPLY);
drawable.setBounds(0, 0, size, size);
return new ImageSpan(drawable, ImageSpan.ALIGN_BOTTOM);
}