// DrawingDemoActivity.java Copyright (c) Kari Laitinen // http://www.naturalprogramming.com/ // 2011-05-12 File created. // 2015-12-08 Last modification. /* This program is an Android application that shows how rectangles, ovals, arcs, lines, etc. can be drawn with the drawing methods of the Canvas class. This app was tested with an emulator with screen size 480x800. This app does not need an XML file. Documentation of the Canvas class can be found at http://developer.android.com/reference/android/graphics/Canvas.html */ package drawing.demo ; import android.app.Activity ; import android.os.Bundle ; import android.graphics.* ; // Classes Canvas, Color, Paint, RectF, etc. import android.view.View ; import android.content.Context ; final class DrawingDemoView extends View { public DrawingDemoView( Context context ) { super( context ) ; } @Override protected void onDraw( Canvas canvas ) { Paint filling_paint = new Paint() ; filling_paint.setStyle( Paint.Style.FILL ) ; Paint outline_paint = new Paint() ; // Initial color is black. outline_paint.setStyle( Paint.Style.STROKE ) ; int view_width = getWidth() ; int view_height = getHeight() ; outline_paint.setTextSize( 24 ) ; canvas.drawText( "View size is " + view_width + " x " + view_height, 20, 40, outline_paint ) ; outline_paint.setStrokeWidth( 4 ) ; outline_paint.setColor( Color.GREEN ) ; // We'll draw a horizontal green line whose length is 240 points. canvas.drawLine( view_width / 2 - 120, view_height / 8, view_width / 2 + 120, view_height / 8, outline_paint ) ; // The size of the following filled rectangle is 160x80 filling_paint.setColor( Color.CYAN ) ; canvas.drawRect( view_width / 2 - 80, // x-coordinate of upper left corner view_height / 4 - 50, // y-coordinate of upper left corner view_width / 2 + 80, // x-coordinate of lower right corner view_height / 4 + 30, // y-coordinate of lower right corner filling_paint ) ; // The size of the following hollow rectangle is 180x100 outline_paint.setColor( Color.BLUE ) ; canvas.drawRect( view_width / 2 - 90, view_height / 4 - 60, view_width / 2 + 90, view_height / 4 + 40, outline_paint ) ; RectF square_around_ball = new RectF( view_width / 2 - 80, view_height / 2 - 80, view_width / 2 + 80, view_height / 2 + 80 ) ; filling_paint.setColor( Color.MAGENTA ) ; canvas.drawOval( square_around_ball, filling_paint ) ; canvas.drawOval( square_around_ball, outline_paint ) ; // The center point of the pacman will be 40 points to the left // from the middle of the screen. RectF square_around_pacman = new RectF( view_width / 2 - 40 - 80, view_height / 5 * 4 - 80, view_width / 2 - 40 + 80, view_height / 5 * 4 + 80 ) ; filling_paint.setColor( Color.YELLOW ) ; outline_paint.setColor( Color.BLACK ) ; // The following calls draw a 'pacman' canvas.drawArc( square_around_pacman, 45, 270, true, filling_paint ) ; canvas.drawArc( square_around_pacman, 45, 270, true, outline_paint ) ; RectF square_around_pie = new RectF( view_width / 2 + 40 - 80, view_height / 5 * 4 - 80, view_width / 2 + 40 + 80, view_height / 5 * 4 + 80 ) ; // The following method calls draw a 'pie'. // If the fourth parameter were false, an arc would be drawn. canvas.drawArc( square_around_pie, 315, 90, true, filling_paint ) ; canvas.drawArc( square_around_pie, 315, 90, true, outline_paint ) ; } } public class DrawingDemoActivity extends Activity { DrawingDemoView drawing_demo_view; @Override public void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ) ; drawing_demo_view = new DrawingDemoView( this ) ; setContentView( drawing_demo_view ) ; } }