This document discusses implementing Android design principles in custom views. It begins by outlining principles like "Never lose my stuff" and "Only show what I need when I need it". It then provides examples of implementing these in custom views, such as using a NavigationDrawer or animations. It discusses different types of custom views like compound views that extend existing views. The document also covers performance optimization techniques for custom views like using Canvas versus extending a ViewGroup. Finally, it suggests ideas for implementing the principle of giving "tricks that work everywhere", such as touch gestures.
2. Lars Vogel
Eclipse IDE and
platform developer
Android and Eclipse
developer and trainer
Founder of vogella
GmbH
http://www.vogella.com
visited by > 50 000
developers every day
5. Android design principles
Never lose my stuff
Decide for me but let me have the final say
Only show what I need when I need it
Delight me in surprising ways
Give me tricks that work everywhere
12. Design principles implemented
Only show what I need when I
need it
http://www.vogella.com/tutorials/AndroidActionBar/article.html
Example
Navigation
18. Implementation
mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity
*/
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
getActionBar().setTitle(title);
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle("Open Drawer");
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
23. Animation history
Properties API (3.0)
New simplified options for
Animations (4.1)
ActivityOptions for Activity
animations (4.1)
Scenes and transitions (4.4)
NineOldAndroids to support < 3.0
Android versions
40. Custom View
Extend View class or its subclass
Override some methods
onDraw()
onTouchEvent()
…
Use new class in your layout
41. Canvas API
You draw on a surface creating a Bitmap
Canvas provides API to draw on the canvas
Note: As of Android 4.0, the runtime maps the Canvas API to
OpenGL calls
48. @Override
protected void dispatchDraw(Canvas canvas) {
if (path == null) {
createShineArea();
}
// Draw the shine behind the children.
canvas.drawPath(path, paint);
// Draw the children
super.dispatchDraw(canvas);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
// Invalidate the path whenever the size changes.
path = null;
}
53. View State
To save a view state, Activity calls
onSaveInstanceState() on every its child view which
has an android:id attribute.
To restore a view state, Activity calls
onRestoreInstanceState() on every its child view
which has an android:id attribute.
com.vogella.android.customview.persistence
59. Idea to develop an async custom
view
See http://lucasr.org/2014/05/12/custom-layouts-on-android/
60. Note: Learn about Drawables
http://www.vogella.com/tutorials/AndroidDrawables/article.html
Sometimes you don't need
custom views just a
combination of existing
Drawables