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

Similar documents
Programming of Mobile Services, Spring 2012

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

App Development for Smart Devices. Lec #18: Advanced Topics

Custom Views in Android Tutorial

TWO-DIMENSIONAL FIGURES

Android Exam AND-401 Android Application Development Version: 7.0 [ Total Questions: 129 ]

Change in Orientation. Marco Ronchetti Università degli Studi di Trento

INTRODUCTION TO ANDROID

Recipes4Success. Animate Plant Growth. Share 4 - Animation

User Interface Development. CSE 5236: Mobile Application Development Instructor: Adam C. Champion Course Coordinator: Dr.

App Development for Smart Devices. Lec #9: Advanced Topics

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

CMPSCI 119 LAB #2 Anime Eyes Professor William T. Verts

GATAV 5. Animations Using a Game Loop

Mobile Development Lecture 8: Intents and Animation

LifeStreet Media Android Publisher SDK Integration Guide

This module sets out essential concepts and skills relating to demonstrating competence in using presentation software.

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

Better UI Makes ugui Better!

Orientation & Localization

CS 2110 Fall Instructions. 1 Installing the code. Homework 4 Paint Program. 0.1 Grading, Partners, Academic Integrity, Help

Better UI Makes ugui Better!

CMPSCI 119 LAB #2 Greebles / Anime Eyes Professor William T. Verts

The following is the Syllabus for Module 6, Presentation, which is the basis for the practice-based test in this module.

Introduction to Android Development

CISC 1600, Lab 2.1: Processing

CS 528 Mobile and Ubiquitous Computing Lecture 3b: Android Activity Lifecycle and Intents Emmanuel Agu

Android App Development. Mr. Michaud ICE Programs Georgia Institute of Technology

CISC 1600 Lecture 3.1 Introduction to Processing

Better UI Makes ugui Better!

States of Activities. Active Pause Stop Inactive

UI, Continued. CS 2046 Mobile Application Development Fall Jeff Davidson CS 2046

Programming with Android: Animations, Menu, Toast and Dialogs. Luca Bedogni. Dipartimento di Informatica: Scienza e Ingegneria Università di Bologna

Graphics Support in Android

Understand applications and their components. activity service broadcast receiver content provider intent AndroidManifest.xml

JavaFX Technology Building GUI Applications With JavaFX - Tutorial Overview

Graphics -- To be discussed

Mobile Application (Design and) Development

Computer Games 2012 Game Development

Android Application Development. By : Shibaji Debnath

Paint Tutorial (Project #14a)

CISC 1600, Lab 3.1: Processing

Mobile App Design Project Doodle App. Description:

Mobile Programming Lecture 5. Composite Views, Activities, Intents and Filters

mith College Computer Science CSC103 How Computers Work Week 7 Fall 2017 Dominique Thiébaut

A Crash Course to Android Mobile Platform

Programming Project 1

UI Elements. If you are not working in 2D mode, you need to change the texture type to Sprite (2D and UI)

SIMPLE APPLET PROGRAM

Chapter 7 Exercise Solutions

CS 4330/5390: Mobile Application Development Exam 1

mith College Computer Science CSC103 How Computers Work Week 6 Fall 2017 Dominique Thiébaut

Android 101: How to draw on a SurfaceView and handle touch events. Richard A. Perez Android 101

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

Getting Started With Android Feature Flags

public void paintcomponent(graphics g) { Graphics2D g2 = (Graphics2D)g;... }

Corel Draw 11. What is Vector Graphics?

Fragments were added to the Android API in Honeycomb, API 11. The primary classes related to fragments are: android.app.fragment

Graphical User Interfaces

To specify the dimensions of the drawing canvas use the size statement: ! size( 300, 400 );

LECTURE NOTES OF APPLICATION ACTIVITIES

(SSOL) Simple Shape Oriented Language

Practice Midterm 1. Problem Points Score TOTAL 50

1. Multimedia authoring is the process of creating a multimedia production:

Chart And Graph. Features. Features. Quick Start Folders of interest Bar Chart Pie Chart Graph Chart Legend

9. APPLETS AND APPLICATIONS

Customisation and production of Badges. Getting started with I-Color System Basic Light

Solving an Android Threading Problem

Graphical User Interfaces

Designer Reference 1

Embedded Systems Programming - PA8001

CS378 -Mobile Computing. User Interface Basics

ACS-1805 Introduction to Programming (with App Inventor)

Building MyFirstApp Android Application Step by Step. Sang Shin Learn with Passion!

Adobe Flash CS4 Part 1: Introduction to Flash

Android Development Tutorial

Introduction to Mobile Application Development Using Android Week Three Video Lectures

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

The American University in Cairo. Academic Computing Services. Word prepared by. Soumaia Ahmed Al Ayyat

Graphics Applets. By Mr. Dave Clausen

C omputer D riving L icence

Mobile Systeme Grundlagen und Anwendungen standortbezogener Dienste. Location Based Services in the Context of Web 2.0

CS 315 Software Design Homework 1 First Sip of Java Due: Sept. 10, 11:30 PM

A Step-by-step guide to creating a Professional PowerPoint Presentation

Lecture 7 A First Graphic Program And Data Structures & Drawing

Graphical User Interfaces

Output models Drawing Rasterization Color models

Animating the Page IN THIS CHAPTER. Timelines and Frames

Software System Components 1 Graphics

PSD to Mobile UI Tutorial

Assignment 2. Application Development

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

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

M.C.A. Semester V Subject: - Mobile Computing (650003) Week : 1

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

Procedural decomposition examples

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

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Managing Screen Orientation

Android Apps Development for Mobile Game Lesson 5

CS 4518 Mobile and Ubiquitous Computing Lecture 5: Rotating Device, Saving Data, Intents and Fragments Emmanuel Agu

Transcription:

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

Outline 2D graphics drawing Color / Paint / Canvas XML drawable (from resources) direct to a Canvas / View.onDraw() 2D animation frame by frame XML / View animation Handling screen rotation blocking rotation automatic handling (separate vertical/landscape layouts) preserving temporary data

android.graphics.color (1) colors are represented with four numbers (ARGB): alpha, red, green, blue Each component can have 256 possible values, or 8 bits, typically packed into a 32-bit integer (for efficiency) Alpha is a measure of transparency: value "0" -> color is completely transparent value "255" -> color is completely opaque in the middle -> semitransparent colors color definition: use one of the static constants on the Color class int color = Color.BLUE; // solid blue

android.graphics.color (2) static factory methods of the Color int color1 = Color.argb(127, 255, 0, 255); // Translucent purple int color2 = Color.rgb(255, 0, 0); //opaque (alpha=255) red better off defining all your colors in an XML resource <?xml version="1.0" encoding="utf-8"?> <resources> <color name="mycolor">#7fff00ff</color> </resources> then can reference colors by defined name in other XML files or in Java code int color = getresources().getcolor(r.color.mycolor);

android.graphics.paint / Canvas Paint class: holds the style and color to draw a solid color: paint.setcolor(color.ltgray); Canvas class represents a surface on which you draw Initially clear, like blank transparencies for an overhead projector Canvas methods let draw lines, rectangles, circles, or other arbitrary graphics In Android: Display screen Activity View View.onDraw( Canvas ) Canvas

XML Drawables (1) Draw into a View object from your layout: drawing is handled by the system (normal View hierarchy drawing process) simply define the graphics in XML

XML Drawables (View s Layout definition) (2)

XML Drawables (3) DEMO from Android in Action example source chapter 9 / XMLDraw

Direct drawing on Canvas (1)

android.graphics.canvas (3) Canvas methods drawing geometric primitives: public void drawcolor (int color) public void drawpaint (Paint paint) public void drawarc (RectF oval, float startangle, float sweepangle, boolean usecenter, Paint paint) public void drawcircle (float cx, float cy, float radius, Paint paint) public void drawline (float startx, float starty, float stopx, float stopy, Paint paint) public void drawrect (Rect r, Paint paint) public void drawtext (String text, float x, float y, Paint paint)

Canvas drawing example (1)

Canvas drawing example (2)

Frame by frame animation (1) simple animations by showing a set of images one after another define a set of resources in a XML file and call AnimationDrawable start(). create a new directory called /anim under the /res resources directory. Place all the images for this example in the /drawable directory. DEMO: [XMLAnimate] (chapter 9)

Frame by frame animation (2) create a new directory called /anim under the /res resources directory. Place all the images for this example in the /drawable directory. create an XML file Simple_animation.xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false"> <item android:drawable="@drawable/ball1" android:duration="50" /> <item android:drawable="@drawable/ball2" android:duration="50" /> <item android:drawable="@drawable/ball3" android:duration="50" /> <item android:drawable="@drawable/ball4" android:duration="50" /> <item android:drawable="@drawable/ball5" android:duration="50" /> <item android:drawable="@drawable/ball6" android:duration="50" /> </animation-list> two attributes: drawable, which describes the path to the image, duration - the length of time to show the image, in miliseconds

Frame by frame animation (3) we can t control the animation from within the OnCreate method!

Frame by frame animation (4) DEMO from Android in Action example source chapter 9 / XMLAnimate

XML View animation (1) define animations that can rotate, fade, move, or stretch graphics or text Animation XML files are placed in the res/anim source directory. Android supports four types of animations: <alpha> Defines fading, from 0.0 to 1.0 (0.0 being transparent) <scale> Defines sizing, x and y (1.0 being no change) <translate> Defines motion, x and y (percentage or absolute) <rotate> Defines rotation, pivot from x and y (degrees) attributes that can be used with any animation type: duration Duration, in milliseconds startoffset Offset start time, in milliseconds interpolator Used to define a velocity curve for speed of animation

XML View animation (2) example parameters for "scale" (scaler.xml resource) android:fromxscale = "0.5" android:toxscale = "2.0" android:fromyscale = "0.5" android:toyscale = "2.0" android:pivotx = "50%" android:pivoty = "50%" android:startoffset = "700" android:duration = "400" android:fillbefore = "false" can use this animation with any View object: view.startanimation(animationutils.loadanimation(this, R.anim.scaler));

Getting screen sizes A common mistake to use the width and height of a view inside its constructor! When a view s constructor is called, Android doesn t know yet how big the view will be, so the sizes are set to zero. The real sizes are calculated during the layout stage (which occurs after construction but before anything is drawn) Use the onsizechanged( )method to be notified of the values when they are known Use the getwidth( ) and getheight( )methods in the ondraw( ) method @Override protected void onsizechanged(int w, int h, int oldw, int oldh) { width = w / 9f; height = h / 9f; getrect(selx, sely, selrect); Log.d(TAG, "onsizechanged: width " + width + ", height " + height); super.onsizechanged(w, h, oldw, oldh); }

Handling screen rotation (1) To block Android from rotating your activity, add android:screenorientation = "portrait" (or "landscape") to AndroidManifest.xml file: <activity android:name=".rotationdemo" android:screenorientation="portrait" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> add android:screenorientation = "sensor" to rotate screen based on the position of the phone from accelerometer (this setting disables having the keyboard trigger a rotation event)

Handling screen rotation (2) Customize/provide different layouts for various display orientations by creating specific folders for each configuration: res/layout - default layout folder, res/layout-port - support the portrait orientation, res/layout-land - for landscape orientation, res/layoutsquare - for square displays Example layout definition downloaded for horizontal orientation res/layout-land/main.xml

Handling screen rotation (3) can set the orientation of the device in code: setrequestedorientation(activityinfo.screen_orientation_landscape); To handle rotations on your own: put an android:configchanges entry in your AndroidManifest.xml listing the configuration changes you want to handle yourself android:configchanges="orientation keyboardhidden implement onconfigurationchanged() which will be called when configuration changes you listed in occurs:

Handling screen rotation (4) Preserving temporary data durring configuration changes: (by default Android destroys and re-creates your activity from scratch) 1. blocking configuration changes ( android:screenorientation= ) 2. handle rotations on your own ( android:configchanges= ) by implementing onconfigurationchanged(configuration); 3. preserve/store the data in the Bundle by implementing onsaveinstancestate(bundle); oncreate(bundle); or onrestoreinstancestate(bundle); data have to be serialisable! 4. preserve/store data in any Object by implementing void onretainnonconfigurationinstance(object) Object getlastnonconfigurationinstance( ); data could be any Java object.