2017-07-07 7 views
3

同じ種類の質問が多いことは知っていますが、私は非常に多くのソリューションを試しましたが、それらのすべてが自分の要件を満たしていません。ImageSpanとスパン可能な文字列の整列

私の問題は、Spanable文字列とImagespanを含むテキストの間に動的な行間を追加する必要がありますが、行間を追加すると、テキストとイメージの配置が歪んでしまいます。

私は、静脈内に、ほぼすべてのthisのようなStackOverflowの上で利用可能なソリューションの、this & thisが、すべてを試してみました。 私は任意のヘルプは高く評価されます

Screenshot after adding dynamic line spacing

間隔ダイナミックなラインを追加した後

Screenshot before adding dynamic line spacing 2.スクリーンショット間隔ダイナミックラインを追加する前に、スクリーンショット

  1. スクリーンショットを添付しています。前もって感謝します! onDrawの方法で「Y」

答えて

2

使用は、テキストのベースラインを検索し、テキストビューのベースラインに描画可能を揃える

public class VerticalImageSpan extends ImageSpan { 



public VerticalImageSpan(Drawable drawable) { 
     super(drawable); 
    } 

    /** 
    * update the text line height 
    */ 
    @Override 
    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.descent - fmPaint.ascent; 
      int drHeight = rect.bottom - rect.top; 
      int centerY = fmPaint.ascent + fontHeight/2; 

      fontMetricsInt.ascent = centerY - drHeight/2; 
      fontMetricsInt.top = fontMetricsInt.ascent; 
      fontMetricsInt.bottom = centerY + drHeight/2; 
      fontMetricsInt.descent = fontMetricsInt.bottom; 
     } 
     return rect.right; 
    } 

    /** 
    * see detail message in android.text.TextLine 
    * 
    * @param canvas the canvas, can be null if not rendering 
    * @param text the text to be draw 
    * @param start the text start position 
    * @param end the text end position 
    * @param x  the edge of the replacement closest to the leading margin 
    * @param top the top of the line 
    * @param y  the baseline 
    * @param bottom the bottom of the line 
    * @param paint the work paint 
    */ 
    @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(); 
     Paint.FontMetricsInt fmPaint = paint.getFontMetricsInt(); 
     int fontHeight = fmPaint.descent - fmPaint.ascent; 
     int centerY = y + fmPaint.descent - fontHeight/2; 
     int transY = centerY - (drawable.getBounds().bottom - drawable.getBounds().top)/2; 
     canvas.translate(x, transY); 
     drawable.draw(canvas); 
     canvas.restore(); 
    } 

} 
関連する問題