使用paint()在文本外部绘制矩形

loading...


0

我想在动态文本字符串中绘制矩形 . 我已经绘制了一个矩形并在其中绘制文本,但它没有按照我的期望显示 . 以下是我所做的代码 .

public class QuoteSpan extends 
   CharacterStyle implements LeadingMarginSpan {

private final int color;
private String body;

private final int width;
private final int paddingLeft;
private final int paddingRight;

private static final float WIDTH_SP = 2f;
private static final float PADDING_LEFT_SP = 1.5f;
private static final float PADDING_RIGHT_SP = 50f;//8f;

public QuoteSpan(int color, DisplayMetrics metrics) {
    this.color = color;
    this.width = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, WIDTH_SP, metrics);
    this.paddingLeft = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, PADDING_LEFT_SP, metrics);
    this.paddingRight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, PADDING_RIGHT_SP, metrics);
}

@Override
public void updateDrawState(TextPaint tp) {
    tp.setColor(this.color);
}

@Override
public int getLeadingMargin(boolean first) {
    return paddingLeft + width + paddingRight;
}

@Override
public void drawLeadingMargin(Canvas c, Paint p, int x, int dir, int top, int baseline, int bottom,
        CharSequence text, int start, int end, boolean first, Layout layout) {


    System.out.println("Quote::::: QuoteSpan: DrawLeadingMargin start:"+start+" end:"+end+" x: "+x +" top:"+top+ " dir:"+dir+
            " padding Left:"+paddingLeft+" padding right:"+paddingRight+" width: "+ width);
    Paint.Style style = p.getStyle();

    Rect r= new Rect();
    r.width();

    int color = p.getColor();
    p.setStyle(Paint.Style.STROKE);
    p.setColor(this.color);
    float size= p.getTextSize();

    c.drawRect(x + dir * paddingLeft, top, x + dir * (paddingRight + width), bottom, p);

    System.out.println(" Draw Rect: Left:"+ x + dir * paddingLeft+
    " top:"+top+" right:"+x + dir * (paddingLeft + width)+" bottom:"+bottom);

    p.setStyle(style);
    p.setColor(color);
    p.setAlpha(this.getColor());

    p.setTextAlign(Paint.Align.CENTER);
    /*c.drawText(text.toString(), x + dir * paddingLeft, x + dir * (paddingRight + width) ,p);*/

}

@ColorInt
public int getColor() {
    return this.color;
}
}

下图显示了上述代码的输出:

问题是我无法在框的中心添加动态矩形框和文本 . 提前致谢 .

loading...

0回答

No Data

评论

暂时没有评论!