Can I draw with antialiasing on canvas?

AndroidGraphicsAndroid CanvasAntialiasing

Android Problem Overview


Can I draw with anti-aliasing on canvas?

I need my circles and line have smooth edges.

Android Solutions


Solution 1 - Android

Drawing operations want Paint. In this Paint you set Paint.setFlags(Paint.ANTI_ALIAS_FLAG)

Solution 2 - Android

Check this out. It fairly uses smooth edges.. http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/FingerPaint.html

The paint properties needed to get anti-aliasing is :

     mPaint = new Paint();
     mPaint.setAntiAlias(true);

For drawing use:

     mPath = new Path();
     mPath.reset();
     mPath.moveTo(x, y);//can be used where to trigger the path

onDraw method should contain:

     canvas.drawPath(mPath, mPaint);

Declare the mPath and mPaint as global.

Solution 3 - Android

You can provide ANTI_ALIAS_FLAG in Paint constructor Paint(int) . The ANTI_ALIAS_FLAG flag enables antialiasing when drawing. Enabling this flag will cause all draw operations that support antialiasing to use it.

Or you can set this flag later using setFlags(int) method

Example implementation In Kotlin:

 //Paint
private var indicatorPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
    isAntiAlias = true
    color = fillColor
    strokeWidth = lineThickness.toFloat()
    style = Paint.Style.STROKE
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSuzan CiocView Question on Stackoverflow
Solution 1 - AndroidAlexander KulyakhtinView Answer on Stackoverflow
Solution 2 - AndroidArun ChettoorView Answer on Stackoverflow
Solution 3 - AndroidHitesh SahuView Answer on Stackoverflow