프로젝트 개발에서 이러한 수요에 부딪혔다. 라벨(그림)과 문자, 라벨이 제목의 첫머리에 나타나자 자연스럽게 TextView+ImageSpan의 방식으로 해야 한다는 생각이 들었다. 처음에는 생각이 없었다. 인터넷에서 검색해 보면 기본적으로 언급이 있었지만 한 가지 문제가 해결되지 않고 바로 중간에 있었다.아무리 설정해도 안 돼!나중에 한 문장을 찾았는데 ImageSpan의 getsize () 방법이 전시 위치를 설정했습니다!다음은 사용자 정의 수정된 ImageSpan을 보여 드리겠습니다. ImageSpan을 어떻게 사용하는지는 더 이상 말하지 않겠습니다.
/**
* ImageSpan
*
* @author KenChung
*/
public class VerticalImageSpan extends ImageSpan {
public VerticalImageSpan(Drawable drawable) {
super(drawable);
}
public int getSize(Paint paint, CharSequence text, int start, int end,
Paint.FontMetricsInt fontMetricsInt) {
Drawable drawable = getDrawable();
Rect rect = drawable.getBounds();
if (fontMetricsInt != 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;
fontMetricsInt.ascent = -bottom;
fontMetricsInt.top = -bottom;
fontMetricsInt.bottom = top;
fontMetricsInt.descent = top;
}
return rect.right;
}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end,
float x, int top, int y, int bottom, Paint paint) {
Drawable drawable = getDrawable();
canvas.save();
int transY = 0;
transY = ((bottom - top) - drawable.getBounds().bottom) / 2 + top;
canvas.translate(x, transY);
drawable.draw(canvas);
canvas.restore();
}
}