StringBuilder sb =new StringBuilder();
fcs =new ForegroundColorSpan(Color.parseColor("#0096ff"));
fcs2 =new ForegroundColorSpan(Color.parseColor("#000000"));
fcs3 =new ForegroundColorSpan(Color.parseColor("#0096ff"));
fcs4 =new ForegroundColorSpan(Color.parseColor("#000000"));
sb.append(nickname +"送给" +giftReceiver +giftNum +giftUnit +giftUrl);
final String content = sb.toString();
Spannable span = EaseSmileUtils2.getSmiledText2(context, content);
final int NStart = content.indexOf(nickname);
final int NEnd = NStart +nickname.length();
span.setSpan(fcs, NStart, NEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
final int fStart = content.indexOf("送给");
final int fEnd = fStart +"送给".length();
span.setSpan(fcs2, fStart, fEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
int receiverStart = content.indexOf(giftReceiver);
int receiverEnd = receiverStart +giftReceiver.length();
span.setSpan(fcs3, receiverStart, receiverEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
span.setSpan(new TextClick(),receiverStart, receiverEnd, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
int fStart2 = content.indexOf(giftNum +giftUnit);
int fEnd2 = fStart2 + (giftNum +giftUnit).length();
span.setSpan(fcs4, fStart2, fEnd2, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
final int urlStart = content.indexOf(giftUrl);
final int urlEnd = urlStart +giftUrl.length();
final int dip2px = DisplayUtil.dip2px(context, bitmapWidth);
Log.i("", "getView: dip2px: " + dip2px);
final Spannable finalSpan = span;
Glide.with(context.getApplicationContext()).asBitmap().load(giftUrl).into(new SimpleTarget(dip2px, dip2px) {
@Override
public void onResourceReady(Bitmap resource, Transition transition) {
int width = resource.getWidth();
int height = resource.getHeight();
float scale = width/height;
Log.i("", "onResourceReady: scale: "+ scale);
// 设置想要的大小
int newWidth =dip2px;
int newHeight = (int) (dip2px /1.4);
Log.i("", "onResourceReady: newWidth : " + newWidth +" newHight: "+ newHeight);
// 计算缩放比例
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数
Matrix matrix =new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片
resource = Bitmap.createBitmap(resource, 0, 0, width, height, matrix,
true);
finalSpan.setSpan(new CustomImageSpan(resource, 2), urlStart, urlEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
contentView.setText(finalSpan, TextView.BufferType.SPANNABLE);
}
});
/**
* 实现了文字和图片居中对齐
* 需要做的事情是 获取文本内容的中间线以及图片的中间线,然后获取两者差值,
* 然后在draw方法中绘制图片时将差值作为canvas.translate(x, transY) 中的transY;同时要重写 getSize( )。
*/
public classCustomImageSpanextendsImageSpan{ //自定义对齐方式--与文字中间线对齐 private int ALIGN_FONTCENTER = 2;
public CustomImageSpan(Context context, int resourceId) {
super(context, resourceId);
}
public CustomImageSpan(Context context, int resourceId, int verticalAlignment) {
super(context, resourceId, verticalAlignment);
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,
Paint paint) {
//draw 方法是重写的ImageSpan父类 DynamicDrawableSpan中的方法,在DynamicDrawableSpan类中,虽有getCachedDrawable(),
// 但是私有的,不能被调用,所以调用ImageSpan中的getrawable()方法,该方法中 会根据传入的drawable ID ,获取该id对应的
// drawable的流对象,并最终获取drawable对象
Drawable drawable = getDrawable(); //调用imageSpan中的方法获取drawable对象
canvas.save();
//获取画笔的文字绘制时的具体测量数据
Paint.FontMetricsInt fm = paint.getFontMetricsInt();
//系统原有方法,默认是Bottom模式)
int transY = bottom - drawable.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
transY -= fm.descent;
} else if (mVerticalAlignment == ALIGN_FONTCENTER) {
//此处加入判断, 如果是自定义的居中对齐
//与文字的中间线对齐(这种方式不论是否设置行间距都能保障文字的中间线和图片的中间线是对齐的) // y+ascent得到文字内容的顶部坐标,y+descent得到文字的底部坐标,(顶部坐标+底部坐标)/2=文字内容中间线坐标
transY = ((y + fm.descent) + (y + fm.ascent)) / 2 - drawable.getBounds().bottom / 2;
}
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
/**
* 重写getSize方法,只有重写该方法后,才能保证不论是图片大于文字还是文字大于图片,都能实现中间对齐
*/
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
Drawable d = getDrawable();
Rect rect = d.getBounds();
if (fm != null) {
Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt();
int fontHeight = fmPaint.bottom - fmPaint.top;
int drHeight = rect.bottom - rect.top;
int top = drHeight / 2 - fontHeight / 4;
int bottom = drHeight / 2 + fontHeight / 4;
fm.ascent = -bottom;
fm.top = -bottom;
fm.bottom = top;
fm.descent = top;
}
return rect.right;
}
}