37
Jérémie Laval Happy Hacker Xamarin [email protected] Tips & Tricks to Spice Up Your Android App

Tips & Tricks to spice up your Android app

Embed Size (px)

DESCRIPTION

We will explore how Android (especially in recent versions) lets you develop awesomer apps and how you can tap into that potential with Xamarin.Android. Topics covered will include graphics, user interaction, best practices and animations.

Citation preview

Page 1: Tips & Tricks to spice up your Android app

Jérémie LavalHappy [email protected]

Tips & Tricks to Spice Up Your Android App

Page 2: Tips & Tricks to spice up your Android app

Spice-Up Tricks

Page 3: Tips & Tricks to spice up your Android app

ListView Tricks

Page 4: Tips & Tricks to spice up your Android app

Why ListView? Ubiquity.

Page 5: Tips & Tricks to spice up your Android app

Why ListView? Ubiquity.

Page 6: Tips & Tricks to spice up your Android app

Why ListView? Ubiquity.

Page 7: Tips & Tricks to spice up your Android app

Why ListView? Ubiquity.

Page 8: Tips & Tricks to spice up your Android app

From Basic ListView…

Boring

Page 9: Tips & Tricks to spice up your Android app

… to Complete App

Page 10: Tips & Tricks to spice up your Android app

Let’s Build This

Page 11: Tips & Tricks to spice up your Android app

Breaking Things Up

1. The Basics Loading and data fetching

2. The Pretty Improving the look and feel

3. The Gimmick Spicing up the result

Page 12: Tips & Tricks to spice up your Android app

The Basics

Page 13: Tips & Tricks to spice up your Android app

DEMO

Page 14: Tips & Tricks to spice up your Android app

ListFragment Customization

Page 15: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

ListFragment Customization<?xml  version="1.0"  encoding="utf-­‐8"?><LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical"        android:layout_width="fill_parent"        android:layout_height="fill_parent">        <LinearLayout                android:orientation="horizontal"                android:id="@id/android:empty">                <ProgressBar  />                <TextView  android:text="Loading..."  />        </LinearLayout>        <ListView                android:id="@id/android:list"                android:layout_weight="1"  /></LinearLayout

Page 16: Tips & Tricks to spice up your Android app

...

LulzDog

LulzCat

...

...

Warning! Async Loadingrequest

callback

Page 17: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

Async Loading

public  override  View  GetView  (int  position,  View  convertView,  ViewGroup  parent){   MyCustomView  view  =  EnsureView  (convertView);   var  versionNumber  =  Interlocked.Increment  (ref  view.VersionNumber);

  var  item  =  events  [position];   var  avatarView  =  view.FindViewById<ImageView>  (...);

  avatarView.SetImageDrawable  (EmptyAvatarDrawable);

  FetchAvatar  (view,  avatarView,  item,  versionNumber);

  return  view;}

Page 18: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

Async Loading (cont)var  imageCache  =  new  ConcurrentDictionary<string,  Task<Bitmap>>  ();void  FetchAvatar  (...  view,  ...  avatarView,  string  url,  long  versionNumber){   var  bmp  =  imageCache.GetOrAdd  (url,  u  =>  Task.Run  (()  =>  DownloadData  (u)));   if  (bmp.IsCompleted  &&  bmp.Result  !=  null)     avatarView.SetImage  (bmp.Result);   else     bmp.ContinueWith  (t  =>  {       if  (view.VersionNumber  ==  versionNumber  &&  t.Result  !=  null)         handler.Post  (()  =>  {           if  (view.VersionNumber  ==  versionNumber)             avatarView.SetImageAnimated  (t.Result);         });     });}

Page 19: Tips & Tricks to spice up your Android app

The Pretty

Page 20: Tips & Tricks to spice up your Android app

DEMO

Page 21: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

Prettify Your List Items <?xml  version="1.0"  encoding="utf-­‐8"?><shape  xmlns:android="http://schemas.android.com/apk/res/android"              android:shape="rectangle">        <corners  android:radius="3dp"  />        <gradient                  android:startColor="@android:color/transparent"                  android:endColor="#10000000"                  android:type="linear"                  android:centerColor="@android:color/transparent"                  android:centerY="0.8"                  android:angle="270"  />        <stroke                  android:width="1dp"                  android:color="#D0D0D0"  /></shape>

Page 22: Tips & Tricks to spice up your Android app

Prettify Your List Items

<FrameLayout  android:background="@drawable/list_item_box"  />

Page 23: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

Custom Drawablepublic  class  RoundCornersDrawable  :  Drawable{    public  RoundCornersDrawable  (Bitmap  bitmap,  float  cornerRadius  =  5)    {        this.cornerRadius  =  cornerRadius;        this.paint  =  new  Paint  ()  {  AntiAlias  =  true  };        var  tileMode  =  Shader.TileMode.Clamp;        paint.SetShader  (new  BitmapShader  (bitmap,  tileMode,  tileMode));    }

   public  override  void  Draw  (Canvas  canvas)    {        canvas.DrawRoundRect  (rect,  cornerRadius,  cornerRadius,  paint);    }}

Page 24: Tips & Tricks to spice up your Android app

Custom Drawable

imageView.SetImageDrawable  (new  RoundCornersDrawable  (glacierBmp,  cornerRadius:  10))

Page 25: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

15

16

Easy Inset Effects<TextView

       android:text="White-­‐based  inset"        android:shadowColor="@android:color/white"        android:shadowDx="1"        android:shadowDy="1"        android:shadowRadius="0.1"  />

<TextView        android:text="Black-­‐based  inset"        android:shadowColor="@android:color/black"        android:shadowDx="-­‐1"        android:shadowDy="-­‐1"        android:shadowRadius="0.1"  />

Page 26: Tips & Tricks to spice up your Android app

Easy Inset Effects

Page 27: Tips & Tricks to spice up your Android app

The Gimmick

Page 28: Tips & Tricks to spice up your Android app

The Animated Stuff

Page 29: Tips & Tricks to spice up your Android app

“The best animations are the ones your usersdon’t notice because they feel natural”

Page 30: Tips & Tricks to spice up your Android app

- Jérémie Laval

“The best animations are the ones your usersdon’t notice because they feel natural”

Page 31: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

Fade ImageViewpublic  static  class  ImageViewExtensions{    public  static  void  SetImageDrawableAnimated  (this  ImageView  view,                                                                                                  Drawable  drawable)    {        var  lng  =  view.Resources.GetInteger  (ConfigLongAnimTime);        var  med  =  view.Resources.GetInteger  (ConfigMediumAnimTime);

       view.Animate  ().Alpha  (0).SetDuration  (med).WithEndAction  (()  =>  {            view.SetImageDrawable  (drawable);            view.Animate  ().Alpha  (1).SetDuration  (lng);        });    }}

Page 32: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

ListView Item Animations

public  override  View  GetView  (int  position,  View  convertView,  ViewGroup  parent){   var  view  =  EnsureView  (convertView);   var  item  =  events  [position];

  if  (!item.Consumed)  {     item.Consumed  =  true;     var  animation  =  AnimationUtils.MakeInChildBottomAnimation  (context);     view.StartAnimation  (animation);   }

  return  view;}

Page 33: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

Automatic Layout Transitions<LinearLayout        android:orientation="vertical"        android:id="@+id/AnimatedLayout"        android:animateLayoutChanges="true"        android:minHeight="49dp"        android:layout_height="wrap_content">

       <LinearLayout  android:id="@+id/PresentationLayout"  />

       <LinearLayout  android:id="@+id/ActionLayout"                                    android:layout_height="1px"  />

</LinearLayout>

Page 34: Tips & Tricks to spice up your Android app

01

02

03

04

05

06

07

08

09

10

11

12

13

14

Automatic Layout Transitionsif  (presentationLayout.Visibility  ==  ViewStates.Gone)  {   presentationLayout.Visibility  =  ViewStates.Visible;   var  lp  =  new  LayoutParams  (actionLayout.LayoutParameters)  {     Height  =  1   };   actionLayout.LayoutParameters  =  lp;}  else  {   var  lp  =  new  LayoutParams  (actionLayout.LayoutParameters)  {     Height  =  ViewGroup.LayoutParams.WrapContent,     Gravity  =  GravityFlags.Center   };   actionLayout.LayoutParameters  =  lp;   presentationLayout.Visibility  =  ViewStates.Gone;}

Page 35: Tips & Tricks to spice up your Android app

Last Call

Page 36: Tips & Tricks to spice up your Android app

developers.android.com/designA worthy read for every app developer