2012-02-13 127 views
4

我想了解如何在畫布上繪製垂直文本。對不起,也許愚蠢的問題,但我不能解決這個問題。我可以這樣做:在畫布上垂直繪製文本

if (i ==10) 
     { 
      this_str2 = "0.00"; 
     } 


       canvas.save(); 
       canvas.rotate(-90,190,90); 
       canvas.drawText(this_str2, x_guide +50, drawSizes[1] + drawSizes[3] - (i * drawSizes[3]/10) +20, paint); 
       canvas.restore(); 
     } 

但它不能正常顯示在X和Y是任何其他辦法解決這個問題7

回答

4

試試這個自定義TextView的,我不記得我了,如果從(這裏在StackOverflow中),如果有人記得,請在評論中發佈一個鏈接。

import android.content.Context; 
import android.graphics.Canvas; 
import android.text.TextPaint; 
import android.util.AttributeSet; 
import android.view.Gravity; 
import android.widget.TextView; 

public class VerticalTextView extends TextView 
{ 
    final boolean topDown; 

    public VerticalTextView(Context context, AttributeSet attrs) 
    { 
     super(context, attrs); 
     final int gravity = getGravity(); 
     if (Gravity.isVertical(gravity) && (gravity & Gravity.VERTICAL_GRAVITY_MASK) == Gravity.BOTTOM) 
     { 
      setGravity((gravity & Gravity.HORIZONTAL_GRAVITY_MASK) | Gravity.TOP); 
      topDown = false; 
     } 
     else 
      topDown = true; 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
    { 
     super.onMeasure(heightMeasureSpec, widthMeasureSpec); 
     setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth()); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) 
    { 
     TextPaint textPaint = getPaint(); 
     textPaint.setColor(getCurrentTextColor()); 
     textPaint.drawableState = getDrawableState(); 

     canvas.save(); 

     if (topDown) 
     { 
      canvas.translate(getWidth(), 0); 
      canvas.rotate(90); 
     } 
     else 
     { 
      canvas.translate(0, getHeight()); 
      canvas.rotate(-90); 
     } 

     canvas.translate(getCompoundPaddingLeft(), getExtendedPaddingTop()); 

     getLayout().draw(canvas); 
     canvas.restore(); 
    } 
} 
+1

http://blog.stylingandroid.com/archives/796 – 2013-06-13 17:18:38