Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.

Slides:



Advertisements
Similar presentations
Games in Python – the easy way
Advertisements

2D Graphics Drawing Things. Graphics In your GUI, you might want to draw graphics E.g. draw lines, circles, shapes, draw strings etc The Graphics class.
Frame Windows A frame object is used to create a graphical frame window. This frame is used to show information in a graphical application. The JFrame.
LAB SESSION 7 Graphical user interface Applet fundamentals Methods in applets Execution of an applet Graphics class.
Chapter 5 Programming Graphics. Chapter Goals To be able to write applications with simple graphical user interfaces To display graphical shapes such.
More Java Drawing in 2D Animations with Timer. Drawing Review A simple two-dimensional coordinate system exists for each graphics context or drawing surface.
Graphics with Canvas, SurfaceView, and multitouch processing (panning and multitouch zoom) GraphicsWithCanvas_2012.pptx.
GUI and Swing, part 2 The illustrated edition. Scroll bars As we have previously seen, a JTextArea has a fixed size, but the amount of text that can be.
Android Form Elements. Views Provide common UI functionality Form elements: text area, button, radio button, checkbox, dropdown list, etc. Date and time.
ObjectDraw and Objects Early Chris Nevison Barbara Wells.
Java Review Structure of a graphics program. Computer Graphics and User Interfaces Java is Object-Oriented A program uses objects to model the solution.
Crash Course in Android Development. 2 Content  Installing the ADT  Hardware and OS requirements  Java  ADT Bundle  Eclipse Project Setup  Drawing.
2D Graphics: Part 1. Android Graphics Libraries 2D Graphics –custom 2D graphics library in packages android.graphics android.graphics.drawable android.graphics.drawable.shapes.
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
Android Application Development 2013 PClassic Chris Murphy 1.
Chapter 5 - Making Music: An On-Screen Piano
Introduction to Android Programming Content Basic environmental structure Building a simple app Debugging.
2D Graphics: Part 2.
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Exploring 2D Graphics: The Sudoku Example Content taken from book: “Hello, Android” by Ed Burnette Third Edition.
Android How to Program Presented by Thomas Bucag, Rob Goodfellowe, Samantha Tomeï © by Pearson Education, Inc. All Rights Reserved.
Doodlz App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
1 Mobile Computing Advanced Touching Copyright 2014 by Janson Industries Assg Part1Assg Part1 AssgPart2AssgPart2.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
Programming Handheld and Mobile devices 1 Programming of Handheld and Mobile Devices Lecture 13 lcdui and OXO Rob Pooley
Graphics Concepts CS 2302, Fall /3/20142 Drawing Paths.
Java GUI’s are event driven, meaning they generate events when the user interacts with the program. Typical events are moving the mouse, clicking a mouse.
© 2011 Delmar, Cengage Learning Chapter 1 Getting to Know Illustrator.
© A+ Computer Science - Chicken yeller = new Chicken();
Java Graphics. Review 3 kinds of elements in components API? Layout managers Events Extend vs. Implement.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
Tutorial 2 Drawing Shapes, Adding Text, and Creating Symbols.
Android Graphics Library. Color Android colors are represented with four numbers, one each for alpha, red, green, and blue (ARGB). Each component can.
Custom Widget 1 UNIT 26 로봇 SW 콘텐츠 교육원 조용수. 캔버스 public void drawColor(int color) 2 public class ControllerView extends View { public ControllerView(Context.
CS378 - Mobile Computing Responsiveness. An App Idea From Nifty Assignments Draw a picture use randomness Pick an equation at random Operators in the.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Agenda Java Coordinate Systems. Graphics Class. Drawing on Panels. Drawing Shapes.
(C) 2010 Pearson Education, Inc. All rights reserved.  Class Graphics (from package java.awt) provides various methods for drawing text and shapes onto.
Announcements Homework #2 will be posted after class due Thursday Feb 7, 1:30pm you may work with one other person No office hours tonight (sorry!) I will.
Graphics basic 1. 2 Objectives Understand Java coordinate systems. Draw things using the methods in the Graphics class. Override the paintComponent method.
Android Threads. Threads Android will show an “ANR” error if a View does not return from handling an event within 5 seconds Or, if some code running in.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Introduction to Java Chapter 8 - Introduction to Java Graphics1 Chapter 8 Introduction to Java Graphics.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved COS240 O-O Languages AUBG,
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
By: Eliav Menachi.  Android custom 2D graphics library  OpenGL ES 1.0 for high performance 3D graphics.
David Sutton 2D GRAPHICS IN ANDROID. INTRODUCTION AND OUTLINE  In this week’s session we will create a simple Kaleidoscope application. Topics that will.
CHAPTER 5 Graphics Drawing Audio. Chapter objectives: Learn to draw to a canvas Examine the process of adding drawn elements and UI controls to a layout.
The Doodlz app enables you to paint by dragging one or more fingers across the screen. The app provides options for setting the drawing color.
Android Application 2D Graphics cs.
Lecture 3: Animation & Graphics
CS499 – Mobile Application Development
Lecture 3: Animation & Graphics
Mobile Computing With Android ACST Intro to Android, Part 2
2D Graphics: Part 2.
Android Layouts 24 July 2018 S.RENUKADEVI/AP/SCD/ANDROID LAYOUTS 1.
Example: Card Game Create a class called “Card”
Mobile Computing With Android ACST 4550 Bitmaps, Fonts and Gestures
Miscellaneous Topics #6: Polygons GUI Components and Event Handlers
Mobile Computing With Android ACST 4550 Android Logs and Gestures
Android Programming Lecture 6
Graphics with Canvas.
Cannon Game App Android How to Program
A+ Computer Science METHODS.
Chapter 8: Graphics, Animations, Sounds, and Gaming
Getting Started with Milestone 2
Lecture 3: Animation & Graphics
A+ Computer Science METHODS.
Mobile Programming Gestures in Android.
CIS 470 Mobile App Development
Presentation transcript:

Basic 2D Graphics in Android

Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic Many of them require a lot of knowledge of the underlying graphics libraries We will look at the very simplest form of 2D graphics

Drawing on a Canvas Visible elements in an Android UI are called Views Each View has an associated Canvas When the View is shown, its onDraw method is automatically called by Android It uses the Canvas to render the different things it wants to display We can create our own View with our own onDraw method to display basic objects using the Canvas

Canvas and Paint Canvas has methods for drawing Arcs, Bitmaps, Circles, Lines, Ovals, Paths, Rectangles, etc. Also methods to rotate, scale, skew, translate Paint has methods for setting the alpha, color, shade, stroke, etc.

Let's Create a New Project! In Eclipse, go to File → New → Project Then select “Android Project” Name the project “FunWithDrawing” Specify the package as “edu.upenn.cs4hs” Name the Activity class “FunWithDrawingActivity” Next, create your own custom View class

Creating Your Own View Class 1. Create a new Java class that extends View 2. Implement the necessary constructors 3. Implement the onDraw method and use the Canvas parameter to draw using a Paint object 4. Add your View to the application's Layout

1 package edu.upenn.cs4hs; 2 3 public class DrawableView extends View { 4 5 // Second, you must implement these constructors!! 6 public DrawableView(Context c) { 7 super(c); 8 } 9 public DrawableView(Context c, AttributeSet a) { 10 super(c, a); 11 } continued on next slide... Create a DrawableView class

Still in the DrawableView class // Third, implement the onDraw method. 14 // This method is called when the View is displayed 15 protected void onDraw(Canvas canvas) { // this is the “paintbrush” 18 Paint paint = new Paint(); 19 // set the color 20 paint.setColor(Color.RED); // draw Rectangle with corners at (40, 20) and (90, 80) 23 canvas.drawRect(40, 20, 90, 80, paint); // change the color 26 paint.setColor(Color.BLUE); 27 // set a shadow 28 paint.setShadowLayer(10, 10, 10, Color.GREEN); // create a “bounding rectangle” 31 RectF rect = new RectF(150, 150, 280, 280); 32 // draw an oval in the bounding rectangle 33 canvas.drawOval(rect, paint); 34 } } // end of DrawableView class

1 2 3 <edu.upenn.cs4hs.DrawableView 4 xmlns:android=" 5 android:layout_width="fill_parent" 6 android:layout_height="wrap_content" 7 /> 8 Modify main.xml as follows

Detecting User Interaction and Touch Events

Detecting Touch Events When the user touches/clicks on the View, Android invokes the View's onTouchEvent method A MotionEvent object is automatically generated and is passed to the method From the MotionEvent, you can determine: – the type of Action (down, up/release, move) – where the event occurred (x/y coordinate) – the time at which the event occurred

Modifying the DrawableView 1. In your DrawableView class, modify onDraw so that the color of the rectangle is randomized 2. Then add an onTouchEvent method that looks for an “up” action and calls this.invalidate if the touch is within the bounds of the rectangle

1 package edu.upenn.cs4hs; 2 3 public class DrawableView extends View { 4 5 // these constructors shouldn't change 6 public DrawableView(Context c) { 7 super(c); 8 } 9 public DrawableView(Context c, AttributeSet a) { 10 super(c, a); 11 } 12 DrawableView class

13 // This version of onDraw randomly chooses a color 14 // to use when drawing the rectangle 15 protected void onDraw(Canvas canvas) { // this is the “paintbrush” 18 Paint paint = new Paint(); // set the color randomly 21 int whichColor = (int)(Math.random() * 4); 22 if (whichColor == 0) paint.setColor(Color.RED); 23 else if (whichColor == 1) paint.setColor(Color.GREEN); 24 else if (whichColor == 2) paint.setColor(Color.BLUE); 25 else paint.setColor(Color.YELLOW); // draw Rectangle with corners at (40, 20) and (90, 80) 28 canvas.drawRect(40, 20, 90, 80, paint); } Modify onDraw as follows

32 // this method is called when the user touches the View 33 public boolean onTouchEvent(MotionEvent event) { // if it's an up (“release”) action 36 if (event.getAction() == MotionEvent.ACTION_UP) { // get the coordinates 39 float x = event.getX(); 40 float y = event.getY(); // see if they clicked on the box 43 if (x >= 40 && x = 20 && y <= 80) { // redraw the View... this calls onDraw again! 46 invalidate(); 47 } 48 } 49 // indicates that the event was handled 50 return true; 51 } // end of onTouchEvent 52 } // end of DrawableView class Add an onTouchEvent method

Helpful Hint: Debugging Log.v(tag, message); System.out.println(message); – Window → Show View → Other... → Android → LogCat To show a pop-up window: Toast.makeText( getApplicationContext(), message, Toast.LENGTH_LONG).show();

Coming up after lunch Creating and handling forms Threads System services Discussion: how can you use Android in your high school CS course?