Graphics.  2D Graphics using  ImageView  Canvas  ViewAnimation.

Slides:



Advertisements
Similar presentations
Threads, Surface Views and Real-Time Games. Background Most of the Android apps we’ve covered so far have been single threaded – And Event driven – An.
Advertisements

1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
Graphics with Canvas, SurfaceView, and multitouch processing (panning and multitouch zoom) GraphicsWithCanvas_2012.pptx.
Multimedia.  Android provides built-in encoding/decoding for a variety of common media types  Allows you to play & record audio, still images & video.
CS378 - Mobile Computing 2D Graphics A Crash Course in Using (Android) 2D Graphics Libraries.
Cannon Game 1 Fall 2014 CS7020: Game Design and Development.
Basic 2D Graphics in Android. Android Graphics Programming There are many ways to do graphics programming in Android – 2D vs. 3D – static vs. dynamic.
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.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Camera. Make new project – Give permission to use camera Make button called TakePicture Make button called ShowLastPic Add SurfaceView to layout – Surface.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
My own View Android development Maarten Pennings 2011 oct 14.
2D Graphics: Part 1. Android Graphics Libraries 2D Graphics –custom 2D graphics library in packages android.graphics android.graphics.drawable android.graphics.drawable.shapes.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
Graphics in Android 1 Fall 2012 CS2302: Programming Principles.
Custom Views, Drawing, Styles, Themes, ViewProperties, Animations, oh my!
Basic Drawing Techniques
2D Graphics: Part 2.
Chapter 10: Move! Creating Animation
6-2 2D Graphics CSNB544 Mobile Application Development Thanks to Utexas Austin.
Flag Quiz App 1 CS7030: Mobile App Development. assets Folder 2 CS7030: Mobile App Development  drawable folder – Image contents at the root level 
ANDROID – INTERFACE AND LAYOUT L. Grewe. Interfaces: Two Alternatives Code or XML  You have two ways you can create the interface(s) of your Application.
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.
Animation.
1/29/ Android Programming: FrameLayout By Dr. Ramji M. Makwana Professor and Head, Computer Engineering Department A.D. Patel.
Field Trip #32 Digital Alarm Clock By Keith Lynn.
Camera. Make new project – CameraFun – Give permission to use camera write_external _storage Make two buttons – id takePictureButton – id showLastPicButton.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Introduction to Android (Part.
Animations 1 Fall 2012 CS2302: Programming Principles.
ANDROID – DRAWING IMAGES – SIMPLE EXAMPLE IN INTERFACE AND EVENT HANDLING L. Grewe.
CIS 3.5 Lecture 2.2 More programming with "Processing"
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.
Cosc 5/4730 Android drawing. Screen support Android is the OS and is used a many different hardware created by different vendors. – Screen size For simplicity,
SurfaceView.
Graphics Concepts CS 2302, Fall /17/20142 Drawing in Android.
Painting (Chapter 12) Java Certification Study Group January 25, 1999 Mark Roth.
Copyright© Jeffrey Jongko, Ateneo de Manila University Deconstructing HelloWorld.
Basic Animation. Animation 4 options – Animated.gif – Frame by Frame animation – Tweened animation This is our focus – OpenGL ES Graphics API for more.
A UGMENTED F UTURE OF M OBILE R EALITY Oleg Novosad Mobile / Web / Game Engineer.
Android and s Ken Nguyen Clayton state University 2012.
Graphics in Android 1 CS7030: Mobile App Development.
Mobile Computing Lecture#12 Graphics & Animation.
1 Drawing C Sc 335 Object-Oriented Programming and Design Rick Mercer.
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.
Lecture 3: Animation & Graphics Topics: Animation, Graphics, Drawing Date: Feb 2, 2016.
Graphics & Animation Radan Ganchev Astea Solutions.
Android Application 2D Graphics cs.
Lecture 3: Animation & Graphics
GUI Programming Fundamentals
滑動版面 建國科技大學 資管系 饒瑞佶 2013/7 V1.
CS499 – Mobile Application Development
Lecture 3: Animation & Graphics
Android – Event Handling
Lecture 8: Graphics By: Eliav Menachi.
2D Graphics: Part 2.
S.RENUKADEVI/AP/SCD/ANDROID - Notifications
Mobile Computing With Android ACST 4550 Android Animation
Android Programming Lecture 6
Graphics in Android Fall 2012 CS2302: Programming Principles.
Graphics with Canvas.
Cannon Game App Android How to Program
滑動 建國科技大學 資管系 饒瑞佶.
Lecture 3: Animation & Graphics
Lecture 8: Graphics By: Eliav Menachi.
CIS 470 Mobile App Development
Presentation transcript:

Graphics

 2D Graphics using  ImageView  Canvas  ViewAnimation

 With a View  Simple graphics, no updating  With a Canvas  More complex graphics, with regular updates

 Represents something that can be drawn, such as a bitmap, color, shape, etc.  BitmapDrawable  ColorDrawable  ShapeDrawable

 Can draw Drawables to Views and View subclasses  Programmatically  Via XML

<LinearLayout … android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#FF444444" > <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content"

public class BubbleActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }

public class BubbleActivity extends Activity { public void onCreate(Bundle savedInstanceState) { … setContentView(R.layout.main); LinearLayout ll = …; ImageView bubbleView= new ImageView(this); bubbleView.setBackgroundResource(R.drawable.b128); bubbleView (new LinearLayout.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT)); ll.addView(bubbleView); }

 For drawing primitive shapes, such as rectangles, lines, ovals & rings

<ImageView …/> <ImageView …/>

// cyan_shape.xml // magenta_shape.xml

public class ShapeDrawActivity extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }

public void onCreate(Bundle savedInstanceState) { … RelativeLayout rl = …; myDrawableView magentaView = new myDrawableView(this, Color.MAGENTA); magentaView.setLayoutParams( new LinearLayout.LayoutParams( 100, 100 )); myDrawableView cyanView = new myDrawableView(this, Color.CYAN); cyanView.setLayoutParams( new LinearLayout.LayoutParams( 100, 100 )); …

ImageView spacerView = new ImageView(this); spacerView.setMinimumWidth( 50 ); spacerView.setAlpha( 125 ); LinearLayout ll = new LinearLayout(this); ll.addView(spacerView); ll.addView(cyanView); rl.addView(ll); rl.addView(magentaView); }

… private class myDrawableView extends ImageView { private ShapeDrawable mDrawable; public myDrawableView(Context context, int color) { … mDrawable = new ShapeDrawable(new OvalShape()); mDrawable.getPaint().setColor(color); mDrawable.setBounds( 0, 0, size, size); mDrawable.setAlpha(alpha); } protected void onDraw(Canvas canvas) { mDrawable.draw(canvas); } …

 A 2-layer Drawable  Fades between 1 st & 2 nd layers

// shape_transition.xml <item android:right=" 50 dp"/> <item android:left=" 50 dp"/> // cyan_shape.xml

public void onCreate(Bundle savedInstanceState) { … setContentView(R.layout.main); RelativeLayout rl = … ImageView iv = … TransitionDrawable trans = (TransitionDrawable) getResources().getDrawable(R.drawable.shape_transition); iv.setImageDrawable(trans); rl.addView(iv); trans.startTransition( 5000 ); }

 Helper class for drawing Bitmaps  Bitmap represents a matrix of Pixels  Drawing operations additional require  Something to draw (e.g. Rect, Path, Text, Bitmap)  A paint object (for setting drawing colors & styles)

 Canvas supports multiple drawing methods, e.g.,  drawText()  drawPoints()  drawColor()  drawOval()  drawBitmap()

 Specifies style parameters for drawing, e.g.,  setAntiAlias()  setStrokeWidth()  setTextSize()  setColor()

 TextViews with different paint-related settings  Text  Size, Color, Style, Typeface  Background  Border color, border width, border dash, fill color

 Can use Canvas to draw to generic Views or to SurfaceViews (a View subclass)  Drawing to Views  System provides canvas and controls when drawing occurs  Drawing to SurfaceView  User provides canvas and has greater control over drawing

private class BubbleView extends View { … Paint painter = new Paint; public BubbleView(Context context, Bitmap bitmap) { // Initialize object } protected void onDraw(Canvas canvas) { canvas.drawBitmap(bitmap, y, x, painter); } protected boolean move() { // Move bubble, return false when bubble moves offscreen }

public class BubbleActivity extends Activity { ScheduledFuture mover; public void onCreate(Bundle savedInstanceState) { … RelativeLayout frame = (RelativeLayout) findViewById(R.id.frame); Bitmap bm = BitmapFactory.decodeResource( getResources(),R.drawable.b128); final BubbleView bubbleView = new BubbleView(this, bm); frame.addView(bubbleView); …

ScheduledExecutorService executor = Executors.newScheduledThreadPool( 1 ); mover = executor.scheduleWithFixedDelay(new Runnable() { public void run() { if (!bubbleView.move()) { stop(); } bubbleView.postInvalidate(); } }, 0, 25, TimeUnit.MILLISECONDS); } private void stop) { mover.cancel(true); }

 Create custom class that extends SurfaceView & implements SurfaceHolder.Callback  SurfaceView manages a low-level drawing area called a Surface  SurfaceHolder.Callback declares lifecycle methods called when Surface changes

 Setup  Use SurfaceHandler.getHolder() to acquire Surface  Register for callbacks with addCallback()  Create a thread for drawing operations  Pass it the SurfaceHandler

 To draw  Acquire lock on Canvas  SurfaceHolder.lockCanvas()  Draw  e.g., Canvas.drawBitmap()  Unlock Canvas  SurfaceHolder.unlockCanvasAndPost()

 TransitionDrawable  Tween Animation  Frame-byAnimation

 Animates the contents of a View  Animation - a series of timed changes to View properties, such as size, position, alpha, etc.  Manipulate times to give effect of sequential or simultaneous changes

// icon_animation.xml <alpha android:fromAlpha=" 0.0 " android:toAlpha=" 1.0 " android:duration=" 3000 " … /> <rotate android:fromDegrees=" 0 " android:toDegrees=" 360 " android:pivotX=" 50 %" android:pivotY=" 50 %" android:duration=" 3000 ” android:startOffset=" 3000 " … /> <translate android:fromXDelta=" 0 " android:toXDelta=" 50 " android:fromYDelta=" 0 " android:toYDelta=" 50 " android:startOffset=" 6000 ” android:duration=" 2000 " … />

public class GraphicsTweenAnimationActivity extends Activity { public void onCreate(Bundle savedInstanceState) { … setContentView(R.layout.main); ImageView iv = (ImageView) findViewById(R.id.icon); Animation anim = AnimationUtils.loadAnimation( this,R.anim.icon_animation); iv.startAnimation(anim); }

 Animates a series of View  Each View shown for a specific amount of time

// view_animation.xml …

public class GraphicsFrameAnimationActivity extends Activity { public void onCreate(Bundle savedInstanceState) { … ImageView iv = (ImageView) findViewById(R.id.countdown_frame); iv.setBackgroundResource(R.drawable.view_animation); final AnimationDrawable ivAnim = (AnimationDrawable) iv.getBackground(); final Button button = (Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { if (ivAnim.isRunning()) { ivAnim.stop(); } ivAnim.start(); } …

 Simple case – moving ball  Ball moves diagonally  When ball hits a wall, it reflects  Increment movement each frame  Done for simplicity – not recommended

public class BoxBounceView extends SurfaceView implements Runnable, SurfaceHolder.Callback{ public BoxBounceView(Context context, AttributeSet attrs) { super(context, attrs); holder = getHolder(); holder.addCallback(this); } public void run() { while (running) { Canvas c = null; try { c = holder.lockCanvas(); synchronized (holder) { updatePositions(); doDraw(c); } } finally { if (c != null) { holder.unlockCanvasAndPost(c); } } … }

public class BoxBounceView extends SurfaceView implements Runnable, SurfaceHolder.Callback{ …. public void surfaceCreated(SurfaceHolder holder) { new Thread(this).start(); } private void updatePositions() { bx += dx; by += dy; if (bx + br > 300 || bx - br < 0){ dx = -dx; } if (by + br > 300 || by - br < 0){ dy = -dy; } protected void doDraw(Canvas c) { c.drawRGB(50, 50, 50); Paint p = new Paint(); p.setAntiAlias(true); p.setColor(Color.WHITE); c.drawCircle(bx, by, br, p); } public void surfaceChanged(SurfaceHolder holder, int fmt, int w, int h) { } public void surfaceDestroyed(SurfaceHolder holder) { } … }

 SurfaceView  Needed to access SurfaceHolder (via getHolder())  holder.addCallback(this);  Registers surfaceCreated(), so we know when to draw  c = holder.lockCanvas();  Acquire writable Canvas from holder

 synchronized(holder) ?  Suggested – not necessary  unlockCanvasAndPost(c)  Finalize canvas and display to screen  new Thread(this).start();  Begins rendering  Must be called after surface has been created

 SurfaceHolder.Callback also provides  surfaceChanged(holder, fmt, w, h)  Primarily used to get view width and height  surfaceDestroyed(holder)  may not draw after this public void surfaceDestroyed(SurfaceHolder holder) { boolean retry = true; setRunning(false); while (retry) { try { thread.join(); retry = false; } catch (InterruptedException e) { } }