2012-08-07 38 views
0

所以我有这个函数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); 

       } 

而且它做什么,它吸引的是旋转和文本也旋转箭头接近我想要的是一个静态文本箭头箭头下的某处......或类似的东西。

+0

我想通了!你必须做一个canvas.save();在旋转canvas和canvas.restore()之前;在使用画布绘制文本将是静态之前! – exilonX 2012-08-08 00:03:37

回答

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); 

} 

希望有所帮助。