2012-05-06 23 views
0

下面的程序繪製一個餡餅(部分圓,一個扇區),而我期望它繪製整個(全圓)圓。如何繪製整個圓圈?如何繪製beyound視圖的翻譯限制?

自定義視圖的代碼:

public class CentralCircleView extends View { 

private Paint circlePaint = new Paint(); 
{ 
    circlePaint.setColor(Color.RED); 
    circlePaint.setAntiAlias(true); 
} 

public CentralCircleView(Context context) { 
    super(context); 
} 

public CentralCircleView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
} 


@Override 
protected void onDraw(Canvas canvas) { 
    canvas.drawCircle(0, 0, 100, circlePaint); 
} 
} 

活動的代碼:

public class TransformsActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    CentralCircleView centralCircleView = (CentralCircleView) findViewById(R.id.centralCircleView); 
    centralCircleView.setTranslationX(200f); 
    centralCircleView.setTranslationY(200f); 
    centralCircleView.invalidate(); 
} 
} 

佈局代碼:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 

> 

<com.inthemoon.incubation.CentralCircleView 
    android:id="@+id/centralCircleView" 
    android:layout_marginLeft="0dp" 
    android:layout_marginTop="0dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    /> 

</RelativeLayout> 

這是它吸引。圓圈的其餘部分在哪裏?

enter image description here

回答

4

你的觀點有其高度設置爲WRAP_CONTENT這意味着你需要實現方法onMeasure()來告訴RelativeLayout的觀點希望有多大是。

+0

感謝您對這個參數提示,但如果我改變它的價值'fill_parent',結果是一樣的。所以這個問題依然存在。 –

+0

那麼,第二個問題是你正在0,0處繪製圓。在Android上,圓的座標表示圓的_center_的座標。在你的情況下,你應該使用drawCircle(100,100,100,paint) –

+0

我知道0,0是一個圓的中心,這就是我想要的。問題是如何獲得完整的圈子,而不是一個部門。 –

-5

要繪製「視野外」,應使用clipRect()方法。例如,在我的情況,我在下面的方式來寫onDraw()

protected void onDraw(Canvas canvas) { 
    canvas.clipRect(new Rect(-100,-100,100,100), Region.Op.UNION); 
    canvas.drawCircle(0, 0, 100, circlePaint); 
} 
+0

你將會遇到使用這種技術的問題。例如,每次需要重繪視圖時,視圖外部的屏幕部分都不會重繪,這會導致各種工件。有各種各樣的黑客可以用來解決這個問題,但最好的解決方案是在你的視圖中完全畫出你的圈子。 –

+0

你說我不能在我的視野外畫畫,不是說它可能但不好。你錯了。接受這個更好,而不是給我一些不足之處。我只是學習Android,我的問題只是爲了瞭解真相。 –

+0

當你這樣做時,你可能會知道Romain寫了大部分的Android平臺代碼。他*是這個話題的真相之源。 :)我們試圖告訴你爲什麼你會看到你所看到的,以及爲什麼將你的未來發展置於這些有缺陷的假設上,如果你試圖依賴它們,會讓你陷入困境。 – adamp