SlideShare a Scribd company logo
1 of 32
Download to read offline
Android UI Tips & Tricks
Shem Magnezi
Making your good app great
Know your app
Understand
what you need
No magic/ generic
solutions
You know how to
write a good app
Looks good Feel slick
Not gonna talk
about viral/
design/
downloads etc...
Agenda
● Working with images
● Bring your views into life with animations
● Upgrade your lists views
Working with images
Cache Cache Cache
● Use cache for performance
● Be careful not using too much memory
Images in memory
Bitmap memory size:
Bitmap.getWidth() * Bitmap.getHeight() * Bitmap.config
Determine how much the bitmap gonna take:
Image.width * Image.height * Options.config / Options.sampleSize
Determine your cache size
● Approximate per-application memory: getSystemService
(Context.ACTIVITY_SERVICE).getMemoryClass()
● Pay attention to: onTrimMemory(int level) on your
Application
● Profile your memory
usage live
Loading the proper image type
For small image views work with thumbnails:
MediaStore.Images.Thumbnails.getThumbnail(
..., int kind, ...)
MINI_KIND: 512 x 384
MICRO_KIND: 96 x 96
Sample size
Original size is probably too big, so load smaller size.
Original
inSampleSize=2
memory/4
Determine the right sample size
● Get the original image size using: options.
inJustDecodeBounds = true;
● Get the view that gonna present the image
● Find the minimum sample_size so:
○ image.width / sample_size > view.width
○ image.height / sample_size > view.height
○ it also prefer that sample_size will be power of 2 for faster/
easier decoding
Find your view size
Sometimes your view size is 0 (cause is not yet drawn), so
you should wait until the system will draw it:
view.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
public void onGlobalLayout() {
//load image for the right size
}
});
}
Determine image size
CENTER_CROP
float scale = Math.max(viewW / imgW, viewH / imgH)
float scaledWidth = scaale * viewW;
float scaledHeight = scale * viewHt;
CENTER_INSIDE
float scale = Math.min(viewW / imgW, viewH / imgH)
float scaledWidth = scaale * viewW;
float scaledHeight = scale * viewHt;
Make your cache a bit smarter
get(Item image, Size size) {
cached = getImage(image);
if (cached != null) {
if (cached.size >= size) {
//saved time!
} else {
//maybe display a lower
//resolution until loading
//the full image
}
} else {
//photo not in cache, but we
//did our best
}
}
put(Item image, Size size) {
if (size < MICRO_KIND_SIZE) {
//load micro kind thumbnail
} else if (size < MINI_KIND_SIZE) {
//load mini kind thumbnail
} else {
//read the full image with the
//right sample size
}
}
Bitmap config
ARG_565 has no alpha channel
and is it in lower quality
But:
● It’s ~x2 faster to load
● It consume half of the
memory
● Most of the time you won’t
see any difference
source: Romain Guy
Animations
Interpolator
Sometimes you can use interpolator instead of
couple of sequenced animations.
For example, the built-in bounce animation on
android.
Between-activities animations
Set activity style:
<item name="android:windowBackground">@android:color/transparent</item>
When moving to this activity:
startActivity(intent);
overridePendingTransition(0, 0);
Do the animation:
ViewTreeObserver observer = animationImage.getViewTreeObserver();
observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
animationImage.getViewTreeObserver().removeOnPreDrawListener(this);
runEnterAnimation(back, startBounds);
}
});
Pre drawer listener?
Very useful for animations!
● Create enter animation to your activity
● Create animation to your view when it’s
added to the screen
● Animate list items when the list changes
Smart image animation
Case #1
Scaling animation for the image view:
ObjectAnimator.ofFloat(image_1, "scaleY", 1, 1.5
f);
<ImageView
android:id="@+id/image_1"
android:layout_width="225dp"
android:layout_height="150dp"
android:scaleType="centerCrop"
/>
When animating the
view there is no re-
layout and the image
not preserving it’s
scale type
Case #2
Use a frame and do a reverse scaling to the inner
image:
<RelativeLayout android:id="@+id/image_2"
android:layout_width="225dp"
android:layout_height="150dp">
<ImageView android:id="@+id/inner_image"
android:layout_width="225dp"
android:layout_height="300dp"
android:layout_marginTop="-75dp"
android:layout_marginBottom="-75dp"/>
</RelativeLayout>
anim.playTogether(
ObjectAnimator.ofFloat(image_2, "scaleY", 1, 1.5f),
ObjectAnimator.ofFloat(inner, "scaleY", 1f, 0.6666f));
● Lots of
calculations
● The animation is
not linear
● Need an extra
view
Case #3
Use an extra image as the target view:
anim.playTogether(
ObjectAnimator.ofFloat(image_3, "scaleY", 0.6666f, 1f),
ObjectAnimator.ofFloat(image_3, "alpha", 0, 1));
<ImageView
android:id="@+id/image_3"
android:layout_width="225dp"
android:layout_height=" 225dp"
android:scaleType="centerCrop"
/>
● You are losing
the original view
● The animation
isn’t smooth
Case #4
Implement you own Animation:
public class ExpandAnimation extends Animation {
protected void applyTransformation(float inp ...) {
...
if (inp < 1.0f) {
lp.height =(int)(start + (end - start)* inp);
mAnimatedView.requestLayout();
}
}
<ImageView android:id="@+id/image_4"
android:layout_width="225dp"
android:layout_height="150dp"
android:scaleType="centerCrop" />
Working with Lists
The basic things
● Reuse items
● ViewHolder pattern
● Long tasks should run in background with
AsyncTask
● Cancel view loading tasks using
RecyclerListener
● Use ListFragment
Profile your drawing
● Design your layout as flat as you can
● Avoid over drawing or nested weights
● Profile your list using GPU overdraw and
GPU Rendering in developer options
Empty view in your ListFragment
Use
android:id="@android:id/empty"
for the case the list is
empty
<ListView
android:id="@android:id/list"
… />
<RelativeLayout
android:id="@android:id/empty"
... />
Save each item state
In your adapter:
Set<Integer> opened = HashSet<Integer>();
On widget opened:
opened.add(item.getId());
In getView():
view.setOpened(opened.contains(item.getId());
Scrolled view inside ListView
You sometime want to put a view that can be scrolled by himself as one of
your listview items- for example putting a grid view of images.
For doing it you must let layout manager that this view must take it’s full size:
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightSpec);
getLayoutParams().height = getMeasuredHeight();
}
source: stackoverflow.com
Smart scrollbar for easy navigation
● Put a relative view that contains your list view and set
him as a OnScrollListener
● On onScroll calc the right position of your scroller
view using totalItemCount and
visibleItemCount
● On draw put your scroller view using
setTranslationY
Smart scrollbar for easy navigation
You can even add a behavior for dragging the scroller
using onTouchEvent:
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
if (//event in scroll bar view)
mDragging = true;
} else if (me.getAction() == MotionEvent.ACTION_UP) {
if (mDragging)
mDragging = false;
} else if (me.getAction() == MotionEvent.ACTION_MOVE) {
if (mDragging)
mList.setSelectionFromTop(//calc the right item index, 0);
}
}
smagnezi8@gmail.com
Thank you.

More Related Content

What's hot

Android UI Testing with Espresso
Android UI Testing with EspressoAndroid UI Testing with Espresso
Android UI Testing with EspressoGary Cheng
 
Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator Alessio Ricco
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycleAhsanul Karim
 
Fragments: Why, How, What For?
Fragments: Why, How, What For?Fragments: Why, How, What For?
Fragments: Why, How, What For?Brenda Cook
 
Android - Working with Fragments
Android - Working with FragmentsAndroid - Working with Fragments
Android - Working with FragmentsCan Elmas
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder BehindJohn Wilker
 
Getting started with Verold and Three.js
Getting started with Verold and Three.jsGetting started with Verold and Three.js
Getting started with Verold and Three.jsVerold
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views Lars Vogel
 
Getting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingGetting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingBitbar
 
Android development session 5 - Debug android studio
Android development   session 5 - Debug android studioAndroid development   session 5 - Debug android studio
Android development session 5 - Debug android studioFarabi Technology Middle East
 
Разработка приложений для Android Honeycomb: ActionBar & Fragments
Разработка приложений для Android Honeycomb: ActionBar & FragmentsРазработка приложений для Android Honeycomb: ActionBar & Fragments
Разработка приложений для Android Honeycomb: ActionBar & FragmentsAlexey Ustenko
 
[Individual presentation] android fragment
[Individual presentation] android fragment[Individual presentation] android fragment
[Individual presentation] android fragmentGabriele Vecchia
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in androidPrawesh Shrestha
 
XCUITest for iOS App Testing and how to test with Xcode
XCUITest for iOS App Testing and how to test with XcodeXCUITest for iOS App Testing and how to test with Xcode
XCUITest for iOS App Testing and how to test with XcodepCloudy
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 FragmentsDiego Grancini
 

What's hot (20)

Android UI Testing with Espresso
Android UI Testing with EspressoAndroid UI Testing with Espresso
Android UI Testing with Espresso
 
Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator Best Practices in apps development with Titanium Appcelerator
Best Practices in apps development with Titanium Appcelerator
 
Day 4: Activity lifecycle
Day 4: Activity lifecycleDay 4: Activity lifecycle
Day 4: Activity lifecycle
 
Fragments: Why, How, What For?
Fragments: Why, How, What For?Fragments: Why, How, What For?
Fragments: Why, How, What For?
 
Android - Working with Fragments
Android - Working with FragmentsAndroid - Working with Fragments
Android - Working with Fragments
 
Android development session 4 - Fragments
Android development   session 4 - FragmentsAndroid development   session 4 - Fragments
Android development session 4 - Fragments
 
Leaving Interface Builder Behind
Leaving Interface Builder BehindLeaving Interface Builder Behind
Leaving Interface Builder Behind
 
Getting started with Verold and Three.js
Getting started with Verold and Three.jsGetting started with Verold and Three.js
Getting started with Verold and Three.js
 
Day seven
Day sevenDay seven
Day seven
 
Android design and Custom views
Android design and Custom views Android design and Custom views
Android design and Custom views
 
Fragment
Fragment Fragment
Fragment
 
Getting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App TestingGetting Started with XCTest and XCUITest for iOS App Testing
Getting Started with XCTest and XCUITest for iOS App Testing
 
Android development session 5 - Debug android studio
Android development   session 5 - Debug android studioAndroid development   session 5 - Debug android studio
Android development session 5 - Debug android studio
 
Разработка приложений для Android Honeycomb: ActionBar & Fragments
Разработка приложений для Android Honeycomb: ActionBar & FragmentsРазработка приложений для Android Honeycomb: ActionBar & Fragments
Разработка приложений для Android Honeycomb: ActionBar & Fragments
 
[Individual presentation] android fragment
[Individual presentation] android fragment[Individual presentation] android fragment
[Individual presentation] android fragment
 
IOS Storyboards
IOS StoryboardsIOS Storyboards
IOS Storyboards
 
Introduction to fragments in android
Introduction to fragments in androidIntroduction to fragments in android
Introduction to fragments in android
 
XCUITest for iOS App Testing and how to test with Xcode
XCUITest for iOS App Testing and how to test with XcodeXCUITest for iOS App Testing and how to test with Xcode
XCUITest for iOS App Testing and how to test with Xcode
 
Android App Development - 06 Fragments
Android App Development - 06 FragmentsAndroid App Development - 06 Fragments
Android App Development - 06 Fragments
 
Android UI Fundamentals part 1
Android UI Fundamentals part 1Android UI Fundamentals part 1
Android UI Fundamentals part 1
 

Similar to Android ui tips & tricks

Android UI Tips & Tricks
Android UI Tips & TricksAndroid UI Tips & Tricks
Android UI Tips & TricksDroidConTLV
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimizationveeracynixit
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimizationveeracynixit
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimizationveeracynixit
 
Top Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on TabletsTop Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on TabletsMotorola Mobility - MOTODEV
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkJussi Pohjolainen
 
Improving Game Performance in the Browser
Improving Game Performance in the BrowserImproving Game Performance in the Browser
Improving Game Performance in the BrowserFITC
 
Getting Started With Material Design
Getting Started With Material DesignGetting Started With Material Design
Getting Started With Material DesignYasin Yildirim
 
What is image in Swift?/はるふ
What is image in Swift?/はるふWhat is image in Swift?/はるふ
What is image in Swift?/はるふha1f Yamaguchi
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limitsDroidcon Berlin
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientBin Shao
 
Android - Displaying images
Android - Displaying imagesAndroid - Displaying images
Android - Displaying imagesMatteo Bonifazi
 
10 things I've learned when working with html5 canvas
10 things I've learned when working with html5 canvas10 things I've learned when working with html5 canvas
10 things I've learned when working with html5 canvasFrançois Crevola
 
Android Development with Flash Builder Burrito
Android Development with Flash Builder BurritoAndroid Development with Flash Builder Burrito
Android Development with Flash Builder BurritoJeff Bollinger
 
From Zero to Hero – Web Performance
From Zero to Hero – Web PerformanceFrom Zero to Hero – Web Performance
From Zero to Hero – Web PerformanceSebastian Springer
 
Andromance - Android Performance
Andromance - Android PerformanceAndromance - Android Performance
Andromance - Android PerformanceOrhun Mert Simsek
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI晓东 杜
 
Printing photos-html-using-android
Printing photos-html-using-androidPrinting photos-html-using-android
Printing photos-html-using-androidKetan Raval
 

Similar to Android ui tips & tricks (20)

Android UI Tips & Tricks
Android UI Tips & TricksAndroid UI Tips & Tricks
Android UI Tips & Tricks
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimization
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimization
 
Android memory and performance optimization
Android memory and performance optimizationAndroid memory and performance optimization
Android memory and performance optimization
 
Top Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on TabletsTop Tips for Android UIs - Getting the Magic on Tablets
Top Tips for Android UIs - Getting the Magic on Tablets
 
Android 2D Drawing and Animation Framework
Android 2D Drawing and Animation FrameworkAndroid 2D Drawing and Animation Framework
Android 2D Drawing and Animation Framework
 
Improving Game Performance in the Browser
Improving Game Performance in the BrowserImproving Game Performance in the Browser
Improving Game Performance in the Browser
 
Getting Started With Material Design
Getting Started With Material DesignGetting Started With Material Design
Getting Started With Material Design
 
What is image in Swift?/はるふ
What is image in Swift?/はるふWhat is image in Swift?/はるふ
What is image in Swift?/はるふ
 
Android programming -_pushing_the_limits
Android programming -_pushing_the_limitsAndroid programming -_pushing_the_limits
Android programming -_pushing_the_limits
 
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficientTh 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
Th 0230 turbo_chargeyourui-howtomakeyourandroidu_ifastandefficient
 
Android - Displaying images
Android - Displaying imagesAndroid - Displaying images
Android - Displaying images
 
Work With Images
Work With ImagesWork With Images
Work With Images
 
10 things I've learned when working with html5 canvas
10 things I've learned when working with html5 canvas10 things I've learned when working with html5 canvas
10 things I've learned when working with html5 canvas
 
Multi Screen Hell
Multi Screen HellMulti Screen Hell
Multi Screen Hell
 
Android Development with Flash Builder Burrito
Android Development with Flash Builder BurritoAndroid Development with Flash Builder Burrito
Android Development with Flash Builder Burrito
 
From Zero to Hero – Web Performance
From Zero to Hero – Web PerformanceFrom Zero to Hero – Web Performance
From Zero to Hero – Web Performance
 
Andromance - Android Performance
Andromance - Android PerformanceAndromance - Android Performance
Andromance - Android Performance
 
performance optimization: UI
performance optimization: UIperformance optimization: UI
performance optimization: UI
 
Printing photos-html-using-android
Printing photos-html-using-androidPrinting photos-html-using-android
Printing photos-html-using-android
 

More from Shem Magnezi

The Future of Work(ers)
The Future of Work(ers)The Future of Work(ers)
The Future of Work(ers)Shem Magnezi
 
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...Shem Magnezi
 
Good rules for bad apps
Good rules for bad appsGood rules for bad apps
Good rules for bad appsShem Magnezi
 
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...Shem Magnezi
 
Iterating on your idea
Iterating on your ideaIterating on your idea
Iterating on your ideaShem Magnezi
 
The Redux State of the Art
The Redux State of the ArtThe Redux State of the Art
The Redux State of the ArtShem Magnezi
 
Startup hackers toolbox
Startup hackers toolboxStartup hackers toolbox
Startup hackers toolboxShem Magnezi
 
Fuck you startup world
Fuck you startup worldFuck you startup world
Fuck you startup worldShem Magnezi
 
The Future of Work
The Future of WorkThe Future of Work
The Future of WorkShem Magnezi
 
Android Developer Toolbox 2017
Android Developer Toolbox 2017Android Developer Toolbox 2017
Android Developer Toolbox 2017Shem Magnezi
 
Good rules for bad apps
Good rules for bad appsGood rules for bad apps
Good rules for bad appsShem Magnezi
 
Building android apps with kotlin
Building android apps with kotlinBuilding android apps with kotlin
Building android apps with kotlinShem Magnezi
 
Android dev toolbox
Android dev toolboxAndroid dev toolbox
Android dev toolboxShem Magnezi
 
Know what (not) to build
Know what (not) to buildKnow what (not) to build
Know what (not) to buildShem Magnezi
 

More from Shem Magnezi (14)

The Future of Work(ers)
The Future of Work(ers)The Future of Work(ers)
The Future of Work(ers)
 
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
 
Good rules for bad apps
Good rules for bad appsGood rules for bad apps
Good rules for bad apps
 
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
“Micro Frontends”- You Keep Using That Word, I Don’t Think It Means What You ...
 
Iterating on your idea
Iterating on your ideaIterating on your idea
Iterating on your idea
 
The Redux State of the Art
The Redux State of the ArtThe Redux State of the Art
The Redux State of the Art
 
Startup hackers toolbox
Startup hackers toolboxStartup hackers toolbox
Startup hackers toolbox
 
Fuck you startup world
Fuck you startup worldFuck you startup world
Fuck you startup world
 
The Future of Work
The Future of WorkThe Future of Work
The Future of Work
 
Android Developer Toolbox 2017
Android Developer Toolbox 2017Android Developer Toolbox 2017
Android Developer Toolbox 2017
 
Good rules for bad apps
Good rules for bad appsGood rules for bad apps
Good rules for bad apps
 
Building android apps with kotlin
Building android apps with kotlinBuilding android apps with kotlin
Building android apps with kotlin
 
Android dev toolbox
Android dev toolboxAndroid dev toolbox
Android dev toolbox
 
Know what (not) to build
Know what (not) to buildKnow what (not) to build
Know what (not) to build
 

Android ui tips & tricks

  • 1. Android UI Tips & Tricks Shem Magnezi
  • 2. Making your good app great Know your app Understand what you need No magic/ generic solutions You know how to write a good app Looks good Feel slick Not gonna talk about viral/ design/ downloads etc...
  • 3. Agenda ● Working with images ● Bring your views into life with animations ● Upgrade your lists views
  • 5. Cache Cache Cache ● Use cache for performance ● Be careful not using too much memory
  • 6. Images in memory Bitmap memory size: Bitmap.getWidth() * Bitmap.getHeight() * Bitmap.config Determine how much the bitmap gonna take: Image.width * Image.height * Options.config / Options.sampleSize
  • 7. Determine your cache size ● Approximate per-application memory: getSystemService (Context.ACTIVITY_SERVICE).getMemoryClass() ● Pay attention to: onTrimMemory(int level) on your Application ● Profile your memory usage live
  • 8. Loading the proper image type For small image views work with thumbnails: MediaStore.Images.Thumbnails.getThumbnail( ..., int kind, ...) MINI_KIND: 512 x 384 MICRO_KIND: 96 x 96
  • 9. Sample size Original size is probably too big, so load smaller size. Original inSampleSize=2 memory/4
  • 10. Determine the right sample size ● Get the original image size using: options. inJustDecodeBounds = true; ● Get the view that gonna present the image ● Find the minimum sample_size so: ○ image.width / sample_size > view.width ○ image.height / sample_size > view.height ○ it also prefer that sample_size will be power of 2 for faster/ easier decoding
  • 11. Find your view size Sometimes your view size is 0 (cause is not yet drawn), so you should wait until the system will draw it: view.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() { public void onGlobalLayout() { //load image for the right size } }); }
  • 12. Determine image size CENTER_CROP float scale = Math.max(viewW / imgW, viewH / imgH) float scaledWidth = scaale * viewW; float scaledHeight = scale * viewHt; CENTER_INSIDE float scale = Math.min(viewW / imgW, viewH / imgH) float scaledWidth = scaale * viewW; float scaledHeight = scale * viewHt;
  • 13. Make your cache a bit smarter get(Item image, Size size) { cached = getImage(image); if (cached != null) { if (cached.size >= size) { //saved time! } else { //maybe display a lower //resolution until loading //the full image } } else { //photo not in cache, but we //did our best } } put(Item image, Size size) { if (size < MICRO_KIND_SIZE) { //load micro kind thumbnail } else if (size < MINI_KIND_SIZE) { //load mini kind thumbnail } else { //read the full image with the //right sample size } }
  • 14. Bitmap config ARG_565 has no alpha channel and is it in lower quality But: ● It’s ~x2 faster to load ● It consume half of the memory ● Most of the time you won’t see any difference source: Romain Guy
  • 16. Interpolator Sometimes you can use interpolator instead of couple of sequenced animations. For example, the built-in bounce animation on android.
  • 17. Between-activities animations Set activity style: <item name="android:windowBackground">@android:color/transparent</item> When moving to this activity: startActivity(intent); overridePendingTransition(0, 0); Do the animation: ViewTreeObserver observer = animationImage.getViewTreeObserver(); observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { animationImage.getViewTreeObserver().removeOnPreDrawListener(this); runEnterAnimation(back, startBounds); } });
  • 18. Pre drawer listener? Very useful for animations! ● Create enter animation to your activity ● Create animation to your view when it’s added to the screen ● Animate list items when the list changes
  • 20. Case #1 Scaling animation for the image view: ObjectAnimator.ofFloat(image_1, "scaleY", 1, 1.5 f); <ImageView android:id="@+id/image_1" android:layout_width="225dp" android:layout_height="150dp" android:scaleType="centerCrop" /> When animating the view there is no re- layout and the image not preserving it’s scale type
  • 21. Case #2 Use a frame and do a reverse scaling to the inner image: <RelativeLayout android:id="@+id/image_2" android:layout_width="225dp" android:layout_height="150dp"> <ImageView android:id="@+id/inner_image" android:layout_width="225dp" android:layout_height="300dp" android:layout_marginTop="-75dp" android:layout_marginBottom="-75dp"/> </RelativeLayout> anim.playTogether( ObjectAnimator.ofFloat(image_2, "scaleY", 1, 1.5f), ObjectAnimator.ofFloat(inner, "scaleY", 1f, 0.6666f)); ● Lots of calculations ● The animation is not linear ● Need an extra view
  • 22. Case #3 Use an extra image as the target view: anim.playTogether( ObjectAnimator.ofFloat(image_3, "scaleY", 0.6666f, 1f), ObjectAnimator.ofFloat(image_3, "alpha", 0, 1)); <ImageView android:id="@+id/image_3" android:layout_width="225dp" android:layout_height=" 225dp" android:scaleType="centerCrop" /> ● You are losing the original view ● The animation isn’t smooth
  • 23. Case #4 Implement you own Animation: public class ExpandAnimation extends Animation { protected void applyTransformation(float inp ...) { ... if (inp < 1.0f) { lp.height =(int)(start + (end - start)* inp); mAnimatedView.requestLayout(); } } <ImageView android:id="@+id/image_4" android:layout_width="225dp" android:layout_height="150dp" android:scaleType="centerCrop" />
  • 25. The basic things ● Reuse items ● ViewHolder pattern ● Long tasks should run in background with AsyncTask ● Cancel view loading tasks using RecyclerListener ● Use ListFragment
  • 26. Profile your drawing ● Design your layout as flat as you can ● Avoid over drawing or nested weights ● Profile your list using GPU overdraw and GPU Rendering in developer options
  • 27. Empty view in your ListFragment Use android:id="@android:id/empty" for the case the list is empty <ListView android:id="@android:id/list" … /> <RelativeLayout android:id="@android:id/empty" ... />
  • 28. Save each item state In your adapter: Set<Integer> opened = HashSet<Integer>(); On widget opened: opened.add(item.getId()); In getView(): view.setOpened(opened.contains(item.getId());
  • 29. Scrolled view inside ListView You sometime want to put a view that can be scrolled by himself as one of your listview items- for example putting a grid view of images. For doing it you must let layout manager that this view must take it’s full size: @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int heightSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, heightSpec); getLayoutParams().height = getMeasuredHeight(); } source: stackoverflow.com
  • 30. Smart scrollbar for easy navigation ● Put a relative view that contains your list view and set him as a OnScrollListener ● On onScroll calc the right position of your scroller view using totalItemCount and visibleItemCount ● On draw put your scroller view using setTranslationY
  • 31. Smart scrollbar for easy navigation You can even add a behavior for dragging the scroller using onTouchEvent: public boolean onTouchEvent(MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { if (//event in scroll bar view) mDragging = true; } else if (me.getAction() == MotionEvent.ACTION_UP) { if (mDragging) mDragging = false; } else if (me.getAction() == MotionEvent.ACTION_MOVE) { if (mDragging) mList.setSelectionFromTop(//calc the right item index, 0); } }