2013-08-19 304 views
0

這個問題是繼續我以前的doubt對齊問題的視圖

現在我正在試圖使一個看法。我正在繪製一個比例尺,但對齊不正確,我無法找出問題所在。這裏是我的代碼:

protected void onDraw(Canvas canvas) { 

            super.onDraw(canvas); 

            w= canvas.getWidth(); 
            h=canvas.getHeight(); 
            DrawRange(canvas,innerRadius,outerRadius); 
          } 

here innerRadius = 250; ,外半徑= 300;

private void DrawRange(Canvas canvas,int r,int R) { 
           RectF rect = new RectF(canvas.getWidth()/2- r, canvas.getHeight()/2-r, canvas.getWidth()/2+r, canvas.getHeight()/2+r); 
           RectF rect1 = new RectF(canvas.getWidth()/2- R, canvas.getHeight()/2-R, canvas.getWidth()/2+R, canvas.getHeight()/2+R); 

           Paint scalePaint = new Paint(); 
           scalePaint.setStyle(Paint.Style.STROKE); 
           scalePaint.setColor(0x9f004d0f); 
           scalePaint.setStrokeWidth(2); 
           scalePaint.setAntiAlias(true); 

           scalePaint.setTextSize(35.0f); 
           scalePaint.setTypeface(Typeface.SANS_SERIF); 
           scalePaint.setTextScaleX(0.4f); 
           scalePaint.setTextAlign(Paint.Align.CENTER); 
           canvas.drawOval(rect1, scalePaint); 
           canvas.drawOval(rect, scalePaint); 

           canvas.save(Canvas.CLIP_SAVE_FLAG); 
           int xc = 0; 
           for (int i = 0; i < totalNicks; i++) { 

            float y1 = 330; 
            float y2 = y1 + 5; 

            if (i % 5 == 0) { 
              canvas.drawText(""+xc, r-15, y2 , scalePaint); 



              xc+=5; 
            }else{ 
             canvas.drawLine(r, y1, r, y2, scalePaint); 
            } 

            canvas.rotate(degreesPerNick, w/2, h/2); 

           } 
           canvas.restore(); 

          } 

enter image description here

+0

你是什麼意思時,你說「的定位是不妥當的」?你能描述一下你期望它的樣子嗎,以及實際結果與這個不同嗎? – Michelle

+0

當然我會附上屏幕截圖,以便您能夠理解 – baloo

+0

我猜測的問題是y1和y2的值;暫時我正在讓他們硬編碼,因爲我只是想學習 – baloo

回答

1

我不知道你是繪製文本,並在錯誤的地方破折號。關鍵參考點是圓的中心:

int cX = canvas.getWidth()/2; 
int cY = canvas.getHeight()/2; 

另一個關鍵參考是兩個半徑之間的差:

int deltaR = R - r; 

的破折號和文本在12時總是繪製說20%從外圓內圓到的方式1/3以上:

int dashInnerY = cY - r - deltaR/5; // 20% of the way between inner and outer radii 
int dashOuterY = cY - R + deltaR/3; // 1/3 of the way between outer and inner radii 

然後渲染破折號:

canvas.drawLine(cX, dashInnerY, cX, dashOuterY, scalePaint); 

而且數量:

canvas.drawText(""+xc, cX, dashInnerY, scalePaint); 
+0

這確實是非常有幫助的。現在我可以弄清楚我的錯誤在哪裏......非常感謝 – baloo