如何在android中绘制画布中的文本

时间:2015-01-29 06:31:58

标签: android text android-canvas

我正在编写一个包含仪表视图的应用程序。我是从here下载的,我希望在仪表视图内(在仪表视图的中心)绘制文本,如何在仪表视图中绘制文本,请帮助我。

我的朋友在堆栈溢出后回答我这样做了但是它没有用,请让它工作

 private void drawGauge() 
    {
        if (null != mBackground) 
        {
           mBackground.recycle();
        }

        /*--For Semi circle put getHeight()/2--*/
        mBackground = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_8888);
        newBitmap = Bitmap.createBitmap(mBackground.getWidth(), mBackground.getHeight(), Config.ARGB_8888);
        Canvas newCanvas = new Canvas(newBitmap);
        final Canvas canvas = new Canvas(mBackground);
        final float scale = Math.min(getWidth(), getHeight());
        canvas.scale(scale, scale);
        canvas.translate((scale == getHeight()) ? ((getWidth() - scale) / 2) / scale : 0,(scale == getWidth()) ? ((getHeight() - scale) / 2) / scale : 0);

        Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintText.setColor(Color.WHITE);
        paintText.setTextSize(50);
        paintText.setTextAlign(Align.CENTER);
        paintText.setStyle(Style.FILL);
        Rect rectText = new Rect();
        paintText.getTextBounds(captionString, 0, captionString.length(), rectText);
        newCanvas.drawText(captionString, 0, rectText.height(), paintText);


        //canvas.drawText("MedeQuip", 200, 500, shadowpaint);
        //drawRim(canvas);
        //drawFace(canvas);

对于vipluv:

 protected void onDraw(final Canvas canvas) {
        drawBackground(canvas);
        final float scale = Math.min(getWidth(), getHeight());
        canvas.scale(scale, scale);
        canvas.translate((scale == getHeight()) ? ((getWidth() - scale) / 2) / scale : 0, (scale == getWidth()) ? ((getHeight() - scale) / 2) / scale : 0);

        if (mShowNeedle) {
            drawNeedle(canvas);
        }

        if (mShowText) {
            drawText(canvas);
        }

        Paint paintText = new Paint(Paint.ANTI_ALIAS_FLAG);
        paintText.setColor(Color.WHITE);
        paintText.setTextSize(150);
        paintText.setTextAlign(Align.CENTER);
        paintText.setStyle(Style.FILL);
        Rect rectText = new Rect();
        paintText.getTextBounds(captionString, 0, captionString.length(), rectText);
        canvas.drawText(captionString, rectText.width(), rectText.height(), paintText);

        computeCurrentValue();
    }

1 个答案:

答案 0 :(得分:0)

在GaugeView.java文件的onDraw()函数中,在调用computeCurrentValue()之前添加一个canvas.drawText()。

使用您自己的Paint对象,并确保已初始化其textize和颜色属性。