在画布上绘制的圆圈与屏幕不匹配 [英] Circle drawn on canvas doesn't match the screen

查看:159
本文介绍了在画布上绘制的圆圈与屏幕不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在画面中央绘制圆形,但我会得到这样的:

I want to draw circle in center of screen, but I'm getting something like this:

我使用这段代码画出这个圆圈。

I'm using this code to draw this circle.

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



我不明白为什么它在两边切割,即使我使用大小屏幕来绘制它,所以它应该完美适合它。

I don't understand why it's cut at sides even though I use size of screen to draw it, so it should perfectly fit it.

推荐答案

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

You didn't account for the thickness of the line (strokeWidth). You drew a circle assuming it had 0 thickness, so the "actual" circle IS touching the edges of the screen, but since you used a thick paintbrush, some of the paint leaked past the edge.

这篇关于在画布上绘制的圆圈与屏幕不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆