2010-04-16 21 views
66

我有一個視圖,我使用onDraw(Canvas canvas)方法中的Canvas對象進行繪製。我的代碼是:Android Canvas.drawText

Paint paint = new Paint(); 
paint.setColor(Color.WHITE); 
paint.setStyle(Style.FILL); 
canvas.drawPaint(paint); 

paint.setColor(android.R.color.black); 
paint.setTextSize(20); 
canvas.drawText("Some Text", 10, 25, paint); 

問題是文本沒有通過背景顯示,我做錯了什麼?如果我刪除canvas.drawPaint(paint)和paint.setColor(android.R.color.black),你可以看到屏幕上的文字.....

回答

117

解決了這個問題,事實證明android.R .color.black與Color.BLACK不同。將代碼更改爲:

Paint paint = new Paint(); 
paint.setColor(Color.WHITE); 
paint.setStyle(Style.FILL); 
canvas.drawPaint(paint); 

paint.setColor(Color.BLACK); 
paint.setTextSize(20); 
canvas.drawText("Some Text", 10, 25, paint); 

它現在一切正常!

+32

是。如果您想使用ID爲R.color.black的'res/colors.xml'文件中的顏色定義,則不能只使用該ID。如果你想從資源中獲得實際的顏色值,可以使用'paint.setColor(getResources()。getColor(R.color.black));' – 2011-12-07 20:49:56

+0

任何人都知道如何用[RectShape]在Android Canvas ShapeDrawable中繪製文本。 http://stackoverflow.com/questions/25401981/how-to-display-text-in-android-canvas-shapedrawable-with-rectshape)? – 2014-08-20 10:51:52

+0

並在'dp'中設置文本大小,您可以使用[this](http://stackoverflow.com/questions/3061930/how-to-set-unit-for-paint-settextsize) – 2016-05-31 13:46:51

11

應該指出的是,documentation建議直接使用Layout而不是Canvas.drawText。我關於使用StaticLayout的完整答案是here,但我將在下面提供一個摘要。

String text = "This is some text."; 

TextPaint textPaint = new TextPaint(); 
textPaint.setAntiAlias(true); 
textPaint.setTextSize(16 * getResources().getDisplayMetrics().density); 
textPaint.setColor(0xFF000000); 

int width = (int) textPaint.measureText(text); 
StaticLayout staticLayout = new StaticLayout(text, textPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
staticLayout.draw(canvas); 

以下是在自定義視圖的上下文中更充分地例如:

enter image description here

public class MyView extends View { 

    String mText = "This is some text."; 
    TextPaint mTextPaint; 
    StaticLayout mStaticLayout; 

    // use this constructor if creating MyView programmatically 
    public MyView(Context context) { 
     super(context); 
     initLabelView(); 
    } 

    // this constructor is used when created from xml 
    public MyView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     initLabelView(); 
    } 

    private void initLabelView() { 
     mTextPaint = new TextPaint(); 
     mTextPaint.setAntiAlias(true); 
     mTextPaint.setTextSize(16 * getResources().getDisplayMetrics().density); 
     mTextPaint.setColor(0xFF000000); 

     // default to a single line of text 
     int width = (int) mTextPaint.measureText(mText); 
     mStaticLayout = new StaticLayout(mText, mTextPaint, (int) width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 

     // New API alternate 
     // 
     // StaticLayout.Builder builder = StaticLayout.Builder.obtain(mText, 0, mText.length(), mTextPaint, width) 
     //  .setAlignment(Layout.Alignment.ALIGN_NORMAL) 
     //  .setLineSpacing(1, 0) // multiplier, add 
     //  .setIncludePad(false); 
     // mStaticLayout = builder.build(); 
    } 

    @Override 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     // Tell the parent layout how big this view would like to be 
     // but still respect any requirements (measure specs) that are passed down. 

     // determine the width 
     int width; 
     int widthMode = MeasureSpec.getMode(widthMeasureSpec); 
     int widthRequirement = MeasureSpec.getSize(widthMeasureSpec); 
     if (widthMode == MeasureSpec.EXACTLY) { 
      width = widthRequirement; 
     } else { 
      width = mStaticLayout.getWidth() + getPaddingLeft() + getPaddingRight(); 
      if (widthMode == MeasureSpec.AT_MOST) { 
       if (width > widthRequirement) { 
        width = widthRequirement; 
        // too long for a single line so relayout as multiline 
        mStaticLayout = new StaticLayout(mText, mTextPaint, width, Layout.Alignment.ALIGN_NORMAL, 1.0f, 0, false); 
       } 
      } 
     } 

     // determine the height 
     int height; 
     int heightMode = MeasureSpec.getMode(heightMeasureSpec); 
     int heightRequirement = MeasureSpec.getSize(heightMeasureSpec); 
     if (heightMode == MeasureSpec.EXACTLY) { 
      height = heightRequirement; 
     } else { 
      height = mStaticLayout.getHeight() + getPaddingTop() + getPaddingBottom(); 
      if (heightMode == MeasureSpec.AT_MOST) { 
       height = Math.min(height, heightRequirement); 
      } 
     } 

     // Required call: set width and height 
     setMeasuredDimension(width, height); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     // do as little as possible inside onDraw to improve performance 

     // draw the text on the canvas after adjusting for padding 
     canvas.save(); 
     canvas.translate(getPaddingLeft(), getPaddingTop()); 
     mStaticLayout.draw(canvas); 
     canvas.restore(); 
    } 
} 
相關問題