How to draw circle by canvas in Android?

How to draw circle by canvas in Android?

You can draw a circle on an Android Canvas using the Canvas's drawCircle method. Here's how to do it step by step:

  1. Create a Custom View or Override onDraw Method:

    You can draw a circle on a custom View by creating a custom View class and overriding the onDraw method. Here's an example:

    import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.view.View; public class CircleView extends View { public CircleView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // Create a Paint object for drawing Paint paint = new Paint(); paint.setColor(Color.RED); // Circle color paint.setStyle(Paint.Style.FILL); // Fill the circle paint.setAntiAlias(true); // Smooth edges // Get the center coordinates of the view int centerX = getWidth() / 2; int centerY = getHeight() / 2; // Define the radius of the circle int radius = 100; // Adjust as needed // Draw the circle on the canvas canvas.drawCircle(centerX, centerY, radius, paint); } } 
  2. Add the Custom View to Your Layout:

    You can add the CircleView to your layout XML file or programmatically in your activity. Here's how to add it to an XML layout file:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- Your other views --> <com.example.yourpackage.CircleView android:id="@+id/circleView" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <!-- Your other views --> </LinearLayout> 

    Replace com.example.yourpackage with the actual package name where your CircleView class is located.

  3. Instantiate and Use the Custom View:

    In your activity, instantiate the CircleView and add it to your layout. For example:

    import android.app.Activity; import android.os.Bundle; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Find the CircleView by its ID CircleView circleView = findViewById(R.id.circleView); // You can customize the circle's appearance or behavior here if needed } } 

    Replace R.layout.activity_main with the appropriate layout resource if you are using a different layout.

  4. Run Your App:

    When you run your app, the custom View will draw a circle on the screen, as specified in the onDraw method.

This example demonstrates how to draw a simple circle on an Android Canvas using a custom View. You can customize the circle's appearance (e.g., color, size, position) by modifying the properties in the onDraw method.


More Tags

graphql-tag ansi-sql android-dialer connectivity modality fastparquet cryptography ora-00933 image-size application-settings

More Java Questions

More Financial Calculators

More Mixtures and solutions Calculators

More Chemical thermodynamics Calculators

More Auto Calculators