0% found this document useful (0 votes)
2 views

Android_Graphics_Canvas

The document explains how to use the Canvas API in Android for drawing shapes, text, and images by overriding the onDraw method. It covers adding shadows with the Paint class, creating gradients using the Shader class, and drawing complex shapes with the Path class. Additionally, it demonstrates how to define a custom view in XML for use in layouts.

Uploaded by

Gunjan Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Android_Graphics_Canvas

The document explains how to use the Canvas API in Android for drawing shapes, text, and images by overriding the onDraw method. It covers adding shadows with the Paint class, creating gradients using the Shader class, and drawing complex shapes with the Path class. Additionally, it demonstrates how to define a custom view in XML for use in layouts.

Uploaded by

Gunjan Anand
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Using Graphics: Canvas, Drawing, Shadows, and Gradients in Android

1. Canvas API for Drawing


The Canvas class provides methods to draw shapes, text, and images in a custom View.
You typically override the onDraw(Canvas canvas) method in a View subclass.

public class CustomView extends View {


private Paint paint;

public CustomView(Context context) {


super(context);
paint = new Paint();
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.FILL);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawCircle(200, 200, 100, paint);
}
}

2. Adding Shadows to Drawings


You can add shadows using the setShadowLayer() method of the Paint class.

paint.setShadowLayer(10, 5, 5, Color.BLACK);
canvas.drawText("Hello, Shadow!", 100, 200, paint);

3. Using Gradients
Gradients allow smooth color transitions. You can use the Shader class with
LinearGradient, RadialGradient, or SweepGradient.

LinearGradient gradient = new LinearGradient(0, 0, 400, 0,


Color.RED, Color.BLUE, Shader.TileMode.CLAMP);
paint.setShader(gradient);
canvas.drawRect(0, 0, 400, 400, paint);

4. Drawing Paths for Custom Shapes


To draw complex shapes, use the Path class.

Path path = new Path();


path.moveTo(100, 100);
path.lineTo(200, 300);
path.lineTo(50, 300);
path.close();
canvas.drawPath(path, paint);

5. Using a Custom View in XML


You can define a custom view in an XML layout.

<com.example.CustomView
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Conclusion
Using Canvas, Paint, and Shader, you can create rich custom graphics in Android.
Whether it's simple shapes, shadows, or complex gradients, these APIs provide full control over UI
design.

You might also like