0

以下程序绘制了一个饼图(部分圆形,一个扇区),而我希望它绘制整个(整圆)圆形。如何画出整个圆圈?

自定义视图代码:

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>

这就是它所绘制的。圈子的其余部分在哪里?

在此处输入图像描述

4

2 回答 2

4

您的 View 将其高度设置为 wrap_content 这意味着您需要实现 onMeasure() 方法来告诉 RelativeLayout 视图想要多大。

于 2012-05-06T19:31:31.823 回答
-5

To draw "outside the view" one should use clipRect() method. For example, in my case, I was to write onDraw() in the following way:

protected void onDraw(Canvas canvas) {
    canvas.clipRect(new Rect(-100,-100,100,100), Region.Op.UNION);
    canvas.drawCircle(0, 0, 100, circlePaint);
}
于 2012-05-07T15:41:41.367 回答