android custom views

23
Android Custom Views

Upload: babar-sanah

Post on 09-May-2015

480 views

Category:

Technology


1 download

DESCRIPTION

Android Custom Views

TRANSCRIPT

Page 1: Android Custom Views

Android Custom Views

Page 2: Android Custom Views

• Name: Babar Sana

• Department: Android

• Designation: Software Engineer

Page 3: Android Custom Views

Table of Contents

1.Custom Views• Default views• How Android Draws views• Reason for creating views• Responsibility of views• Using new views in your layout files• Create Screenshot of Views

Page 4: Android Custom Views

Table of Contents

2.Compound Views

3.Creating Custom Views• Creating Custom Views• Measurements• Defining Custom layout manager

4.Life Cycle•Life Cycle Events related to Windows•Traversal life cycle Events•Activity Life Cycle

Page 5: Android Custom Views

5.Define Additional Attributes

6.Question

Table of Contents

Page 6: Android Custom Views

1.Custom Views

The Android framework provides several default views but developer can also create their custom views and use them in their application. The base class a view is the View.

Page 7: Android Custom Views

How Android Draws Views

• Once an Activity receives the focus, it must provide the root node of its layout hierarchy to the Android system. Afterwards the Android system starts the drawing procedure.

• Drawing begins with the root node of the layout. The layout hierarchy is traversed in the order of declaration, i.e., parents are drawn before their children and children are drawn in the order of declaration.

• Drawing the layout is a two pass process:• measuring pass - implemented in the measure(int, int) method and is a top-down traversal

of the view hierarchy. Every view stores its measurements.

• layout pass - implemented in the layout(int, int, int, int) method is also a top-down traversal of the view hierarchy. During this phase each layout manager is responsible for positioning all of its children. It uses the the sizes computed in the measure pass.

Page 8: Android Custom Views

How Android Draws Views

• A view or activity can retrigger the measure and layout pass with a call to the requestLayout() method.

• After the measure and layout calculation, the views draw themselves. This operation can be triggered with the invalidate() method from the View class.

Page 9: Android Custom Views

Reasons for Creating Views

• View are typically created to provide a user interface experience with is not possible with the default views.

• Using custom view allows the developer allow to do certain performance optimization

Page 10: Android Custom Views

Responsibility of Views

• Views are responsible for measuring, layouting and drawing themselves and their child elements (in case of a ViewGroup)

• Views are also responsible for saving their UI state and handling touch events

Page 11: Android Custom Views

 Ways of creating custom views

• Customs views are typically classified as compound views or custom views. It is possible to create custom views by:

• Compound views• Custom Views

– By extending an existing view• By extending the View class

Page 12: Android Custom Views

Using new views in layout files

• Custom and compound views can be used in layout files. For this you need to use the full qualified name in the layout file, e.g. using the package and class name.

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" >

<de.android.ownview.MyDrawView android:id="@+id/myDrawView1" android:layout_width="wrap_content" android:layout_height="wrap_content" />

</LinearLayout>

Page 13: Android Custom Views

 Create screenshots (Images) of views

• Every View class support the creating of an image of its current display. The following coding shows an example for that.

# Build the Drawing Cacheview.buildDrawingCache();

# Create BitmapBitmap cache = view.getDrawingCache();

# Save BitmapsaveBitmap(cache);view.destroyDrawingCache();

Page 14: Android Custom Views

Compound Views

• Compound views are a very powerful way of reusing code and UI when programming for Android. A Compound View is essentially a collection of two or more other UI elements placed together, which you can use as if it was a single View. For instance, you could use Compound Views to create a ListView item which contains a small picture and some text. You can also add custom logic to this Compound View.

Page 15: Android Custom Views

Creating custom views

• By extending the View class or one of its subclasses you can create your custom view.

• For drawing view use the onDraw() method. In this method you receive a Canvas object which allows you to perform drawing operations on it, e.g. draw lines, circle, text or bitmaps. If the view should be re-drawn you call the invalidate() method which triggers a call to the onDraw() method of this view.

Defining custom layout managers• You can implement your custom layout manager by extending the ViewGroup

class. This allows you to implement more efficient layout managers or to implement effects which are currently missing in the Android platform.

• A custom layout manager can override the onMeasure() and onLayout() method and specialize the calculation of its children. For example it can leave out the time consuming support of layout_weight of the LinearLayout class.

Page 16: Android Custom Views

Life Cycle

 Life cycle events related to the window• A view is displayed if it is attached to a layout hierarchy which is attached to a

window. A view has several life cycle hooks.• The onAttachedToWindow() is called once the window is available.• The onDetachedFromWindow() is used when the view is removed from its parent

(and if the parent is attached to a window). This happens for example if the activity is recycled (e.g. via the finished() method call) or if the view is recycled in a ListView. The onDetachedFromWindow() method can be used to stop animations and to clean up resources used by the view.

Page 17: Android Custom Views

Traversal life cycle events

• Traversals life cycle events consists of Animate, Measure, Layout and Draw.

• All views must know how to measure and layout themselves. The requestLayout() method call tells the view to measure and layout itself. As this operation may influence the layout of other views it calls also requestLayout() of its parent.

• The onMeasure() method determines the size for the view and its children. It must set the dimension via the setMeasuredDimension() method is this method call before returning.

• The onLayout() positions the views based on the result of the onMeasure() method call. This call happens typically once, whichonMeasure() can happens once.

Page 18: Android Custom Views

Creating custom views

Creation

Constructor:There is a form of the constructor that are called when the view is created from code and a form that is called when the view is inflated from a layout file. The second form should parse and apply any attributes defined in the layout file.OnFinishInflate()Called after a view and all of its children has been inflated from XML.This is called as the last phase of inflation, after all child views have been added

Page 19: Android Custom Views

Creating custom views

Layout

onMeasure(int ,int)Called to determine the size requirements for this view and all of its children.When overriding this method, you must call setMeasuredDimension(int, int) measured width and height of this view. Failure to do so will trigger an IllegalStateExceptiononLayout (boolean changed, int left, int top, int right, int bottom)Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.protected void onSizeChanged (int w, int h, int oldw, int oldh)This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.

Page 20: Android Custom Views

Creating custom views

Draw:

protected void onDraw (Canvas canvas)Implement this to do your drawing.

Page 21: Android Custom Views

Creating custom views

Event Processing:onKeyDown(int, KeyEvent)Called when a new hardware key event occurs.onKeyUp(int, KeyEvent)Called when a hardware key up event occurs.onTrackballEvent(MotionEvent)Called when a trackball motion event occurs.onTouchEvent(MotionEvent)Called when a touch screen motion event occurs.

Page 22: Android Custom Views

Creating custom views

Focus:

onFocusChanged(boolean, int, android.graphics.Rect)Called when the view gains or loses focus.onWindowFocusChanged(boolean)Called when the window containing the view gains or loses focus.

Page 23: Android Custom Views

Creating custom views

Attaching:onAttachedToWindow()Called when the view is attached to a window.onDetachedFromWindow()Called when the view is detached from its window.onWindowVisibilityChanged(int)Called when the visibility of the window containing the view has changed.