在旋转画布中绘制静态文本

时间:2012-08-07 23:59:51

标签: android canvas

所以我有这个onDraw函数

protected void onDraw(Canvas canvas) {
                    Paint paint = mPaint;

                    canvas.drawColor(Color.WHITE);

                    paint.setAntiAlias(true);
                    paint.setColor(Color.BLACK);
                    paint.setStyle(Paint.Style.FILL);

                    int w = canvas.getWidth();
                    int h = canvas.getHeight();
                    int cx = w / 2;
                    int cy = h / 2;

                    canvas.translate(cx, cy);
                    if (mValues != null) {            
                        canvas.rotate(-mValues[0]);
                    }
                    canvas.drawPath(mPath, mPaint);

                    Paint paint1 = new Paint(); 


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

                }

它做了什么它绘制了一个旋转的箭头,文字也在箭头附近旋转,我想要的是箭头下某处的静态文本...或类似的东西。

1 个答案:

答案 0 :(得分:1)

使用canvas.save();canvas.restore();之类的内容:

protected void onDraw(Canvas canvas) {
    Paint paint = mPaint;

    canvas.drawColor(Color.WHITE);

    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);
    paint.setStyle(Paint.Style.FILL);

    int w = canvas.getWidth();
    int h = canvas.getHeight();
    int cx = w / 2;
    int cy = h / 2;

    canvas.save();
    canvas.translate(cx, cy);
    if (mValues != null) {            
           canvas.rotate(-mValues[0]);
    }
    canvas.drawPath(mPath, mPaint);
    canvans.restore();

    Paint paint1 = new Paint(); 


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

}

希望有所帮助。