America

In the onDraw method of AmericaView, the Path draws the star even though the Path crosses itself. radius is a float because the argument of scale is float. x and y are floats because the arguments of moveTo and lineTo are floats.

Source code in America.zip

  1. MainActivity.java
  2. AmericaView.java
  3. R.java
  4. activity_main.xml: unused and ignored.
  5. AndroidManifest.xml
  6. build.gradle (Module: app)
  7. patton.png (104 × 205 pixels)

Create the project

In the Android Studio project view, select the folder app/java/edu.nyu.scps.america. It already holds the file MainActivity.java.
File → New → UI Component → Custom View
View Class: AmericaView
Finish

Put the file patton.png on your Desktop. Control-click on it and select Copy “patton.png”. Control-click on the app/res/drawable folder in the res folder in the Android Studio project view and select Paste.
Copy
Copy file /Users/myname/Desktop/patton.png
New name: patton.png
To directory: /Users/myname/AndroidStudioProjects/America/app/src/main/res/drawable
OK
You should now see patton.png in the Android Studio project view. See Bitmap under Drawable Resources.

Things to try

  1. Would it be easier to draw the seven red stripes with seven calls to translate?
    		canvas.save();
    
    		for (int i = 0; i < 7; ++i) {
    			//left, top, right, bottom
    			canvas.drawRect(0, 0, stripeWidth, height, paint);
    			canvas.translate(2 * stripeWidth, 0);
    		}
    
    		canvas.restore();
    

  2. The image file patton.png is 104 × 205 pixels. Use a piece of Toast to verify that the BitMap object is 312 × 615 pixels (thrice the original), at least on Genymotion simulating the Samsung Galaxy S5. Call getContext to get the first argument for makeToast. Recall that Text showed there were three pixels per dp. See Puzzle to get the dimensions of the originl image file.