advance ui development and design

82
Advance UI : Development and Design Rakesh Kumar Jha M. Tech, MBA Delivery Manager

Upload: rakesh-jha

Post on 27-Jun-2015

374 views

Category:

Mobile


0 download

DESCRIPTION

Here you will learn - What is Android windowing system Overview Architecture Components Development Code

TRANSCRIPT

Page 1: Advance ui  development and design

Advance UI : Development and Design

Rakesh Kumar JhaM. Tech, MBA

Delivery Manager

Page 2: Advance ui  development and design

Android Windowing System

What is Android windowing systemOverviewArchitectureComponentsDevelopmentCodeQ & A

Page 3: Advance ui  development and design

What is Android windowing system

In computing, a windowing system (or window system) is a type of graphical user interface (GUI) which implements the WIMP (windows, icons, menus, pointer) paradigm for a user interface.

Page 4: Advance ui  development and design

What is Android windowing system

Most popular windowing systems are X11 and Wayland

Most popular widget toolkits are GTK+/Clutter and Qt

Most popular desktop environments are GNOME and the KDE Software Compilation

Page 5: Advance ui  development and design

X Window

The X Window System (sometimes referred to as "X" or as "XWindows") is an open, cross-platform, client/server system for managing a windowed graphical user interface in a distributed network.

is a windowing system for bitmap displays, common on UNIX-like computer operating systems.

Page 6: Advance ui  development and design

System Architecture

Page 7: Advance ui  development and design

System Architecture

Page 8: Advance ui  development and design

System Architecture

Page 9: Advance ui  development and design

Building Blocks

There are more, but we focus on

SurfaceManagerWindowManagerActivityManager

Page 10: Advance ui  development and design

SurfaceManager

frameworks/base/libs/surfaceflinger/a.k.a SurfaceFlingerAllocate surfaces. Backed by shmem/pmem/?Composite surfaces

Page 11: Advance ui  development and design

SurfaceManager

It is used for compositing window manager with off-screen buffering.

Off-screen buffering means you cant directly draw into the screen, but your drawings go to the off-screen buffer.

There it is combined with other drawings and form the final screen the user will see.

This off screen buffer is the reason behind the transparency of windows.

Page 12: Advance ui  development and design

WindowManager

frameworks/base/services/java/com/android/server/WindowManagerService.java

(Ask SurfaceManager to) create/layout surfaces on behalf of the clients

Dispatch input events to clientsTransition animationWindowManagerPolicy

Page 13: Advance ui  development and design

WindowManager

The interface that apps use to talk to the window manager.

Use Context.getSystemService(Context.WINDOW_SERVICE) to get one of these.

Page 14: Advance ui  development and design

WindowManager

Each window manager instance is bound to a particular Display.

To obtain a WindowManager for a different display, use createDisplayContext(Display) to obtain a Context for that display, then use Context.getSystemService(Context.WINDOW_SERVICE) to get the WindowManager.

Page 15: Advance ui  development and design

ActivityManager

frameworks/base/services/java/com/android/server/am/

Manage lifecycles of activitiesManage stacking of activitiesDispatch intentsSpawn processes

Page 16: Advance ui  development and design

ActivityManager

Interact with the overall activities running in the system.

Information you can retrieve about the available memory

Information you can retrieve about any processes that are in an error condition.

Information you can retrieve about a running process.

ActivityManager.MemoryInfo, ActivityManager.RunningAppProcessInfo

Page 17: Advance ui  development and design

An activity has one or more windows (e.g. dialogs)

A window has one or more surfaces (e.g. surface views)

However, in window manager, a window is called a session

A surface is called a window

Page 18: Advance ui  development and design

How Android Draws Views?

• When an Activity receives focus, it will be requested to draw its layout.

• The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.

Page 19: Advance ui  development and design

How Android Draws Views?

• When an Activity receives focus, it will be requested to draw its layout.

• The Android framework will handle the procedure for drawing, but the Activity must provide the root node of its layout hierarchy.

• Drawing the layout is a two pass process: a measure pass and a layout pass.

Page 20: Advance ui  development and design

Handling Gestures

Page 21: Advance ui  development and design

Handling Gestures

Some examples of common multi-touch gestures and actions you might use include:Pinch to zoom in, spread to zoom out.Basic dragging in order to move, adjust, scroll,

and position.Flick to jump to the next screen or scroll extra

fast.Tap and hold to open an item or context menu.Multi-finger drag often scrolls faster!

Page 22: Advance ui  development and design

Handling Gestures

Handling multi touch gestureDetecting common gestureManaging touch eventAnimating a scroll gestureTracking movementDragging & scalling

Page 23: Advance ui  development and design

Handling Gestures

Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures.

Page 24: Advance ui  development and design

Handling Gestures

Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not.

Page 25: Advance ui  development and design

Handling Gestures

To use it , you need to create an object of GestureDetector and then extend another class with GestureDetector.SimpleOnGestureListener to act as a listener and override some methods.

Page 26: Advance ui  development and design

Handling Gestures GestureDetector myG;myG = new GestureDetector(this,new Gesture()); class Gesture extends GestureDetector.SimpleOnGestureListener{ public boolean onSingleTapUp(MotionEvent ev) { } public void onLongPress(MotionEvent ev) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { }}}

Page 27: Advance ui  development and design

Handling Pinch Gesture

Android provides ScaleGestureDetector class to handle gestures like pinch e.t.c. In order to use it , you need to instantiate an object of this class. Its syntax is as follow: -

ScaleGestureDetector SGD; SGD = new ScaleGestureDetector(this,new ScaleListener());

Page 28: Advance ui  development and design

Handling Pinch Gesture We have to define the event listener and override a function OnTouchEvent to make it

working.

public boolean onTouchEvent(MotionEvent ev) { SGD.onTouchEvent(ev); return true;}private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener { @Override public boolean onScale(ScaleGestureDetector detector) { float scale = detector.getScaleFactor(); return true; }}

Page 29: Advance ui  development and design

Handling Pinch Gesture

Apart from the pinch gestures , there are other methods avaialible that notify more about touch events. They are listed below:

1 getEventTime()This method get the event time of the current event being processed..

2 getFocusX()This method get the X coordinate of the current gesture's focal point.

3 getFocusY()This method get the Y coordinate of the current gesture's focal point.

4 getTimeDelta()This method return the time difference in milliseconds between the previous accepted scaling event and the current scaling event.

5 isInProgress()This method returns true if a scale gesture is in progress..

6 onTouchEvent(MotionEvent event)This method accepts MotionEvents and dispatches events when appropriate.

Page 30: Advance ui  development and design

Handling Pinch Gesture

Use of ScaleGestureDetector class. It creates a basic application that allows you to zoom in and out through pinch.

Page 31: Advance ui  development and design

Animation

Page 32: Advance ui  development and design

Animation in Android

• Animation in android is possible from many ways.

• making animation called tweened animation.

Page 33: Advance ui  development and design

Animation in Android

• Animation in android is possible from many ways.

• making animation called tweened animation.

Page 34: Advance ui  development and design

Tween Animation

• Tween Animation takes some parameters such as start value , end value, size , time duration , rotation angle e.t.c and perform the required animation on that object.

Page 35: Advance ui  development and design

Tween Animation

• In order to perform animation in android , call a static function loadAnimation() of the class AnimationUtils.

Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.myanimation);

second parameter, it is the name of the our animation xml file.

Page 36: Advance ui  development and design

Sr.No Method & Description1 start()

This method starts the animation.

2 setDuration(long duration)This method sets the duration of an animation.

3 getDuration()This method gets the duration which is set by above method

4 end()This method ends the animation.

5 cancel()This method cancels the animation.

Tween Animation

Page 37: Advance ui  development and design

Tween AnimationImageView image1 = (ImageView)findViewById(R.id.imageView1); image.startAnimation(animation);

Page 38: Advance ui  development and design

Zoom in animation

• To perform a zoom in animation , create an XML file under anim folder under res directory, and put zoom xml code.

<set xmlns:android="http://schemas.android.com/apk/res/android"> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:fromXScale="0.5" android:toXScale="3.0" android:fromYScale="0.5" android:toYScale="3.0" android:duration="5000" android:pivotX="50%" android:pivotY="50%" > </scale> </set>

Page 39: Advance ui  development and design

Zoom in animation

• The parameter fromXScale and fromYScale defines the start point and the parameters toXScale andtoYScale defines the end point.

• The duration defines the time of animation and the pivotX, pivotYdefines the center from where the animation would start.

Page 40: Advance ui  development and design

Custom UI Views Architecture

Page 41: Advance ui  development and design

• Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View and ViewGroup.

• A partial list of available widgets includes Button, TextView, EditText, ListView, CheckBox, RadioButton, Gallery, Spinner, and the more special-purpose AutoCompleteTextView, ImageSwitcher, and TextSwitcher.

• Among the layouts available are LinearLayout, FrameLayout, RelativeLayout, and others

Page 42: Advance ui  development and design

• If none of the prebuilt widgets or layouts meets your needs, you can create your own View subclass.

Page 43: Advance ui  development and design

View Hierarchy Design

• Sometimes your application's layout can slow down your application. To help debug issues in your layout, the Android SDK provides the Hierarchy Viewer and lint tools.

Page 44: Advance ui  development and design

View Hierarchy Design

• The Hierarchy Viewer application allows you to debug and optimize your user interface

• It provides a visual representation of the layout's View hierarchy

Page 45: Advance ui  development and design

View Hierarchy Design

• Android lint is a static code scanning tool that helps you optimize the layouts and layout hierarchies of your applications, as well as detect other common coding problems.

Page 46: Advance ui  development and design

Using Hierarchy Viewer

• Connect your device or launch an emulator. To preserve security, Hierarchy Viewer can only connect to devices running a developer version of the Android system.

• If you have not done so already, install the application you want to work with.

• Run the application, and ensure that its UI is visible.• From a terminal, launch hierarchyviewer from

the <sdk>/tools/ directory.• Window will launched with device list• Select apps name(packagename) and perform operaion.

Page 47: Advance ui  development and design
Page 48: Advance ui  development and design

Using Hierarchy Viewer

• The View Hierarchy window displays the View objects that form the UI of the Activity that is running on your device or emulator.

• You should see four panes:-– Tree View:– Tree Overview, – Layout View,– Properties View

Page 49: Advance ui  development and design

Using Hierarchy Viewer

• When the UI of the current Activity changes, the View Hierarchy window is not automatically updated.

• To update it, click Load View Hierarchy at the top of the window.

Page 50: Advance ui  development and design
Page 51: Advance ui  development and design

Working with an individual View in Tree View

• Each node in Tree View represents a single View. Some information is always visible.

• Starting at the top of the node, you see the following:

Page 52: Advance ui  development and design

Working with an individual View in Tree View

1. View class: The View object's class.2. View object address: A pointer to View object.3. View object ID: The value of

the android:id attribute.4. Performance indicators:

1. Green: Fastest, 50% faster than view object 2. Yellow : slower 50% of all the View objects3. Red : slowest one in the tree

Page 53: Advance ui  development and design

Working with an individual View in Tree View

5. View index: The zero-based index of the View in its parent View. If it is the only child, this is 0.

Page 54: Advance ui  development and design

Using lint to Optimize Your UI

• The Android lint tool lets you analyze the XML files that define your application's UI to find inefficiencies in the view hierarchy.

• Note: The Android layoutopt tool has been replaced by the lint tool beginning in ADT and SDK Tools revision 16. The lint tool reports UI layout performance issues in a similar way as layoutopt, and detects additional problems.

Page 55: Advance ui  development and design

Using lint to Optimize Your UI

• Improving Your Code with lint• The Android SDK provides a code scanning

tool called lint that can help you to easily identify and correct problems with the structural quality of your code, without having to execute the app or write any test cases.

Page 56: Advance ui  development and design

Using lint to Optimize Your UI

• The lint tool checks your Android project source files for potential bugs and optimization improvements for correctness, security, performance, usability, accessibility, and internationalization.

• You can run lint from the command-line or from the Eclipse environment.

Page 57: Advance ui  development and design

Running lint from Eclipse

If the ADT Plugin is installed in your Eclipse environment, the lint tool runs automatically when you perform one of these actions:Export an APKEdit and save an XML source file in your

Android project (such as a manifest or layout file)

Use the layout editor in Eclipse to make changes

Page 58: Advance ui  development and design

Running lint from the Command-Line

• To run lint against a list of files in a project directory:

int [flags] <project directory>lint --check MissingPrefix myproject

Page 59: Advance ui  development and design

Configuring lint

You can configure lint checking at different levels:

Globally, for all projectsPer projectPer filePer Java class or method (by using

the @SuppressLint annotation), or per XML element (by using the tools:ignoreattribute.

Page 60: Advance ui  development and design

Configuring lint in Eclipse

You can configure global, project-specific, and file-specific settings for lint from the Eclipse user interface.

Page 61: Advance ui  development and design

Global preferences

• Open Window > Preferences > Android > Lint Error Checking.

• Specify your preferences and click OK.

Page 62: Advance ui  development and design

Project and file-specific preferences

• Run the lint tool on your project by right-clicking on your project folder in the Package Explorer and selecting Android Tools > Run Lint: Check for Common Errors.

• From the Lint Warnings view, use the toolbar options to configure lint preferences for individual projects and files in Eclipse.

Page 63: Advance ui  development and design

Project and file-specific preferences

The options you can select include:• Suppress this error with an annotation/attribute - If the issue

appears in a Java class, the lint tool adds a@SuppressLint annotation to the method where the issue was detected. If the issue appears in an .xml file, lintinserts a tools:ignore attribute to disable checking for the lint issue in this file.

• Ignore in this file - Disables checking for this lint issue in this file.• Ignore in this project - Disables checking for this lint issue in this

project.• Always ignore - Disables checking for this lint issue globally for all

projects.

Page 64: Advance ui  development and design

Configuring the lint file

You can specify your lint checking preferences in the lint.xml file.

If you are creating this file manually, place it in the root directory of your Android project.

If you are configuring lint preferences in Eclipse, the lint.xml file is automatically created and added to your Android project for you.

Page 65: Advance ui  development and design

Sample lint.xml file<?xml version="1.0" encoding="UTF-8"?><lint> <!-- Disable the given check in this project --> <issue id="IconMissingDensityFolder" severity="ignore" />

<!-- Ignore the ObsoleteLayoutParam issue in the specified files --> <issue id="ObsoleteLayoutParam"> <ignore path="res/layout/activation.xml" /> <ignore path="res/layout-xlarge/activation.xml" /> </issue>

<!-- Ignore the UselessLeaf issue in the specified file --> <issue id="UselessLeaf"> <ignore path="res/layout/main.xml" /> </issue>

<!-- Change the severity of hardcoded strings to "error" --> <issue id="HardcodedText" severity="error" /></lint>

Page 66: Advance ui  development and design

Event Propagation and Event Handling in Views

For each application, a ViewRootImpl object is created to handle communications with the remote system WindowManagerService object.

The communication is through a Linux pipe which is encapsulated in an InputChannel object (mInputChannel field in class ViewRootImpl).

TheViewRootImpl object also registers an instance of InputEventReceiver when the first View object is registered with it.

Page 67: Advance ui  development and design

Event Propagation and Event Handling in Views

public void setView(View view, ...) {... mInputEventReceiver = new WindowInputEventReceiver(mInputChannel, Looper.myLooper());... }The constructor of class WindowInputEventReceiver (class WindowManagerService extends from classInputEventReceiver) calls a native methond nativeInit(...):

Page 68: Advance ui  development and design

Event Propagation and Event Handling in Views

58 public InputEventReceiver(InputChannel inputChannel, Looper looper) {...66 mInputChannel = inputChannel;67 mMessageQueue = looper.getQueue();68 mReceiverPtr = nativeInit(this, inputChannel, mMessageQueue);...71 }

Page 69: Advance ui  development and design

Event Propagation and Event Handling in Views

Three parameters are passed to the native function nativeInit: 1) The receiver object itself; 2) TheInputChannel object passed from the ViewRootImpl object. 3) The main message queue (an object of class MessageQueue) of the

application.

Page 70: Advance ui  development and design

Event Propagation and Event Handling in Views

227 static jint nativeInit(JNIEnv* env, jclass clazz, jobject receiverObj,228 jobject inputChannelObj, jobject messageQueueObj) {229 sp<InputChannel> inputChannel = android_view_InputChannel_getInputChannel(env,230 inputChannelObj);...236 sp<MessageQueue> messageQueue = android_os_MessageQueue_getMessageQueue(env, messageQueueObj);...242 sp<NativeInputEventReceiver> receiver = new NativeInputEventReceiver(env,243 receiverObj, inputChannel, messageQueue);244 status_t status = receiver->initialize();...254 }

Page 71: Advance ui  development and design

Event Propagation and Event Handling in Views

Included in the event listener interfaces are the following callback methods:-

onClick()onLongClick()onFocusChange()onKey()onTouch()onCreateContextMenu()

Page 72: Advance ui  development and design

Event Propagation and Event Handling in Views

Included in the event listener interfaces are the following callback methods:-

onClick()onLongClick()onFocusChange()onKey()onTouch()onCreateContextMenu()

Page 73: Advance ui  development and design
Page 74: Advance ui  development and design
Page 75: Advance ui  development and design

Localisation and Accessibility

Page 76: Advance ui  development and design

• An android application can run on many devices in many different regions.

• In order to make your application more interactive, your application should handle text,numbers,files e.t.c in ways appropriate to the locales where your application will be used.

Page 77: Advance ui  development and design

Localizing Strings

• In order to localize the strings used in your application , make a new folder under res with name ofvalues-local where local would be the replaced with the region.

• For example, in the case of italy, the values-it folder would be made under res. It is shown in the image below:

Page 78: Advance ui  development and design

Localizing Strings

Page 79: Advance ui  development and design

Localizing Strings

• Once that folder is made, copy the strings.xmlfrom default folder to the folder you have created. And change its contents. For example, i have changed the value of hello_world string.

Page 80: Advance ui  development and design

Localizing Strings

• ITALY, RES/VALUES-IT/STRINGS.XML

<;?xml version="1.0" encoding="utf-8"?><resources>

<string name="hello_world">Ciao mondo!</string></resources>

Page 81: Advance ui  development and design

Localizing Strings

• Chinese, RES/VALUES-zh/STRINGS.XML

<;?xml version="1.0" encoding="utf-8"?><resources>

<string name="hello_world">Hola Mundo!</string></resources>

Page 82: Advance ui  development and design

Localizing Strings

• FRENCH, RES/VALUES-FR/STRINGS.XML

<;?xml version="1.0" encoding="utf-8"?><resources>

<string name="hello_world">Bonjour le monde !</string></resources>