我正在使用以下代码实现表情符号:
builder.setSpan(new ImageSpan(mContext, resId, ImageSpan.ALIGN_BASELINE),
start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE );
Run Code Online (Sandbox Code Playgroud)
结果(构建器)被设置为TextView的文本.如果跨度被文本包围,即当开始> 0和结束<长度 - 1时,它可以正常工作,但如果周围没有文本,则图像被切断(向上移动).我该如何解决这个问题?
非常感谢.
继续你的解决方案,这是我DynamicDrawableSpan用来代替我的自定义ImageSpan.该draw()方法(从DynamicDrawableSpan复制并修改)确保在与基线对齐之前存在文本.
class StickerSpan extends DynamicDrawableSpan {
Drawable mDrawable;
public StickerSpan(Context context, Bitmap bitmap) {
super(DynamicDrawableSpan.ALIGN_BASELINE);
setBitmap(context, bitmap);
}
public void setBitmap(Context context, Bitmap bitmap) {
mDrawable = new BitmapDrawable(context.getResources(), bitmap);
int width = mDrawable.getIntrinsicWidth();
int height = mDrawable.getIntrinsicHeight();
mDrawable.setBounds(0, 0, width > 0 ? width : 0, height > 0 ? height : 0);
}
@Override
public Drawable getDrawable() {
return mDrawable;
}
@Override
public void draw(Canvas canvas, CharSequence text,
int start, int end, float x,
int top, int y, int bottom, Paint paint) {
Drawable b = mDrawable;
canvas.save();
int transY = bottom - b.getBounds().bottom;
if (mVerticalAlignment == ALIGN_BASELINE) {
int textLength = text.length();
for (int i = 0; i < textLength; i++) {
if (Character.isLetterOrDigit(text.charAt(i))) {
transY -= paint.getFontMetricsInt().descent;
break;
}
}
}
canvas.translate(x, transY);
b.draw(canvas);
canvas.restore();
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8804 次 |
| 最近记录: |