Programming of Mobile Services, Spring 2012

Similar documents
Mobile Application (Design and) Development

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

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

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

INTRODUCTION TO ANDROID

Android Development Tutorial

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

Graphics Support in Android

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

API Guide for Gesture Recognition Engine. Version 1.3

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

Sensors. Marco Ronchetti Università degli Studi di Trento

MotionEvents Touch Handling Gestures

Android Apps Development for Mobile Game Lesson Create a simple paint brush that allows user to draw something (Page 11 13)

OPTIMIZING ANDROID UI PRO TIPS FOR CREATING SMOOTH AND RESPONSIVE APPS

Basic UI elements: Android Buttons (Basics) Marco Ronchetti Università degli Studi di Trento

Custom Views in Android Tutorial

ACTIVITY, FRAGMENT, NAVIGATION. Roberto Beraldi

Android. Soft Keyboard Graphics and Media

LẬP TRÌNH DI ĐỘNG. Bài 13: Sensors + Animations

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

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

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

Diving into Android. By Jeroen Tietema. Jeroen Tietema,

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

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

GATAV 5. Animations Using a Game Loop

Announcements. Android: n-puzzle Walkthrough. Tommy MacWilliam. Dynamic GUIs. ListViews. Bitmaps. Gameplay. Saving State. Menus

A Crash Course to Android Mobile Platform

EMBEDDED SYSTEMS PROGRAMMING Application Tip: Saving State

Theme 2 Program Design. MVC and MVP

Android HelloWorld - Example. Tushar B. Kute,

Android. Soft Keyboard Graphics and Media. Soft keyboards. Action events. Tailoring the soft keyboard

Learning Android Canvas

GATAV 9. Goodby flatland Welcome to 3D!

Embedded Systems Programming - PA8001

User Interface: Layout. Asst. Prof. Dr. Kanda Runapongsa Saikaew Computer Engineering Khon Kaen University

Programming Mobile Applications with Android Lab2

Mobile Development Lecture 8: Intents and Animation

Solving an Android Threading Problem

Vienos veiklos būsena. Theory

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

Embedded Systems Programming - PA8001

Introduction to Mobile Application Development Using Android Week Three Video Lectures

Android Application Model I

Have a development environment in 256 or 255 Be familiar with the application lifecycle

Arrays of Buttons. Inside Android

Mastering Android Drawables

Introduction to Android Development

Real-Time Embedded Systems

Mobile Image Processing

App Development for Smart Devices. Lec #7: Audio, Video & Telephony Try It Out

Graphics. Android - graphics 1/9

Stanislav Rost CSAIL, MIT

1 카메라 1.1 제어절차 1.2 관련주요메서드 1.3 제작철차 서피스뷰를생성하고이를제어하는서피스홀더객체를참조해야함. 매니페스트에퍼미션을지정해야한다.

Android and OpenGL. Android Smartphone Programming. Matthias Keil. University of Freiburg

Mobile Image Processing

Spring Lecture 7 Lecturer: Omid Jafarinezhad

Mobile App Design Project Doodle App. Description:

Android Application Model I. CSE 5236: Mobile Application Development Instructor: Adam C. Champion, Ph.D. Course Coordinator: Dr.

EMBEDDED SYSTEMS PROGRAMMING UI and Android

CISC 1600, Lab 3.1: Processing

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

Contents. Canvas and Graphical shapes Drawing Font and Text Bitmaps and Buffer Example Animation 3D-Graphics Hardware Support

CISC 1600, Lab 2.1: Processing

Fragments. Lecture 11

TextView. A label is called a TextView. TextViews are typically used to display a caption TextViews are not editable, therefore they take no input

Mobile Development Lecture 10: Fragments

public AnimLayer(Context context, AttributeSet attrs, int defstyle) { super(context, attrs, defstyle); initlayer(); }

Contents. Orientation SearchBar and Gallery List Advanced Multiple Form Multi Touch Animations Layout Designing UI using UI Builder

ANDROID SERVICES, BROADCAST RECEIVER, APPLICATION RESOURCES AND PROCESS

CS 403X Mobile and Ubiquitous Computing Lecture 3: Introduction to Android Programming Emmanuel Agu

Android development. Outline. Android Studio. Setting up Android Studio. 1. Set up Android Studio. Tiberiu Vilcu. 2.

Practical 1.ListView example

COMP4521 EMBEDDED SYSTEMS SOFTWARE

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

Android Specifics. Jonathan Diehl (Informatik 10) Hendrik Thüs (Informatik 9)

LifeStreet Media Android Publisher SDK Integration Guide

Activities. Repo:

Android Basics. Android UI Architecture. Android UI 1

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

Fragments. Lecture 10

Android Apps Development for Mobile Game Lesson 5

ELET4133: Embedded Systems. Topic 15 Sensors

Mobila applikationer och trådlösa nät, HI1033, HT2012

Intents. Your first app assignment

Output models Drawing Rasterization Color models

Mobile Application Development - Android

Software Practice 3 Before we start Today s lecture Today s Task Team organization

Programming with Android: Application Resources. Dipartimento di Scienze dell Informazione Università di Bologna

Multiple devices. Use wrap_content and match_parent Use RelativeLayout/ConstraintLayout Use configuration qualifiers

Android & iphone. Amir Eibagi. Localization

Basic GUI elements - exercises

Introduction to Mobile Application Development Using Android Week Four Video Lectures

Android Apps Development for Mobile and Tablet Device (Level I) Lesson 2

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

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

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

BSCS 514- Computer Graphics. Course Supervisor : Dr. Humera Tariq Hands on Lab Sessions: Mr. Faraz Naqvi

Lab 7: Create a picture viewer application

Transcription:

Programming of Mobile Services, Spring 2012 HI1017 Lecturer: Anders Lindström, anders.lindstrom@sth.kth.se Lecture 6 Today s topics Android graphics - Views, Canvas, Drawables, Paint - Double buffering, bitmaps - Animations - Graphics and multi threading, SurfaceView

Android graphics 2D graphics library, package android.graphics, android.view OpenGL ES 1.0, 2D and 3D, hardware acceleration, package javax.microedition.khronos.opengles, android.opengl Issues: - Screens with different sizes and resolutions - Performance

Android graphics

Android 2D graphics Different needs, different ways to draw 1. Static define views, drawings and animations in layout files 2. Update views when needed, e.g. on user input. Draw on a Canvas (View.onDraw), called implicitly via View.invalidate (from main-thread ) 3. In a separate thread, wherein you manage a SurfaceView and perform painting directly to a Canvas

Drawables android.graphics.drawable Something that can be drawn, e.g. an image, shape, transition, animation Create Drawables from resources, xml layout or by calling a constructor Images are stored in res/drawable-ldpi, / - mdpi, / -hdpi Preferred format: png, 9.png (acceptable: jpg)

Drawables Defining view with image in layout xml: <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_image"/> Creating a drawable from resources: Resources res = mcontext.getresources(); Drawable myimage = res.getdrawable(r.drawable.my_image);

Drawables Load image into ImageView in code: protected void oncreate(bundle savedinststate) {... LinearLayout layout = new LinearLayout(this); ImageView iw = new ImageView(this); iw.setimageresource(r.drawable.my_image); iw.setadjustviewbounds(true);... layout.addview(iw); setcontentview(layout);...

Drawables, 9-patch stretchable Strechable png-image (9.png)

(be)tween Animation Package android.view.animation Simple transformations: position, size, rotation, transparency Define the transformations that you want to occur, when they will occur, and how long they should take to apply Transformations can be sequential or simultaneous Define in res/anim/my_animation.xml

Tween Animation res/anim/hyperspace_jump.xml (not complete, see code ex.) <set... android:shareinterpolator="false" > <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:duration="700" android:fromxscale="1.0" android:fromyscale="1.0" android:toxscale="1.4 android:toyscale="0.6" /> <set android:interpolator="@android:anim/decelerate_interpolator" > <scale android:duration="2000 android:fromxscale="1.4 android:fromyscale="0.6".../> <rotate android:duration="2000" android:fromdegrees="0 android:todegrees="360" android:pivotx="50%" android:pivoty="50%" android:startoffset="700 /> </set> </set>

Corresponding code Tween Animation ImageView spaceshipimage = (ImageView) findviewbyid(r.id.spaceshipimage); Animation hyperspacejumpanimation = AnimationUtils.loadAnimation( this, R.anim.hyperspace_jump); spaceshipimage.startanimation(hyperspacejumpanimation);

Frame Animation res/anim/rocket_thrust.xml <animation-list xmlns:android="http://schemas.android.com/apk/res/android " android:oneshot="true"> <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> </animation-list>

Frame Animation Corresponding code: private AnimationDrawable rocketanimation; public void oncreate(bundle savedinstancestate) {... ImageView rocketimage = (ImageView) findviewbyid(r.id.rocket_image); rocketimage.setbackgroundresource( R.drawable.rocket_thrust); rocketanimation = (AnimationDrawable) rocketimage.getbackground(); private void somelistener(...) { rocketanimation.start();

Update views from main-thread Graphic components Canvas defines what to "draw" and holds a bit map representing the pixels Drawables for drawing primitives, e.g. Rect, Path, text, Bitmap, image, Animations Paint - describes colors and styles for the drawing Color (int), Typeface, Update view: extend the View class and override ondraw(canvas)

Canvas Holds the surface upon which your graphics will be drawn and thus all of your "draw" calls public class MyView extends View {... public void ondraw(canvas canvas) { Paint paint = new Paint(); paint.setcolor(color.white); canvas.drawpaint(paint);... private void onsomeaction( ) {... invalidate(); // Request a call to ondraw...

ondraw + Canvas

ondraw + Canvas public class BasicGraphicsView extends View { @Override protected void ondraw(canvas canvas) { // Current size of this view int w = this.getwidth(), h = this.getheight(); int offset = (h - w)/2; // Background Paint bgpaint = new Paint(); bgpaint.setcolor(colorlightgrey); Canvas.drawPaint(bgPaint); // Fill a rectangle Paint rectpaint = new Paint(); rectpaint.setcolor(color.white); canvas.drawrect(0, offset, w, h-offset, rectpaint);

ondraw, draw an image public class BasicGraphicsView extends View { private Drawable cross; public BasicGraphicsView(Context context) { super(context); Resources resources = context.getresources(); cross = (Drawable) resources.getdrawable(r.drawable.cross); protected void ondraw(canvas canvas) { int x = 100, y = 200; int iw = cross.getintrinsicwidth(); int ih = cross.getintrinsicheight(); Rect bounds = new Rect(x, y, x+iw, y+ih); cross.setbounds(bounds); cross.draw(canvas);

Canvas, off screen drawing Drawing is performed on an underlying Bitmap holding the pixels You can create a new Canvas, e.g. for off screen drawing Provide the Bitmap upon which drawing will actually be performed Bitmap offscreen= Bitmap.createBitmap( width, height, Bitmap.Config.ARGB_8888); Canvas temp = new Canvas(offscreen); // Draw off screen, using the offscreen canvas temp.drawrect(...);... // Copy the bitmap to the Canvas associated with screen canvas.drawbitmap(offscreen,...);

SurfaceView Provides a surface in which a secondary thread can render in to the screen, at it s own chosen speed Surface a handle on to a raw buffer being managed by the screen compositor Use SurfaceView when your view constantly needs updates, e.g. a game view Penalty: Memory consuming Implement SurfaceHolder.Callback methods called (by the main-thread) when the surface is created, changed, or destroyed

SurfaceView Make sure the secondary (drawing ) thread only touches the underlying surface between SurfaceHolder.Callback.surfaceCreated() and SurfaceHolder.Callback.surfaceDestroyed() public void run() { while(running) {... Canvas canvas = holder.lockcanvas(); { Paint paint = new Paint(); paint.setcolor(color.white); canvas.drawpaint(paint);... holder.unlockcanvasandpost(canvas); try { Thread.sleep(time);...

SurfaceView The Activity holding the SurfaceView and graphics thread must override the appropriate life cycle call-backs public class SurfaceActivity extends Activity { private SnowSurfaceView view; public void oncreate(bundle savedinstancestate) {... setcontentview(view); protected void onresume() { super.onresume(); view.resume(); protected void onpause() { super.onpause(); view.pause();

SurfaceView + graphics thread Model tasks and objects in the game as classes Use SurfaceView + thread Off screen drawing Manage life cycle call backs Reuse objects (e.g. Paint) Check for actual screen size and changes in size and orientation SurfaceViewExample.zip

Touch Events Fired when user touches the screen Listen to touch event: Extend View and override ontouchevent (MotionEvent event), or... Implement View.OnTouchListener and override ontouch(view v, MotionEvent event)

Touch Events @Override public boolean ontouchevent(motionevent event) { int action = event.getaction(); switch (action) { case (MotionEvent.ACTION_DOWN): // Touch screen pressed break; case (MotionEvent.ACTION_UP): // Touch screen touch ended break; case (MotionEvent.ACTION_MOVE): // Moved across screen break; case (MotionEvent.ACTION_CANCEL): // Touch event cancelled break; return super.ontouchevent(event);

Touch Events public class TouchView extends View {... @Override public boolean ontouchevent(motionevent event) { if(event.getaction() == MotionEvent.ACTION_DOWN) { int w = cross.getintrinsicwidth(); int h = cross.getintrinsicheight(); int x = (int) event.getx(); int y = (int) event.gety(); crossbounds = new Rect(x-w/2, y-w/2, x+w/2, y+h/2); invalidate(); // Request a redraw of this view return true; return false;

Bonus: Define listener classes in XML! The layout file <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/startbutton" android:text="start game" android:onclick="onstartclick > </Button> The source code public void onstartclick (View view) { Intent intent = new Intent(...); startactivity(intent);

Readings http://developer.android.com/guide/topics/graphics /index.html Code examples at Bilda Meier, Chapter 4, 15 (pp 489 - )