Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Gestures, Animations, Sensors in Android

Gestures, Animations, Sensors in Android

Orhan Obut

October 26, 2014
Tweet

More Decks by Orhan Obut

Other Decks in Programming

Transcript

  1. Gestures, Animations, Sensors in Android Orhan Obut © 2003–2014 Monitise

    Group Limited. All Rights Reserved. Confidential.
  2. View Animation • Older system and can only be used

    for Views • Easy to setup and offers enough capabilities to meet many application's needs. • Perform tweened animation on Views • A tween animation can perform a series of simple transformations (position, size, rotation, and transparency) • Defined by either XML or Android code • XML file is recommended because it's more readable, reusable, and swappable
  3. Property Animation • Introduced in API Level 11 • Lets

    you animate properties of any object. • Extensible and lets you animate properties of custom types.
  4. Property Animation Code Snippet ObjectAnimator animator = ObjectAnimator.ofFloat(foo, TRANSLATION_X, 0f,

    40f); animator.setDuration(40); animator.setTarget(View); animator.start();
  5. Drawable Animation • Involves displaying Drawable resources one after another,

    like a roll of film. • Useful if you want to animate things that are easier to represent with Drawable resources, such as a progression of bitmaps.
  6. Drawable Animation android:oneshot = false for infinite loop <animation-list android:oneshot="true">

    <item drawable="@drawable/d1" android:duration="100" /> <item drawable="@drawable/d2" android:duration="100" /> <item drawable="@drawable/d3" android:duration="100" /> </animation-list> rocketImage.setBackgroundResource(R.drawable.hede);
  7. Canvas and Drawables When drawing 2D graphics you have 2

    options 1. Draw your graphics or animations into a View object from your layout. 2. Draw your graphics directly to a Canvas.
  8. Draw on a View • Consider a custom view and

    use View.onDraw() when you do not need a significant amount of processing • Android framework will only call onDraw() as necessary. • Use invalidate() when you want to do redraw NOTE : Use postInvalidate() if you request from another thread other than main activity thread.
  9. Draw with a Canvas When you would like to perform

    specialized drawing and/or control the animation of graphics, you should do so by drawing through a Canvas Bitmap b = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(b);
  10. Draw on a SurfaceView • Special subclass of View that

    offers a dedicated drawing surface. • The aim is to use background thread, therefore the app isn't required to wait until the system's view hierarchy is ready to draw • Extend SurfaceView • Implement SurfaceHolder.Callback • Handle everything via a SurfaceHolder
  11. Shape Drawable • Use ShapeDrawable when you want to dynamically

    draw 2D graphics. • You can programmatically draw primitive shapes and style. • You can also create shapes in XML and use them.
  12. Nine-patch • Stretchable bitmap image • It must be saved

    with the extension .9. png and save into the res/drawable/ directory • Use Draw 9-patch tools (WYSIWYG)
  13. OpenGL • Android supports OpenGL ES 1.0,2.0 and 3.0, with

    Android framework APIs as well as natively with the Native Development Kit (NDK).
  14. Hardware Acceleration • Introduced in Android 3.0 (API level 11)

    • You can hardware accelerate the majority of the drawing done by the Canvas APIs to further increase their performance. • In manifest, add the following attribute <application android:hardwareAccelerated="true" ...>
  15. • Touch Feedback • Reveal Effect • Activity Transitions •

    Curved Motion • Animating View State Changes • Drawable Tilting • Extracting Colors from an Image
  16. Gestures • One or more fingers touch the screen •

    Interpret the pattern • Gesture has 2 phases : o Gather data o Interpret data
  17. Gather Data • Override onTouchEvent() to intercept touch events. @Override

    public boolean onTouchEvent(MotionEvent event) { }
  18. Interpret Data • Use GestureDetector class for common gestures •

    Use GestureOverlayView for gesture signatures • Use ScaleGestureDetector for scale • Use your own custom pattern if not enough
  19. Gestures • Fling • Double Tap • Single Tap •

    Down • Scroll • Scale (ScaleGestureDetector) • Signatures (GestureOverlayView)
  20. Motion Sensors • Monitor the motion of device such as

    tilt, shake, rotation or swing • All of the motion sensors return multi-dimensional arrays of sensor values for each SensorEvent
  21. Motion Sensors • Accelerometer • Gravity • Gyroscope • Linear

    accelerometer • Rotation vector sensor • Significant motion sensor • Step counter sensor (Kitkat) • Step detector sensor (Kitkat) Motion sensors by themselves are not typically used to monitor device position, but they can be used with other sensors
  22. Motion Sensors Examples • Shake and tilt features • Control

    a car in a game • Control a ball in a game • Make a run or walk application
  23. Position Sensors • Useful for determining device's physical position in

    the world's frame of reference. • Returns multidimensional array
  24. Position Sensors • Geomagnetic field sensor • Game rotation vector

    (Exclude geomagnetic) • Geomagnetic rotation vector sensor (use magnetometer) • Orientation sensor (Deprecated in API 8) • Proximity sensor
  25. Position Sensors Examples • When holding phone close to ear,

    screen goes off • Use them to control rotation • Use for a compass