SlideShare a Scribd company logo
1 of 54
Download to read offline
Tuesday, May 25, 2010
Tuesday, May 25, 2010
Casting a Wide Net:
      Targeting All Android Devices
      Justin Mattson
      Developer Advocate
      19 May 2010




Tuesday, May 25, 2010
House keeping
          Use Wave to see live notes and ask questions



                         http://tinyurl.com/io10casting




      3


Tuesday, May 25, 2010
Agenda
          Resource Loading
          Image & Drawable Considerations
          Adapting to API Availability
          Testing




      4


Tuesday, May 25, 2010
Resource Loading
       • Based on current system configuration
       • If config changes, your app restarts (eg. orientation change)
       • Hierarchy of qualifiers




      5


Tuesday, May 25, 2010
Resource loading
      Screen size
       • Physical size of the screen
       • Buckets
           – small: <~3.0”
           – normal: ~3.0” - ~4.0”
           – large: >~4.0”




      6


Tuesday, May 25, 2010
Resource loading
      Screen orientation
       • landscape, portrait, or
         square
       • Might be triggered by
         accelerometers or other
         events




      7


Tuesday, May 25, 2010
Resource loading
      Screen density
       • Pixels per unit of measurement
       • System looks for “best” fit*
       • Buckets
           – ldpi: ~120dpi
           – mdpi: ~160dpi
           – hdpi: ~240dpi
       • *Matching: everyone is a
         winner
           – first match on 1.5




      8


Tuesday, May 25, 2010
Resource loading
      Screen density
       • Pixels per unit of measurement
       • System looks for “best” fit*
       • Buckets
           – ldpi: ~120dpi
           – mdpi: ~160dpi
           – hdpi: ~240dpi
       • *Matching: everyone is a
         winner
           – first match on 1.5




      8


Tuesday, May 25, 2010
Resource loading
      SDK Version
       • Styles across versions


       • Inexact matching, <= current
         version


       • Work around 1.5 dpi bug
           – Exact matching on 1.5 - 2.0
           – v5 thinks its v6




      9


Tuesday, May 25, 2010
Resource loading
      SDK Version
       • Styles across versions


       • Inexact matching, <= current
         version


       • Work around 1.5 dpi bug
           – Exact matching on 1.5 - 2.0
           – v5 thinks its v6




      9


Tuesday, May 25, 2010
Resource loading
      SDK Version
       • Styles across versions


       • Inexact matching, <= current
         version


       • Work around 1.5 dpi bug
           – Exact matching on 1.5 - 2.0
           – v5 thinks its v6




      9


Tuesday, May 25, 2010
Resource loading
      SDK Version
       • Styles across versions


       • Inexact matching, <= current
         version


       • Work around 1.5 dpi bug
           – Exact matching on 1.5 - 2.0
           – v5 thinks its v6




      9


Tuesday, May 25, 2010
Tuesday, May 25, 2010
Image resources



Tuesday, May 25, 2010
Image Resources
       • Goal is uniform physical sizing across screens
       • Best match allows for up or down sampling based on
         available resources
           – Single or few pixel features are most obviously impacted




      11


Tuesday, May 25, 2010
Image Resources
      The trouble with pixels




      12


Tuesday, May 25, 2010
Image Resources
      The trouble with pixels




      12


Tuesday, May 25, 2010
Image Resources
      The trouble with pixels




      12


Tuesday, May 25, 2010
Image Resources
      The trouble with pixels




      12


Tuesday, May 25, 2010
Image Resources
      The trouble with pixels




      12


Tuesday, May 25, 2010
Manipulating Image Loading
      • DO NOT DO THIS
      • OKAY, okay, if you must...
      • Only possible (and sensical) on 1.6 and above
      • Bitmap scaling controlled by density attributes of the object
      • Normally...
           BitmapFactory.Options default = new BitmapFactory.Options();
           default.inDensity = <density of loaded resource>
           default.inTargetDensity =
           getResources().getDisplayMetrics().densityDpi;

      • Pixels are scaled by inTargetDensity/inDensity
      • Manually set inDensity = inTargetDensity =
        DisplayMetrics.densityDpi

      13


Tuesday, May 25, 2010
Nine-Patches
       • Use to create image resources that can stretch controllably
       • One set of controls for stretching, another for the content
         area




      14


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      Stretching to fill the space




      15


Tuesday, May 25, 2010
Nine-Patches
      A little cushion




      16


Tuesday, May 25, 2010
Tuesday, May 25, 2010
Drawables



Tuesday, May 25, 2010
Shapes
       • Draw optimized views, regardless of screen
       • Primitives like square, oval, rectangle
       • Use these as the basis for simple graphics instead of
         bitmaps
       • Shape.draw() makes it easy to build a custom view




      18


Tuesday, May 25, 2010
Shapes
        public class LogoView extends View {
             private Shape mShape = new RectShape();
             private Drawable mLogo;
             public LogoView (Context context) {
                  super(context);
                  mLogo = getContext().getResources()
                        .getDrawable(R.id.logo);
             }
             protected void onDraw(Canvas canvas) {
                  mShape.resize(canvas.getWidth(), canvas.getHeight());
                  mShape.draw(canvas, mLogoBackgroundPaint);
                  mLogo.draw(canvas);
             }
        }
      19


Tuesday, May 25, 2010
ScaleDrawable
       • Adapt your drawables in a custom manner based on screen
       • Wraps another drawable
       • Allows for more exact control over scaling
       • Ideal for situations where it doesn’t make sense to preserve
         aspect ratio
           – Progress bars
           – Dividers
           – Any place where one dimension is more important than the
             other




      20


Tuesday, May 25, 2010
LevelListDrawable
       • Specified in XML
       • Combines a group of resources into a drawable
       • Use where the drawable is logically the same, but
         presentation varies based on state
           – status meters (eg. battery, signal)
           – weather condition
           – sports team
           – UI theme
       • For cases without a natural “level”, define constants making
         code more readable
       • Select the right resource for the right UI theme


      21


Tuesday, May 25, 2010
LevelListDrawable
           <?xml version="1.0" encoding="utf-8"?>
           <level-list xmlns:android="http://schemas.android.com/apk/
           res/android">
              <item android:maxLevel="0"
                        android:drawable="@drawable/rain_chance_zero"/>
              <item android:maxLevel="10"
                        android:drawable="@drawable/rain_chance_slight"/>
              <item android:maxLevel="40"
                        android:drawable="@drawable/rain_chance_good"/>
              <item android:maxLevel="70"
                        android:drawable="@drawable/rain_chance_high"/>
           </level-list>



      22


Tuesday, May 25, 2010
LevelListDrawable

           public class IO_Demo extends Activity {
               public void onResume() {
                   ((ImageView)findViewBy(R.id.weather))
                        .setImageLevel(getRainChance());
               }


               private int getRainChance() {
                   // call web service to get forecast
               }
           }




      23


Tuesday, May 25, 2010
LevelListDrawable




             Thanks to openclipart.org from for the Public Domain clipart used on this slide.
      24


Tuesday, May 25, 2010
LevelListDrawable




             Thanks to openclipart.org from for the Public Domain clipart used on this slide.
      24


Tuesday, May 25, 2010
LevelListDrawable




             Thanks to openclipart.org from for the Public Domain clipart used on this slide.
      24


Tuesday, May 25, 2010
Tuesday, May 25, 2010
Cross-Version Compatibility



Tuesday, May 25, 2010
API Changes: New Classes
       • Runtime handling of class load failures
       • Class dependencies resolved when class is first loaded
       • Use a second class with a static method to provoke class
         verification
       • Catch any verification error and adapt behavior
       • Avoids messy Class.forName(“”) pattern, but grows number
         of classes needed
           – group in a single package or as inner classes




      26


Tuesday, May 25, 2010
API Changes: New Classes
           public class Canary {
               private static NEW_CLASS foo;


               public static void tryNewClass() {
                   foo = new NEW_CLASS();
               }
           }




      27


Tuesday, May 25, 2010
API Changes: New Classes
           public class MyActivity extends Activity {
            private boolean canaryAvailable;
            private void checkApis() {
                try {
                    Canary.tryNewClass();
                    canaryAvailable = true;
                } catch (VerifyError e) {
                    canaryAvailable = false;
                    Log.w(LOG_TAG, “Canary unavailable, falling back.”);
                }
            }

      28


Tuesday, May 25, 2010
API Changes: New Methods
       • A bit trickier than dealing with classes
       • Determining which APIs are available
           – Use reflection to check availability
           – Make your code version aware
       • Working with availability
           – Call through using reflection (fewer classes)
           – Write a factory to return the right class (cleaner)




      29


Tuesday, May 25, 2010
API Changes: Mirror, mirror...
           public class Canary {
               private static Method getDensity;
               static {
                   getDensity = Canvas.class.getMethod(“getDensity”, null);
               } catch (NoSuchMethodException e) {
                   Log.w(LOG_TAG, “getDensity method not available.”);
               }
           }




      30


Tuesday, May 25, 2010
API Changes: Mirror, mirror...
           public class Canary {
              private static boolean hasDensity;
              private Canvas mDrawingSurface;
              public Integer getDensity() {
                   if (getDensity != null) {
                        return (Integer) getDensity.invoke(mDrawingSurface,
                            null);
                   } else {
                        return DEFAULT;
                   }
              }




      31


Tuesday, May 25, 2010
Tuesday, May 25, 2010
Testing



Tuesday, May 25, 2010
Testing, testing, testing
       • Nothing beats it
       • Verify the UI is behaving as expected
       • Check that any API compatibility code functions properly
       • Hardware testing is hopefully unnecessary, but...




      33


Tuesday, May 25, 2010
Questions?
           Use Wave to see live notes and ask questions



                          http://tinyurl.com/io10casting




      34


Tuesday, May 25, 2010

More Related Content

Similar to Android casting-wide-net-android-devices

Reveal's Advanced Analytics: Using R & Python
Reveal's Advanced Analytics: Using R & PythonReveal's Advanced Analytics: Using R & Python
Reveal's Advanced Analytics: Using R & PythonPoojitha B
 
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...InfiniteGraph
 
Testing iOS Apps
Testing iOS AppsTesting iOS Apps
Testing iOS AppsC4Media
 
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI Projects
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI ProjectsDiscovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI Projects
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI ProjectsWee Hyong Tok
 
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...South Tyrol Free Software Conference
 
Revisiting the Six Degrees Problem with a Graph Database - Nick Quinn
Revisiting the Six Degrees Problem with a Graph Database - Nick QuinnRevisiting the Six Degrees Problem with a Graph Database - Nick Quinn
Revisiting the Six Degrees Problem with a Graph Database - Nick Quinnjaxconf
 
Scaling with Postgres (Highload++ 2010)
Scaling with Postgres (Highload++ 2010)Scaling with Postgres (Highload++ 2010)
Scaling with Postgres (Highload++ 2010)Robert Treat
 
Lessons learnt at building recommendation services at industry scale
Lessons learnt at building recommendation services at industry scaleLessons learnt at building recommendation services at industry scale
Lessons learnt at building recommendation services at industry scaleDomonkos Tikk
 
Using the puppet debugger for lightweight exploration
Using the puppet debugger for lightweight explorationUsing the puppet debugger for lightweight exploration
Using the puppet debugger for lightweight explorationCorey Osman
 
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....Aidan Foster
 
Getting Intimate with Images on Android with James Halpern
Getting Intimate with Images on Android with James HalpernGetting Intimate with Images on Android with James Halpern
Getting Intimate with Images on Android with James HalpernFITC
 
When to use Node? Lessons learned
When to use Node? Lessons learnedWhen to use Node? Lessons learned
When to use Node? Lessons learnedbeatlevic
 
John Resig Beijing 2010 (English Version)
John Resig Beijing 2010 (English Version)John Resig Beijing 2010 (English Version)
John Resig Beijing 2010 (English Version)Jia Mi
 
[Sirius Day Eindhoven 2018] ASML's MDE Going Sirius
[Sirius Day Eindhoven 2018]  ASML's MDE Going Sirius[Sirius Day Eindhoven 2018]  ASML's MDE Going Sirius
[Sirius Day Eindhoven 2018] ASML's MDE Going SiriusObeo
 
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...Ivan Zoratti
 

Similar to Android casting-wide-net-android-devices (20)

Reveal's Advanced Analytics: Using R & Python
Reveal's Advanced Analytics: Using R & PythonReveal's Advanced Analytics: Using R & Python
Reveal's Advanced Analytics: Using R & Python
 
33rd degree
33rd degree33rd degree
33rd degree
 
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...
Everything Goes Better With Bacon: Revisiting the Six Degrees Problem with a ...
 
Testing iOS Apps
Testing iOS AppsTesting iOS Apps
Testing iOS Apps
 
JRubyConf 2009
JRubyConf 2009JRubyConf 2009
JRubyConf 2009
 
Mini training - Moving to xUnit.net
Mini training - Moving to xUnit.netMini training - Moving to xUnit.net
Mini training - Moving to xUnit.net
 
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI Projects
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI ProjectsDiscovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI Projects
Discovering Your AI Super Powers - Tips and Tricks to Jumpstart your AI Projects
 
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...
SFScon 21 - Matteo Camilli - Performance assessment of microservices with str...
 
Revisiting the Six Degrees Problem with a Graph Database - Nick Quinn
Revisiting the Six Degrees Problem with a Graph Database - Nick QuinnRevisiting the Six Degrees Problem with a Graph Database - Nick Quinn
Revisiting the Six Degrees Problem with a Graph Database - Nick Quinn
 
Berlin.JS Meetup
Berlin.JS MeetupBerlin.JS Meetup
Berlin.JS Meetup
 
Scaling with Postgres (Highload++ 2010)
Scaling with Postgres (Highload++ 2010)Scaling with Postgres (Highload++ 2010)
Scaling with Postgres (Highload++ 2010)
 
Lessons learnt at building recommendation services at industry scale
Lessons learnt at building recommendation services at industry scaleLessons learnt at building recommendation services at industry scale
Lessons learnt at building recommendation services at industry scale
 
Using the puppet debugger for lightweight exploration
Using the puppet debugger for lightweight explorationUsing the puppet debugger for lightweight exploration
Using the puppet debugger for lightweight exploration
 
GWT Plus HTML 5
GWT Plus HTML 5GWT Plus HTML 5
GWT Plus HTML 5
 
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....
A Responsive Design Case Study - What We Did Wrong Building ResponsiveDesign....
 
Getting Intimate with Images on Android with James Halpern
Getting Intimate with Images on Android with James HalpernGetting Intimate with Images on Android with James Halpern
Getting Intimate with Images on Android with James Halpern
 
When to use Node? Lessons learned
When to use Node? Lessons learnedWhen to use Node? Lessons learned
When to use Node? Lessons learned
 
John Resig Beijing 2010 (English Version)
John Resig Beijing 2010 (English Version)John Resig Beijing 2010 (English Version)
John Resig Beijing 2010 (English Version)
 
[Sirius Day Eindhoven 2018] ASML's MDE Going Sirius
[Sirius Day Eindhoven 2018]  ASML's MDE Going Sirius[Sirius Day Eindhoven 2018]  ASML's MDE Going Sirius
[Sirius Day Eindhoven 2018] ASML's MDE Going Sirius
 
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
Ora mysql bothGetting the best of both worlds with Oracle 11g and MySQL Enter...
 

More from Marakana Inc.

Android Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaAndroid Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaMarakana Inc.
 
Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven DevelopmentMarakana Inc.
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMarakana Inc.
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical DataMarakana Inc.
 
Deep Dive Into Android Security
Deep Dive Into Android SecurityDeep Dive Into Android Security
Deep Dive Into Android SecurityMarakana Inc.
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupMarakana Inc.
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesMarakana Inc.
 
2010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-62010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-6Marakana Inc.
 
Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6Marakana Inc.
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Marakana Inc.
 
What's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationWhat's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationMarakana Inc.
 
jQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzjQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzMarakana Inc.
 
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Marakana Inc.
 
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram ArnoldEfficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram ArnoldMarakana Inc.
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldMarakana Inc.
 
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboLearn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboMarakana Inc.
 
Replacing Java Incrementally
Replacing Java IncrementallyReplacing Java Incrementally
Replacing Java IncrementallyMarakana Inc.
 
Learn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrLearn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrMarakana Inc.
 

More from Marakana Inc. (20)

Android Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar GargentaAndroid Services Black Magic by Aleksandar Gargenta
Android Services Black Magic by Aleksandar Gargenta
 
JRuby at Square
JRuby at SquareJRuby at Square
JRuby at Square
 
Behavior Driven Development
Behavior Driven DevelopmentBehavior Driven Development
Behavior Driven Development
 
Martin Odersky: What's next for Scala
Martin Odersky: What's next for ScalaMartin Odersky: What's next for Scala
Martin Odersky: What's next for Scala
 
Why Java Needs Hierarchical Data
Why Java Needs Hierarchical DataWhy Java Needs Hierarchical Data
Why Java Needs Hierarchical Data
 
Deep Dive Into Android Security
Deep Dive Into Android SecurityDeep Dive Into Android Security
Deep Dive Into Android Security
 
Securing Android
Securing AndroidSecuring Android
Securing Android
 
Pictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User GroupPictures from "Learn about RenderScript" meetup at SF Android User Group
Pictures from "Learn about RenderScript" meetup at SF Android User Group
 
Android UI Tips, Tricks and Techniques
Android UI Tips, Tricks and TechniquesAndroid UI Tips, Tricks and Techniques
Android UI Tips, Tricks and Techniques
 
2010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-62010 07-18.wa.rails tdd-6
2010 07-18.wa.rails tdd-6
 
Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6Efficient Rails Test-Driven Development - Week 6
Efficient Rails Test-Driven Development - Week 6
 
Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)Graphicsand animations devoxx2010 (1)
Graphicsand animations devoxx2010 (1)
 
What's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovationWhat's this jQuery? Where it came from, and how it will drive innovation
What's this jQuery? Where it came from, and how it will drive innovation
 
jQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda KatzjQuery State of the Union - Yehuda Katz
jQuery State of the Union - Yehuda Katz
 
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
Pics from: "James Gosling on Apple, Apache, Google, Oracle and the Future of ...
 
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram ArnoldEfficient Rails Test Driven Development (class 4) by Wolfram Arnold
Efficient Rails Test Driven Development (class 4) by Wolfram Arnold
 
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram ArnoldEfficient Rails Test Driven Development (class 3) by Wolfram Arnold
Efficient Rails Test Driven Development (class 3) by Wolfram Arnold
 
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas EneboLearn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
Learn about JRuby Internals from one of the JRuby Lead Developers, Thomas Enebo
 
Replacing Java Incrementally
Replacing Java IncrementallyReplacing Java Incrementally
Replacing Java Incrementally
 
Learn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache BuildrLearn to Build like you Code with Apache Buildr
Learn to Build like you Code with Apache Buildr
 

Android casting-wide-net-android-devices

  • 3. Casting a Wide Net: Targeting All Android Devices Justin Mattson Developer Advocate 19 May 2010 Tuesday, May 25, 2010
  • 4. House keeping Use Wave to see live notes and ask questions http://tinyurl.com/io10casting 3 Tuesday, May 25, 2010
  • 5. Agenda Resource Loading Image & Drawable Considerations Adapting to API Availability Testing 4 Tuesday, May 25, 2010
  • 6. Resource Loading • Based on current system configuration • If config changes, your app restarts (eg. orientation change) • Hierarchy of qualifiers 5 Tuesday, May 25, 2010
  • 7. Resource loading Screen size • Physical size of the screen • Buckets – small: <~3.0” – normal: ~3.0” - ~4.0” – large: >~4.0” 6 Tuesday, May 25, 2010
  • 8. Resource loading Screen orientation • landscape, portrait, or square • Might be triggered by accelerometers or other events 7 Tuesday, May 25, 2010
  • 9. Resource loading Screen density • Pixels per unit of measurement • System looks for “best” fit* • Buckets – ldpi: ~120dpi – mdpi: ~160dpi – hdpi: ~240dpi • *Matching: everyone is a winner – first match on 1.5 8 Tuesday, May 25, 2010
  • 10. Resource loading Screen density • Pixels per unit of measurement • System looks for “best” fit* • Buckets – ldpi: ~120dpi – mdpi: ~160dpi – hdpi: ~240dpi • *Matching: everyone is a winner – first match on 1.5 8 Tuesday, May 25, 2010
  • 11. Resource loading SDK Version • Styles across versions • Inexact matching, <= current version • Work around 1.5 dpi bug – Exact matching on 1.5 - 2.0 – v5 thinks its v6 9 Tuesday, May 25, 2010
  • 12. Resource loading SDK Version • Styles across versions • Inexact matching, <= current version • Work around 1.5 dpi bug – Exact matching on 1.5 - 2.0 – v5 thinks its v6 9 Tuesday, May 25, 2010
  • 13. Resource loading SDK Version • Styles across versions • Inexact matching, <= current version • Work around 1.5 dpi bug – Exact matching on 1.5 - 2.0 – v5 thinks its v6 9 Tuesday, May 25, 2010
  • 14. Resource loading SDK Version • Styles across versions • Inexact matching, <= current version • Work around 1.5 dpi bug – Exact matching on 1.5 - 2.0 – v5 thinks its v6 9 Tuesday, May 25, 2010
  • 17. Image Resources • Goal is uniform physical sizing across screens • Best match allows for up or down sampling based on available resources – Single or few pixel features are most obviously impacted 11 Tuesday, May 25, 2010
  • 18. Image Resources The trouble with pixels 12 Tuesday, May 25, 2010
  • 19. Image Resources The trouble with pixels 12 Tuesday, May 25, 2010
  • 20. Image Resources The trouble with pixels 12 Tuesday, May 25, 2010
  • 21. Image Resources The trouble with pixels 12 Tuesday, May 25, 2010
  • 22. Image Resources The trouble with pixels 12 Tuesday, May 25, 2010
  • 23. Manipulating Image Loading • DO NOT DO THIS • OKAY, okay, if you must... • Only possible (and sensical) on 1.6 and above • Bitmap scaling controlled by density attributes of the object • Normally... BitmapFactory.Options default = new BitmapFactory.Options(); default.inDensity = <density of loaded resource> default.inTargetDensity = getResources().getDisplayMetrics().densityDpi; • Pixels are scaled by inTargetDensity/inDensity • Manually set inDensity = inTargetDensity = DisplayMetrics.densityDpi 13 Tuesday, May 25, 2010
  • 24. Nine-Patches • Use to create image resources that can stretch controllably • One set of controls for stretching, another for the content area 14 Tuesday, May 25, 2010
  • 25. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 26. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 27. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 28. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 29. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 30. Nine-Patches Stretching to fill the space 15 Tuesday, May 25, 2010
  • 31. Nine-Patches A little cushion 16 Tuesday, May 25, 2010
  • 34. Shapes • Draw optimized views, regardless of screen • Primitives like square, oval, rectangle • Use these as the basis for simple graphics instead of bitmaps • Shape.draw() makes it easy to build a custom view 18 Tuesday, May 25, 2010
  • 35. Shapes public class LogoView extends View { private Shape mShape = new RectShape(); private Drawable mLogo; public LogoView (Context context) { super(context); mLogo = getContext().getResources() .getDrawable(R.id.logo); } protected void onDraw(Canvas canvas) { mShape.resize(canvas.getWidth(), canvas.getHeight()); mShape.draw(canvas, mLogoBackgroundPaint); mLogo.draw(canvas); } } 19 Tuesday, May 25, 2010
  • 36. ScaleDrawable • Adapt your drawables in a custom manner based on screen • Wraps another drawable • Allows for more exact control over scaling • Ideal for situations where it doesn’t make sense to preserve aspect ratio – Progress bars – Dividers – Any place where one dimension is more important than the other 20 Tuesday, May 25, 2010
  • 37. LevelListDrawable • Specified in XML • Combines a group of resources into a drawable • Use where the drawable is logically the same, but presentation varies based on state – status meters (eg. battery, signal) – weather condition – sports team – UI theme • For cases without a natural “level”, define constants making code more readable • Select the right resource for the right UI theme 21 Tuesday, May 25, 2010
  • 38. LevelListDrawable <?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/ res/android"> <item android:maxLevel="0" android:drawable="@drawable/rain_chance_zero"/> <item android:maxLevel="10" android:drawable="@drawable/rain_chance_slight"/> <item android:maxLevel="40" android:drawable="@drawable/rain_chance_good"/> <item android:maxLevel="70" android:drawable="@drawable/rain_chance_high"/> </level-list> 22 Tuesday, May 25, 2010
  • 39. LevelListDrawable public class IO_Demo extends Activity { public void onResume() { ((ImageView)findViewBy(R.id.weather)) .setImageLevel(getRainChance()); } private int getRainChance() { // call web service to get forecast } } 23 Tuesday, May 25, 2010
  • 40. LevelListDrawable Thanks to openclipart.org from for the Public Domain clipart used on this slide. 24 Tuesday, May 25, 2010
  • 41. LevelListDrawable Thanks to openclipart.org from for the Public Domain clipart used on this slide. 24 Tuesday, May 25, 2010
  • 42. LevelListDrawable Thanks to openclipart.org from for the Public Domain clipart used on this slide. 24 Tuesday, May 25, 2010
  • 45. API Changes: New Classes • Runtime handling of class load failures • Class dependencies resolved when class is first loaded • Use a second class with a static method to provoke class verification • Catch any verification error and adapt behavior • Avoids messy Class.forName(“”) pattern, but grows number of classes needed – group in a single package or as inner classes 26 Tuesday, May 25, 2010
  • 46. API Changes: New Classes public class Canary { private static NEW_CLASS foo; public static void tryNewClass() { foo = new NEW_CLASS(); } } 27 Tuesday, May 25, 2010
  • 47. API Changes: New Classes public class MyActivity extends Activity { private boolean canaryAvailable; private void checkApis() { try { Canary.tryNewClass(); canaryAvailable = true; } catch (VerifyError e) { canaryAvailable = false; Log.w(LOG_TAG, “Canary unavailable, falling back.”); } } 28 Tuesday, May 25, 2010
  • 48. API Changes: New Methods • A bit trickier than dealing with classes • Determining which APIs are available – Use reflection to check availability – Make your code version aware • Working with availability – Call through using reflection (fewer classes) – Write a factory to return the right class (cleaner) 29 Tuesday, May 25, 2010
  • 49. API Changes: Mirror, mirror... public class Canary { private static Method getDensity; static { getDensity = Canvas.class.getMethod(“getDensity”, null); } catch (NoSuchMethodException e) { Log.w(LOG_TAG, “getDensity method not available.”); } } 30 Tuesday, May 25, 2010
  • 50. API Changes: Mirror, mirror... public class Canary { private static boolean hasDensity; private Canvas mDrawingSurface; public Integer getDensity() { if (getDensity != null) { return (Integer) getDensity.invoke(mDrawingSurface, null); } else { return DEFAULT; } } 31 Tuesday, May 25, 2010
  • 53. Testing, testing, testing • Nothing beats it • Verify the UI is behaving as expected • Check that any API compatibility code functions properly • Hardware testing is hopefully unnecessary, but... 33 Tuesday, May 25, 2010
  • 54. Questions? Use Wave to see live notes and ask questions http://tinyurl.com/io10casting 34 Tuesday, May 25, 2010