簡體   English   中英

畫布上繪制的圓圈與屏幕不匹配

[英]Circle drawn on canvas doesn't match the screen

我想在屏幕中心畫圓圈,但我得到的是這樣的東西:

在此輸入圖像描述

我正在使用此代碼繪制此圓圈。

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

我不明白為什么它會在側面切割,即使我使用屏幕尺寸來繪制它,所以它應該完全適合它。

您沒有考慮線條的粗細(strokeWidth)。 假設它有0厚度,你畫了一個圓圈,所以“實際”圓圈觸及屏幕的邊緣,但由於你使用了厚厚的畫筆,一些油漆漏過了邊緣。

你應該減少厚度/ 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);
    }

考慮半徑中的StrokeWidth

// Substract stroke width.
radius -= paint.getStrokeWidth() / 2;
c.drawCircle(width/2, height/2, radius, paint);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM