如何在自定义视图中填充笔划文本?

时间:2011-12-23 08:56:56

标签: android android-layout android-custom-view

我准备了一个视图代码为

的自定义视图
    mPaint.setDither(true);
            mPaint.setColor(0xFFFFFFFF);
            mPaint.setTextSize(100);
            mPaint.setStyle(Paint.Style.STROKE);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
            mPaint.setStrokeWidth(2);

protected void onDraw(Canvas canvas) {

          canvas.drawText("Android",100,100,mPaint);


    }

结果视图是,

http://i.imgur.com/hh96q.png

这里我希望在“Android”中填充一些颜色。我准备了另外一个绘画对象,

mAlphaInner.setDither(true);
        mAlphaInner.setColor(0xFF0000FF);
        mAlphaInner.setTextSize(98);

在onDraw(),

中添加
 canvas.drawText("Android",100,100,mPaint);

但视图是,

http://i.imgur.com/BtD3G.png

请帮我解决如何在“Android”中填充颜色。

2 个答案:

答案 0 :(得分:0)

如果您尝试mPaint.setStyle(Pain.Style.FILL_AND_STROKE)

,该怎么办?

<强>编辑:

在两个颜料中忘记Paint.setTextAlign(Paint.Align.CENTER)

答案 1 :(得分:-1)

使用两种不同的模式绘制文本两次。

        mPaint.setDither(true);
        mPaint.setColor(0xFFFFFFFF);
        mPaint.setTextSize(100);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(2);

        mAlphaInner.set(mPaint);  //Set the same text size!
        mAlphaInner.setColor(0xFF0000FF);
        mAlphaInner.setStyle(Paint.Style.FILL_AND_STROKE);

    protected void onDraw(Canvas canvas) {
      canvas.drawText("Android",100,100,mAlphaInner);
      canvas.drawText("Android",100,100,mPaint);
   }