5

我想在屏幕中心画圆,但我得到了这样的东西:

在此处输入图像描述

我正在使用这段代码来绘制这个圆圈。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();

    Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_4444);

    Canvas c = new Canvas(bmp);

    RectF rect = new RectF(0,0,width,width);
    drawCircle(rect, c, width, height);
    ImageView img = (ImageView) findViewById(R.id.imageView1);
    img.setImageBitmap(bmp);
    img.setScaleType(ScaleType.FIT_CENTER);


}

private void drawCircle(RectF rect, Canvas c, int width, int height) {
    Paint paint = new Paint();
    paint.setARGB(255, 255 , 10, 21);
    paint.setStrokeWidth(10);
    paint.setAntiAlias(true);
    paint.setStrokeCap(Paint.Cap.BUTT);
    paint.setStyle(Paint.Style.STROKE);
    int radius;
    if(width < height)
        radius = width/2;
    else 
        radius = height/2;
    c.drawCircle(width/2, height/2, radius, paint);
}

即使我使用屏幕大小来绘制它,我也不明白为什么它在侧面被切割,所以它应该完全适合它。

4

3 回答 3

8

您没有考虑线条的粗细(strokeWidth)。你画了一个圆圈,假设它的厚度为 0,所以“实际”圆圈接触屏幕的边缘,但由于你使用了厚画笔,一些油漆从边缘漏了出来。

于 2013-01-08T14:56:18.900 回答
2

你应该减少厚度/2。

    private void drawCircle(RectF rect, Canvas c, int width, int height) {
        Paint paint = new Paint();
        paint.setARGB(255, 255 , 10, 21);
        paint.setStrokeWidth(10);
        paint.setAntiAlias(true);
        paint.setStrokeCap(Paint.Cap.BUTT);
        paint.setStyle(Paint.Style.STROKE);
        int radius;
        if(width < height)
            radius = width/2;
        else 
            radius = height/2;

//this is the new line:
        radius-= 5;
        c.drawCircle(width/2, height/2, radius, paint);
    }
于 2013-01-08T14:59:30.133 回答
1

StrokeWidth半径:

// Substract stroke width.
radius -= paint.getStrokeWidth() / 2;
c.drawCircle(width/2, height/2, radius, paint);
于 2013-01-08T14:57:43.720 回答