前言
我们在开发金融类应用中经常遇到某某大,某某小的数字显示,还有中间有下划线数字,这些其实可以归纳统称为富文本文字,在Android解决中就有Spannable类可供解决,但是其富文本支持标签有限,查看TextView源码可知他会对传进的对象判断是否为Spannable再做对应处理,无法识别的会移除,虽然是这样,但是不影响今天所做的第一个实验,金融类大小数字
-
SpannableString
public static SpannableString spannableString(String str){
SpannableString spannableString = new SpannableString(str);
RelativeSizeSpan sizeSpan01 = new RelativeSizeSpan(1.3f);
if(str.length()==1){
//只有百分号
SpannableString temp = new SpannableString("");
temp.setSpan(sizeSpan01,0,str.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
return temp;
}else if(str.length()<=2){
//只有1位数字
spannableString.setSpan(sizeSpan01, 0, str.length()-1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}else {
//超过1为数字
if (str.contains(".")) {
//有小数点
int index = str.lastIndexOf(".");
spannableString.setSpan(sizeSpan01, 0, index, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
} else {
//无小数点
spannableString.setSpan(sizeSpan01, 0, str.length()-1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
}
}
return spannableString;
}
-
Html.from()方法显现html中的 图片
相信有很多网友跟我一样,在百度中看到有人介绍图片加载到数组中,但是写法上遇到了困难,停在那里,这里有找到的一个很好的富文本显示,与我一样稍作修改肯定就能满足你们的需求。
这里其实也是实现ImageGetter 接口加载的图片
@SuppressLint("AppCompatCustomView")
public class RichText extends TextView {
private Drawable placeHolder, errorImage;//占位图,错误图
private OnImageClickListener onImageClickListener;//图片点击回调
private HashSet<Target> targets;
private int d_w = 200;
private int d_h = 200;
public RichText(Context context) {
this(context, null);
init(context);
}
public RichText(Context context, AttributeSet attrs) {
this(context, attrs, 0);
init(context);
}
public RichText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context);
}
private void init(Context context){
targets = new HashSet<>();
d_w=50;
d_h=50;
placeHolder = context.getResources().getDrawable(R.drawable.ic_img_loading_place_holder);
if (placeHolder == null) {
placeHolder = new ColorDrawable(Color.GRAY);
}
placeHolder.setBounds(0, 0, d_w, d_h);
errorImage = context.getResources().getDrawable(R.drawable.ic_img_load_error);
if (errorImage == null) {
errorImage = new ColorDrawable(Color.GRAY);
}
errorImage.setBounds(0, 0, d_w, d_h);
}
/**
* 设置富文本
* @param text 富文本
*/
public void setRichText(String text) {
targets.clear();
Spanned spanned = Html.fromHtml(text, asyncImageGetter, null);
SpannableStringBuilder spannableStringBuilder;
if (spanned instanceof SpannableStringBuilder) {
spannableStringBuilder = (SpannableStringBuilder) spanned;
} else {
spannableStringBuilder = new SpannableStringBuilder(spanned);
}
ImageSpan[] imageSpans = spannableStringBuilder.getSpans(0, spannableStringBuilder.length(), ImageSpan.class);
final List<String> imageUrls = new ArrayList<>();
for (int i = 0, size = imageSpans.length; i < size; i++) {
ImageSpan imageSpan = imageSpans[i];
String imageUrl = imageSpan.getSource();
int start = spannableStringBuilder.getSpanStart(imageSpan);
int end = spannableStringBuilder.getSpanEnd(imageSpan);
imageUrls.add(imageUrl);
final int finalI = i;
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View widget) {
if (onImageClickListener != null) {
onImageClickListener.imageClicked(imageUrls, finalI);
}
}
};
ClickableSpan[] clickableSpans = spannableStringBuilder.getSpans(start, end, ClickableSpan.class);
if (clickableSpans != null && clickableSpans.length != 0) {
for (ClickableSpan cs : clickableSpans) {
spannableStringBuilder.removeSpan(cs);
}
}
spannableStringBuilder.setSpan(clickableSpan, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
super.setText(spanned);
setMovementMethod(LinkMovementMethod.getInstance());
}
private void addTarget(Target target) {
targets.add(target);
}
/**
* 异步加载图片(依赖于Picasso)
*/
private Html.ImageGetter asyncImageGetter = new Html.ImageGetter() {
@Override
public Drawable getDrawable(String url) {
final URLDrawable urlDrawable = new URLDrawable();
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Drawable drawable = new BitmapDrawable(getContext().getResources(), bitmap);
drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
urlDrawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
urlDrawable.setDrawable(drawable);
RichText.this.setText(getText());
Log.i("mtg","图片加载成功");
}
@Override
public void onBitmapFailed(Exception e, Drawable errorDrawable) {
urlDrawable.setBounds(errorDrawable.getBounds());
urlDrawable.setDrawable(errorDrawable);
Log.i("mtg","图片加载失败");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
urlDrawable.setBounds(placeHolderDrawable.getBounds());
urlDrawable.setDrawable(placeHolderDrawable);
Log.i("mtg","图片加载开始加载");
}
};
addTarget(target);
String uri = REQUEST_LIST + url;
// uri="https://static.firefoxchina.cn/img/201807/5_5b5920a27d11d0.jpg";
Log.i("mgg","picasso加载图片:"+uri);
Picasso.get()
.load(uri)
.placeholder(placeHolder)
.error(errorImage)
.into(target);
return urlDrawable;
}
};
private static final class URLDrawable extends BitmapDrawable {
private Drawable drawable;
@SuppressWarnings("deprecation")
public URLDrawable() {
}
@Override
public void draw(Canvas canvas) {
if (drawable != null)
drawable.draw(canvas);
}
public void setDrawable(Drawable drawable) {
this.drawable = drawable;
}
}
public void setPlaceHolder(Drawable placeHolder) {
this.placeHolder = placeHolder;
this.placeHolder.setBounds(0, 0, d_w, d_h);
}
public void setErrorImage(Drawable errorImage) {
this.errorImage = errorImage;
this.errorImage.setBounds(0, 0, d_w, d_h);
}
public void setOnImageClickListener(OnImageClickListener onImageClickListener) {
this.onImageClickListener = onImageClickListener;
}
public interface OnImageClickListener {
/**
* 图片被点击后的回调方法
*
* @param imageUrls 本篇富文本内容里的全部图片
* @param position 点击处图片在imageUrls中的位置
*/
void imageClicked(List<String> imageUrls, int position);
}
ic_img_load_error.png
ic_img_loading_place_holder.png
网友评论