android ui techniques
Post on 03-Jun-2018
239 Views
Preview:
TRANSCRIPT
-
8/12/2019 Android UI Techniques
1/38
Android UI Development:Tips, Tricks, and Techniques
Romain GuyChet HaaseAndroid UI Toolkit Team
Google
-
8/12/2019 Android UI Techniques
2/38
Android UI Development:Tips, Tricks, and Techniques
Romain GuyChet HaaseAndroid UI Toolkit Team
Google
T o t a l l y T e r r i f
i c
-
8/12/2019 Android UI Techniques
3/38
Trash Talk
-
8/12/2019 Android UI Techniques
4/38
Trash Talkor Garbage Zero
Avoid creating garbage,when necessary and possible
-
8/12/2019 Android UI Techniques
5/38
Statics as Temporaries
Instead of a temporary object:
Consider a static instead:
public boolean pointInArea(int x, int y, Area area) {Point testPoint = new Point (x, y);return area.intersect(testPoint);
}
static final Point tmpPoint = new Point();
public boolean pointInArea(int x, int y, Area area) {tmpPoint.x = x;tmpPoint.y = y;return area.intersect(tmpPoint.yPoint);
}
5
-
8/12/2019 Android UI Techniques
6/38
AutoBoxing
Autoboxing creates Objectsfloat x = 5;Float y = x;doSomething(x);
void doSomething(Float z) {}
is equivalent to
float x = 5;Float y = new Float (x);doSomething( new Float (x));
void doSomething(Float z) {}
6
-
8/12/2019 Android UI Techniques
7/38
De-Autoboxing
Use primitive types whenever possible Avoids Object creation Use types you need for the situation
Avoids autoboxing back and forth
7
-
8/12/2019 Android UI Techniques
8/38
Obl iterator The enhanced for() loop is great ... but creates garbage
Consider a size check first:
for (Node node : nodeList) {}
Iterator iter = nodeList.iterator (); while (iter.hasNext()) {}
if (nodeList.size() > 0) {for (Node node : nodeList) {}
}
8
is equivalent to
-
8/12/2019 Android UI Techniques
9/38
Image is Everything Recycle those Bitmaps
Device resources are limited
Finalizers will clear them ... eventually You might think this would help
But you really want to do this
Dont wait for the finalizer to do the work if youneed that memory now
// done using this one, clear reference myBitmap = null;
// done using this one, recycle it
myBitmap.recycle();
9
-
8/12/2019 Android UI Techniques
10/38
Varargh Parameters to varargs method packaged into a
temporary array
void someMethod(float... args) {}
someMethod(5f);
someMethod(new float[]{5});
10
is equivalent to
-
8/12/2019 Android UI Techniques
11/38
Gener-ick T doesnt stand for primitive Type
Generics only deal with Objects; primitive typesget autoboxed
public class MyClass { T myVar; MyClass(T arg) { myVar = arg; }}
float f; MyClass myObject =
new MyClass(f);
11
which is equivalent to MyClass myObject =
new MyClass(new Float(f));
-
8/12/2019 Android UI Techniques
12/38
Tools: Allocation Tracking Limit allocations to find problems
Count the allocations being made
int prevLimt = -1;try { prevLimit =Debug.setAllocationLimit(0); // Do stuff
} finally { Debug.setAllocationLimit(-1);}
12
Debug.startAllocationCounting();// do stuffint allocCount = Debug.getThreadAllocCount();Debug.stopAllocationCounting);
-
8/12/2019 Android UI Techniques
13/38
Tools: DDMS Visual tool helps track allocations down to the
object/file/line number (demo)
13
-
8/12/2019 Android UI Techniques
14/38
Watch the Garbage...But Dont Be Silly
As Michael Abrash might have said:
Premature optimization is the Root of all evil
Minor garbage is irrelevant in most cases
But if you have GCs at critical points in yourapplication, consider Garbage Zero Example: animations
14
Vie w Roo t
-
8/12/2019 Android UI Techniques
15/38
Tools: hat DDMS Heap Analysis Tool is used to track down
memory leaks adb shell dumpsys meminfo
15
-
8/12/2019 Android UI Techniques
16/38
Memory leaks Be careful with Context Be careful with static fields Avoid non-static inner classes Use weak references
16
-
8/12/2019 Android UI Techniques
17/38
YOU and I
-
8/12/2019 Android UI Techniques
18/38
Responsiveness Single-threaded UI Dont block the UI thread
Also called main thread
AsyncTask Worker thread and UI thread messaging
Handler Messaging
18
-
8/12/2019 Android UI Techniques
19/38
Overinvalidating Only redraw what you must (demo)
19
-
8/12/2019 Android UI Techniques
20/38
Fewer is better Many views
Slower layout Slower drawing Slower startup time
Deep hierarchies Memory Slow...
StackOverflowException
20
-
8/12/2019 Android UI Techniques
21/38
HiearchyViewer
-
8/12/2019 Android UI Techniques
22/38
Layout optimizations Custom views Custom layouts ViewStub Compound drawables layoutopt
22
-
8/12/2019 Android UI Techniques
23/38
ViewStub
23
-
8/12/2019 Android UI Techniques
24/38
-
8/12/2019 Android UI Techniques
25/38
ViewStub
25
-
8/12/2019 Android UI Techniques
26/38
ViewStub
26
findViewById( R. id . stub_import) . setVisibility( View. VISIBLE );
// orView importPanel = (( ViewStub )
findViewById( R. id . stub_import)) . inflate();
-
8/12/2019 Android UI Techniques
27/38
27
-
8/12/2019 Android UI Techniques
28/38
28
C d d bl
-
8/12/2019 Android UI Techniques
29/38
Compound drawables
29
C d d bl
-
8/12/2019 Android UI Techniques
30/38
Compound drawables
30
-
8/12/2019 Android UI Techniques
31/38
layoutopt
Li tVi
-
8/12/2019 Android UI Techniques
32/38
ListView
32
1 public View getView( int position, View convertView, ViewGroup parent) {2 View item = mInflater . inflate( R. layout . list_item_icon_text, null );
3 (( TextView ) item . findViewById( R. id . text)) . setText( DATA[position]); 4 (( ImageView ) item . findViewById( R. id . icon)) . setImageBitmap(
5 (position & 1) == 1 ? mIcon1 : mIcon2);
6 return item;7 }
Li tVi
-
8/12/2019 Android UI Techniques
33/38
ListView
33
1 public View getView( int position, View convertView, ViewGroup parent) {2 if (convertView == null ) {3 convertView = mInflater . inflate( R. layout . item, parent, false );
4 }
5 (( TextView ) convertView . findViewById( R. id . text)) . setText( DATA[position]);6 (( ImageView ) convertView . findViewById( R. id . icon)) . setImageBitmap(7 (position & 1) == 1 ? mIcon1 : mIcon2);
8 return convertView;9 }
Li tVi
-
8/12/2019 Android UI Techniques
34/38
ListView
34
static class ViewHolder {
TextView text;ImageView icon;}
ListView
-
8/12/2019 Android UI Techniques
35/38
ListView
35
1 public View getView( int position, View convertView, ViewGroup parent) {
2 ViewHolder holder;3 4 if (convertView == null ) {5 convertView = mInflater . inflate( R. layout . list_item_icon_text,6 parent, false );7 holder = new ViewHolder ();8 holder . text = ( TextView ) convertView . findViewById( R. id . text);9 holder . icon = ( ImageView ) convertView . findViewById( R. id . icon);
10 11 convertView . setTag(holder);12 } else {13 holder = ( ViewHolder ) convertView . getTag();14 }15 16 holder . text . setText( DATA[position]);17 holder . icon . setImageBitmap((position & 1) ==? mIcon1 : mIcon2);18 19 return convertView;20 }
ListView
-
8/12/2019 Android UI Techniques
36/38
ListView
36
0
10.0
20.0
30.0
40.0
50.0
60.0
Dumb Correct Fast
List of 10,000 items on NexusOne, Android 2.2
Graphics optimizations
-
8/12/2019 Android UI Techniques
37/38
Graphics optimizations Pre-scale bitmaps Use compatible bitmaps
ARGB_8888 to draw on 32 bits window
Avoid blending Use View drawing caches
View.setDrawingCacheEnabled(true)
View.isOpaque()
37
For More Information
-
8/12/2019 Android UI Techniques
38/38
For More Information Android developer site
developer.android.com
Romain @romainguy
curious-creature.org Chet
@chethaase
graphics-geek.blogspot.com
38
top related