CS371m - Mobile Computing. 2D Graphics A Crash Course in Using (Android) 2D Graphics Libraries

Similar documents
Graphics Support in Android

Programming of Mobile Services, Spring 2012

+ Inheritance. Sometimes we need to create new more specialized types that are similar to types we have already created.

Mobile Application (Design and) Development

CSE 331 Software Design & Implementation

INTRODUCTION TO ANDROID

Syllabus- Java + Android. Java Fundamentals

CS 160: Lecture 10. Professor John Canny Spring 2004 Feb 25 2/25/2004 1

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL II)

Output models Drawing Rasterization Color models

Custom Views in Android Tutorial

package import import import import import import import public class extends public void super new this class extends public super public void new

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Heavyweight with platform-specific widgets. AWT applications were limited to commonfunctionality that existed on all platforms.

Introduction to Mobile Application Development Using Android Week Three Video Lectures

MARS AREA SCHOOL DISTRICT Curriculum TECHNOLOGY EDUCATION

SEVEN ADVANCED ACADEMY

Programming Concepts and Skills. Creating an Android Project

Fig. 2.2 New Android Application dialog. 2.3 Creating an App 41

ANDROID APPS DEVELOPMENT FOR MOBILE AND TABLET DEVICE (LEVEL I)

CS260 Intro to Java & Android 09.AndroidAdvUI (Part I)

ANDROID (4) 2D Graphics and Animation, Handling Screen Rotation. Marek Piasecki

Merits of QT for developing Imaging Applications UI

Smoother Graphics Taking Control of Painting the Screen

04. Learn the basic widget. DKU-MUST Mobile ICT Education Center

Graphical User Interfaces (GUIs)

IT101. Graphical User Interface

Introduction to Mobile Application Development Using Android Week Four Video Lectures

ANDROID APPS (NOW WITH JELLY BEANS!) Jordan Jozwiak November 11, 2012

Building User Interface for Android Mobile Applications II

Event Driven UIs and Model-View-Controller

CS 160: Interactive Programming

Core Graphics and OpenGL ES. Dr. Sarah Abraham

Android About.me/DavidCorrado Mobile Meetup Organizer

CS 370 Android Basics D R. M I C H A E L J. R E A L E F A L L

ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS

Bouncing and Actor Class Directions Part 2: Drawing Graphics, Adding Touch Event Data, and Adding Accelerometer

ORACLE UNIVERSITY AUTHORISED EDUCATION PARTNER (WDP)

Eventually, you'll be returned to the AVD Manager. From there, you'll see your new device.

CS371m - Mobile Computing. Responsiveness

Avoid Common Pitfalls when Programming 2D Graphics in Java: Lessons Learnt from Implementing the Minueto Toolkit

Introduction To Inkscape Creating Custom Graphics For Websites, Displays & Lessons

Java Programming. Computer Science 112

Introduction p. 1 Java Features p. 2 Java Expansion p. 4 Getting, Setting Up, and Using Java p. 5 The Java Language p. 5 Java Swing Components p.

Java Programming Lecture 6

CORE JAVA& ANDROID SYLLABUS

COMP3421 Computer Graphics

CS 528 Mobile and Ubiquitous Computing Lecture 1b: Introduction to Android. Emmanuel Agu

LTBP INDUSTRIAL TRAINING INSTITUTE

Android App Development

Art 486: Introduction to Interactive Media.

Android for Ubiquitous Computing Researchers. Andrew Rice University of Cambridge 17-Sep-2011

Java for Programmers Course (equivalent to SL 275) 36 Contact Hours

Graphics -- To be discussed

Chapter 2 Welcome App

Let s Make a Front Panel using FrontCAD

Agenda. Overview of Xamarin and Xamarin.Android Xamarin.Android fundamentals Creating a detail screen

Lab 1 - Setting up the User s Profile UI

Mobile App Design Project Doodle App. Description:

Produced by. Mobile Application Development. David Drohan Department of Computing & Mathematics Waterford Institute of Technology

MOBILE COMPUTING 1/20/18. How many of you. CSE 40814/60814 Spring have implemented a command-line user interface?

CMSC434. Introduction to Human-Computer Interaction. Week 10 Lecture 17 Mar 31, 2016 Engineering Interfaces III. Jon

CS 11 java track: lecture 3

Android Development Crash Course

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Practical Problem: Create an Android app that having following layouts.

MS Word 2007: Graphics. Lesson Notes Author: Pamela Schmidt. The Drawing Tools Format Ribbon appears when the object is selected.

Software Development & Education Center. Java Platform, Micro Edition. (Mobile Java)

Game Programming with. presented by Nathan Baur

GUI Design for Android Applications

Android Application Development 101. Jason Chen Google I/O 2008

CS260 Intro to Java & Android 05.Android UI(Part I)

This is the vector graphics "drawing" technology with its technical and creative beauty. SVG Inkscape vectors

In this Class Mark shows you how to put applications into packages and how to run them through the command line.

CS 201 Advanced Object-Oriented Programming Lab 3, Asteroids Part 1 Due: February 17/18, 11:30 PM

Honors Computer Science Python Mr. Clausen Program 7A, 7B

EECS 4443 Mobile User Interfaces. More About Layouts. Scott MacKenzie. York University. Overview (Review)

Create new Android project in Android Studio Add Button and TextView to layout Learn how to use buttons to call methods. Modify strings.

Graphical User Interface (GUI)

Object-oriented programming in Java (2)

8/30/15 MOBILE COMPUTING. CSE 40814/60814 Fall How many of you. have implemented a command-line user interface?

Acknowledgments...xvii. Introduction... Chapter 1: Getting Started Chapter 2: Build a Hi-Lo Guessing Game App!... 19

CS260 Intro to Java & Android 05.Android UI(Part I)

animation, and what interface elements the Flash editor contains to help you create and control your animation.

(Refer Slide Time: 1:12)

CS378 -Mobile Computing. User Interface Basics

Mobile Application Development - Android

ST 软件 软件平台 2. TouchGFX

SHWETANK KUMAR GUPTA Only For Education Purpose

Block I Unit 2. Basic Constructs in Java. AOU Beirut Computer Science M301 Block I, unit 2 1

The Macromedia Flash Workspace

CS371m - Mobile Computing. More UI Navigation, Fragments, and App / Action Bars

Android Application Development Instructions

Mobile Computing Professor Pushpedra Singh Indraprasth Institute of Information Technology Delhi Andriod Development Lecture 09

Output in Window Systems and Toolkits

Week 5: Images & Graphics. Programming of Interactive Systems. JavaFX Images. images. Anastasia Bezerianos. Anastasia Bezerianos

Rasterization and Graphics Hardware. Not just about fancy 3D! Rendering/Rasterization. The simplest case: Points. When do we care?

Framework. Set of cooperating classes/interfaces. Example: Swing package is framework for problem domain of GUI programming

Software Development & Education Center. Java Platform, Standard Edition 7 (JSE 7)

The Internet. CS 2046 Mobile Application Development Fall Jeff Davidson CS 2046

Transcription:

CS371m - Mobile Computing 2D Graphics A Crash Course in Using (Android) 2D Graphics Libraries

Using Graphics Not an end in itself Create a richer, easier to use User Interface Display information in a easier to understand way Entertainment In Android picking the theme affects the appearance of your GUI widgets 2

Richer UI The (OLD) Apple Effect? 3

UI Design Changes The (New) Apple Effect 4

ios 6 and ios 7 5

Clicker Which do you like more: A. The graphics with 3d like effects B. The flat graphics C. I don t have a preference 6

Clicker How do you like your IDE s set up? A. Black text on white background B. White text on black background C. Something else D. I don t have a preference 7

Android Theme Multiple Themes in Android s History Current Standard is the Material Theme Set in Manifest Able to alter aspects of theme Creates a consistent look for app Emphasis on Feedback via animation 8

SIDETRACK ON THE VISUAL DISPLAY OF INFORMATION 9

Visual Display of Information Edward Tufte Yale professor (poly sci, stats, and cs) spark lines small multiple critical of PowerPoint 10

Visual Display Spark Lines Small Multiple 11

EXAMPLES OF THE VISUAL DISPLAY OF INFORMATION 12

Joseph Minard Napoleon's Invasion of Russia Location, size of army, data, temperature 13

John Snow - Deaths from Cholera, 1854 Broad Street Outbreak (The Ghost Map) 14

Space Debris Plot 15

Visualizing Data Ben Fry, one of the creators of Processing 16

All Roads 17

Files on Disk - WinDirStats 18

S&P 500 Heat Map 19

CS324E Heat Map 20

CS324E Wator World 21

Image Processing 22

Color Histogram 23

Histogram Equalization 24

Revised Image 25

26

USING ANDROID NATIVE 2D GRAPHICS 27

Android Graphics NOT the Java awt or swing packages custom set of classes Canvas: class that holds code for various "draw" methods Similar to Java Graphics, Graphics2D objects Paint: Controls the drawing. A whole host of properties. Bitmap: the things drawn on Drawable: the thing to draw. (rectangles, images, lines, etc.) Typeface: for fonts 28

Using a Canvas Simple way -> Create a custom View and override the ondraw method The Canvas is a parameter to ondraw Create a class that extends View override the 2 parameter constructor override the ondraw method perform custom drawing in the ondraw method add the View to the proper layout 29

Graphics Resources Use Drawables in Views Create a folder res/drawable Add images png (preferred) jpg (acceptable) gif (discouraged) Images can be added as background for Views 30

Requesting Redraws of a View call invalidate() on a View to redraw it invalidate redraws the whole View possible to redraw only a portion of the View, the part that changed several overloaded versions of invalidate that accept a region of the View only that portion redrawn Override the ondraw method for the View to redraw key we override ondraw but don't call it! call invalidate! for really complex drawing and graphics move drawing off of the UI thread to a SurfaceView (more complex) 31

GuessFour Board drawn in ondraw method of a View Board will resize for different devices lines, ovals, rectangles, and texts 32

Paint typically create Paint with anti aliasing enable Paint p = new Paint(Paint.ANTI_ALIAS_FLAG); Anti Aliasing on 33

34 Anti Aliasing - The Jaggies Anti Aliasing off Anti Aliasing off Anti Aliasing on

Using the Canvas Class methods to draw: rectangles lines arcs paths images circles ovals points text and many more Ability to set the "clip", portion of canvas where drawing takes place commands to draw something that is outside clip are ignored Ability to translate, rotate, and scale the canvas 35

Paint Object many, many attributes and properties including: current color to draw with Color specified as argb whether to fill or outline shapes size of stroke when drawing text attributes including size, style (e.g. underline, bold), alignment, gradients 36

Gradients 3 kinds of gradients LinearGradeint RadialGradeint SweepGradient at least 2 color, but possibly more flows from one color to another 37

Linear Gradient 38

LinearGradient 39

RadialGradient 40

RadialGradient 41

add depth to pegs and open spots in Guess Four game RadialGradeint 42

SweepGradient 43

SweepGradient 44

SweepGradient 45

SweepGradient 46

SweepGradient 47

SIMPLE ANDROID ANIMATION LOOP 48

Simple Animation Animation altering some property of a 2D primitive position size color alpha Simplest animation loop, after ondraw, call invalidate at the mercy of the UI frame rate 49

Simple (And Poor) Animation Approach draw as fast as possible emulator frame rate (on my machine) 12 fps dev device frame rate, high 50s fps 50

Better Animation Loop Create a Handler to delay / sleep update method will call sleep method on the Handler 51

update method animation loop update tells handler to sleep handler calls update when it wakes up 52

Simple Animation Example Add Custom View to XML in main.xml add custom View as element in LinearLayout Class Name RGB Color, using hexadecimal 53

Simple Example - BalloonView 54

BalloonView Constructors 55

BalloonView Tracks "balloons" on screen Balloon class to track state of each balloon Add balloons randomly Update balloons when instructed change position remove if off screen 56

BalloonView ondraw Method 57

Balloon Class and draw Method 58

BalloonView update Method 59

Animation Loop Activity has AnimationLoop object non standard Android class to simplify animation loops AnimationLoop given target frames per second and View to update 60

Animation Loop When Activity resumes, animation loop started 61

Pausing Animation Loop object paused when Activity paused toggle animation when activity is clicked create on click listener for activity 62

Animation Loop Animation Loop has a Runnable and a Handler Runnable: Any class whose instances are intended to be executed by a thread standard Java class Handler: Allows you to send and process Message and Runnable objects associated with a thread's Message queue. schedule messages and runnables to be executed as some point in the future Android specific class (there is a class named Handler in the Java standard library) 63

AnimationLoop AnimationLoop start method create new Runnable and start (run method called) Runnable run method while (true) sleep for appropriate time on wakeup, tell View to update itself 64

TWEENED ANIMATIONS 65

Simple Animations Tweened Animations also know as View Animations provide a way to perform simple animations on Views, Bitmaps, TextViews, Drawables provide start point, end point, size, rotation, transparency, other properties Can set up tweened animation in XML or programmatically 66

Tweened Animations Used to alter one of four properties of a single View affect Alpha (transparency / opaqueness) Rotation Scale (size) Location (movement, Translate) 67

Tweened Animations define interpolator in XML (optional) allow animation to be repeated, accelerate, decelerate, "bounce", and more can use built in interpolators or create your own define animation in XML alpha, rotate, scale, translate define from value and to value and duration In program load and start animation in response to some event Guess Four, invalid choice, solved puzzle 68

Guess Four res/anim shake up down shake left right 69

cycle_7.xml GuessFour res/anim spin.xml 70

GuessFour Example On error board shakes back and forth On win board spins From BoardView in GuessFour 71

More Tweened Examples hyperspace example from android dev site rotate and change alpha animation types: alpha scale translate rotate http://developer.android.com/guide/topics/resources/animation-resource.html 72

Hyperspace Part 1 73

Hyperspace Part 2 74

More Tweened Examples Moving Button Note, tweened animations draw the button in a different spot But, the button's location does not actually change 75

Up and Down Animation 76

Moving Button Activity Use extra from Intent to determine what type of animation to perform. 77

Tweened Animation 78

Change Background Color Called when button clicked onclick attribute Result? 79

PROPERTY ANIMATIONS -AVAILABLE POST GINGERBREAD, ANDOID 3.0, API LEVEL 11 80

Developer of new animation framework for Android, Chet Haase and Romain Guy 81

Property Animations A more general animation framework Tweened Animations can only affect alpha, scale, rotation, and position Property Animations can affect any property of an object typically a View or Drawable can be defined in sets, change multiple properties at a time animation created separate from target object, reuse with different objects http://developer.android.com/guide/topics/graphics/prop-animation.html 82

Property Animation - Classes ValueAnimator base class for property animations Object Animator convenience class for animating specific object and property ViewPropertyAnimator optimized, simple animation of View objects evaluator classes such as ArgbEvaluator to animate property by defining how it changes over time 83

Some Animations Simple API levels 11+ View objects have animate() method ViewPropertyAnimator methods for alpha, rotation, scale (size), translation (position) 84

ViewPropertyAnimator Example onclick method for a button: button will disappear and then reappear next time clicked 85

More Complex Property Animation Object animation example from moving button example animated class must have a "set<property>" method 86

Button Class 87

ObjectAnimator 88

HOW?????? How can the ObjectAnimator call the right methods on the object passed? A. reflection B. open graphics library C. static D. xml E. generics Declared type is Object must be calling sety method, right? 89

A Sidetrack on Reflection

Reflection Advanced feature of Java. Commonly used by programs that "examine or modify the runtime behavior of applications running in the Java virtual machine" The Android Property Animation framework uses reflection 91

Extensible features Why Reflection like Android Property Animator framework Class Browsers and Visual Development Environments Debugger and Testing Tools am I testing all the public methods? coverage 92

Recall: Property Animation ObjectAnimator class a subclass of ValueAnimator Convenience class for property animation When animator created set animation time, property to animate, and the starting and ending values "The constructors of this class take parameters to define the target object that will be animated as well as the name of the property that will be animated. Appropriate set/get functions are then determined internally and the animation will call these functions as necessary to animate the property. " 93

ObjectAnimator Example Button class must have gety and sety methods that return and accept a float offloat, ofint, ofobject, ofmulti 94

Object Animator How does the Object animator affect the y value of the Button? Not a button Recall Java, declared type, actual type What methods does allow compiler allow? 95

Class objects Everything in Java is a primitive (byte, short, int, long, float, double, char, boolean) or an Object arrays and Enums are Objects The Java virtual machine instantiates an immutable instance of java.lang.class for every type of Object necessary in a running program Entry point for reflection 96

Getting the Class object 97

Accessing Internals Class object may be used to access fields, methods, and constructors including private members methods that enumerate members (instance variables) and methods that search for a member given a name Like a spy 98

Security This appears to be dangerous stuff The ability to find out about private methods and fields and even change them Thus many of the methods in the Class class and Reflection API throw SecurityExceptions If a SecurityManager is present and permission for reflection is not granted, exceptions occur "If you remove this sticker, the warranty is void" 99

Enumerating Fields 100

Enumerating Methods 101

Calling Methods 102

Calling Methods 103

Results of Method Calls - Animate! 104

Clicker Question Can we alter the value stored in an object via reflection if there is not set method? A. No B. Yes C. Maybe 105

And Then the Unthinkable Happened 106

x is private no methods to access or alter x YIKES Recall Button Class 107

Clicker Question Can we alter the value stored in an object via reflection if there is not set method and the value is declared to be final? A. No B. Yes C. Maybe 108

But final is safe, right???? String fields in Java 8 109

Result of changestring We are good right? 110

Oh the Humanity 111

Client Code 112

Result 113

RENDERING WITH COMPLEX CALCULATIONS 114

More Complex Graphics Don't want apps to become unresponsive If complex graphics or animation use SurfaceView class Main view not waiting on ondraw to finish secondary thread with reference to SurfaceView SrufaceView draws and when done display result 115

Using a SurfaceView extend SurfaceView implement SurfaceHolder.Callback methods to notify main View when SurfaceView is created, changed or destroyed 116

Simple Example Static Screen continuously draw several hundred small rectangles (points, with stroke = 10) slowly fill screen and then keep changing 117

Implement SurfaceHolder.Callback methods 118

Prevent Runaway Threads! 119

Inner Class for Thread 120

Run Method in StaticThread Standard Approach for Drawing on SurfaceView 121

Demo run() Pronounced flicker and jitter Double buffer under the hood We are drawing on two different Bitmaps Canvas does drawing onto Bitmap 122

Remove Flicker / Jitter If we draw background each "frame" then we don't redraw previous rectangles How about "saving" all the data? points, colors 123

Alternative Recall two approaches: draw on UI thread by overriding ondraw create custom View (tutorial 4) okay if not a lot of drawing must keep UI thread responsive complex drawing or animations using SurfaceView Third approach, possible variation on the above two approaches maintain a separate Bitmap 124

Separate Bitmap StaticThread has a Bitmap instance var Initialize in constructor 125

Updates to Bitmap Create a Canvas to draw on the Bitmap we are saving When done drawing to Bitmap use SurfaceView Canvas to 126 draw

Demo Alt Version of run() Flicker and jitter? Also possible to save Bitmap to file for later use 127

Animations Frame based vs. Time based Frame based: update every frame simple, but difference in frame rates Time based update every frame but based on time elapsed since last frame more work, more accurate sdk example lunar lander 128

Checking Frame Rate From StaticView Emulator 6-7 fps, dev phone 40-45 fps 129

Controlling Frame Rate Sleep after completing work in loop of run More complex than shown, use previous time and current time 130

Two Methods for Displaying 2D Graphics Two approaches draw graphics or animations into a View object that is part of layout define graphics that go into View simple approach for non dynamic graphics or simple "tweened" animations This is what we did in Tic Tac Toe Draw graphics directly to a Canvas the complex way, but with more control 131

ADDING DRAWABLES TO VIEWS 132

Adding Drawables to Views Change background to an image previously used background colors 133

Top Ten With Image Background 134

Add ImageView to Layout In the main.xml for top ten 135

ImageView Attributes scaletype: how image should be moved or resized in the ImageView tint: affects color of image more to position image in ImageView 136

Changing ImageView Programmatically Randomly set the alpha (transparency of the image) Or pick an image randomly Or set image based on month (Season) 137