简体   繁体   English

帆布画不光滑的圆圈

[英]canvas draw not smooth circles

At the first of my game, I draw some circles from alpha 0 to 255 using canvas(it's like making a fade_in animation by myself) 在游戏的第一阶段,我使用画布从alpha 0到255绘制了一些圆圈(就像我自己制作了fade_in动画一样)

But if you see in picture(this picture captured in alpha 230),from alpha 0 to 254 these circles aren't smooth!(click on picture to see what I mean) 但是,如果您在图片中看到(这张图片是在alpha 230中捕获的),则从alpha 0到254的这些圆圈并不平滑!(单击图片以了解我的意思)

(and only when alpha become 255 the circles become smooth) (并且仅当alpha变为255时圆才变得平滑) 在此处输入图片说明 What's the problem and how can I fix this? 有什么问题,我该如何解决?

my code: I have a game loop, that get canvas 我的代码:我有一个游戏循环,得到了画布

canvas = gameView.getHolder().lockCanvas();

then in my view ,at first I set : 然后在我看来,起初我设置:

    paintAlpha = 0;
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFlags(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeJoin(Paint.Join.ROUND);
    paint.setStrokeCap(Paint.Cap.ROUND);
    paint.setAlpha(paintAlpha);
    paint.setColor(Color.parseColor(color));

then in every loop(every ticks) I do this: 然后在每个循环中(每个刻度),我这样做:

    if(paintAlpha < 255) {
        paintAlpha+=1;
        paint.setAlpha(paintAlpha);
    }
    canvas.drawCircle(cx, cy, currentRadius, paint);

Solution: Thanks to @nitesh. 解决方案:感谢@nitesh。 The problem was because of surfaceView that can't set anti alias to canvas (in View you don't have this problem ,I don't know why) By using Bitmap and draw on it and finally draw bitmap by canvas , the problem solved (instead of drawing on canvas directly) 问题是由于surfaceView无法为画布设置反别名(在View中您没有此问题,我不知道为什么)通过使用Bitmap并在其上绘制,最后通过canvas绘制位图,问题得以解决(而不是直接在画布上绘画)

Set the following property to paint object 设置以下属性以绘制对象

paint.setAntiAlias(true);

For better understanding and other approaches refer this link https://medium.com/@ali.muzaffar/android-why-your-canvas-shapes-arent-smooth-aa2a3f450eb5#.p9iktozdi 为了更好地理解和使用其他方法,请参考此链接https://medium.com/@ali.muzaffar/android-why-your-canvas-shapes-arent-smooth-aa2a3f450eb5#.p9iktozdi

From the article 从文章

Draw a bitmap first if: 如果出现以下情况,请先绘制一个位图:
- You need to persist the image. -您需要保留图像。
- You need to draw transparent pixels. -您需要绘制透明像素。
- Your shapes don't change often and/or require time consuming operations. -您的形状不会经常更改和/或需要耗时的操作。
Use anti-aliasing to draw smooth edges. 使用抗锯齿功能绘制平滑的边缘。
Avoid redraws on the bitmap if possible or else, clear a bitmap before redrawing. 如果可能,请避免在位图上重绘,否则,请在重绘之前清除位图。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    if (bitmap == null) {
        bitmap = Bitmap.createBitmap(200, 
                                     200, 
                                     Bitmap.Config.ARGB_8888);
        bitmapCanvas = new Canvas(bitmap);
    }
    bitmapCanvas.drawColor(
              Color.TRANSPARENT, 
              PorterDuff.Mode.CLEAR); //this line moved outside if
    drawOnCanvas(bitmapCanvas);
    canvas.drawBitmap(bitmap, mLeftX, mTopY, p);
}

protected void drawOnCanvas(Canvas canvas) {
    canvas.drawCircle(mLeftX + 100, mTopY + 100, 100, p);
}

在此处输入图片说明

you can approach this by 你可以通过

paint.setFlags(Paint.ANTI_ALIAS_FLAG);

or 要么

paint.setAntiAlias(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM