User Tools

Site Tools


android_study

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
android_study [2020/03/09 16:24] jrsetiandroid_study [2020/03/10 01:58] (current) – [Background Location] jrseti
Line 17: Line 17:
 ====Background Location==== ====Background Location====
  
-[[https://developer.android.com/about/versions/oreo/background-location-limits|Background Lovation Limits]]+[[https://developer.android.com/about/versions/oreo/background-location-limits|Background Location Limits]]
  
 ====Saving Data==== ====Saving Data====
Line 324: Line 324:
   * it.next() takes some resource to get the next.   * it.next() takes some resource to get the next.
   * Adnroid platform team does not use iterators!!!   * Adnroid platform team does not use iterators!!!
 +  * Simple for loop is fastest! Iterators slowest. 
 +
 +LruCache Class
 +
 +  * LruCache c = new LruCache<String, Bitmap>(size_in_bytes);
 +  * Stores in a key/value relationship.
 +  * Bounded container - has a max size you define.
 +  * You can get the amount of memory available for your app and use some portion. Example:
 +
 +<code>
 +ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
 +int availBytes = am.getMemoryClass() * 2014 * 1024;
 +LruCache c = new LruCache<String, Bitmap>(availBytes/8);
 +</code>
 +
 +  * Tweak the size as you learn over time
 +  * get() and put()
 +
 +LINT
 +
 +  * Can spot and report potential errors and memory usage problems (churn)
 +  * In Android Studio Analyze->"Inspect Code" will run Lint
 +  * In File->Options you can select which issues to report
 +  * Set memory allocations issues to thow an error!
   *    * 
  
 +Hidden Cost of Transparency
  
 +  * Alpha blending costs
 +  * Video 46
  
 +Avoid Allocations in onDraw()
 +
 +  * onDraw() runs onthe UI thread
 +  * onDraw gets called 60 times a second
 +  * A high rate of allocations creates a lot of memory churn, GC eats up battery
 +  * MOVE allocations out of onDraw()
 +  * Make the objects static outside of onDraw()
 +
 +Strict Mode:
 +
 +  * Used to detect bad things done on the UI Thread
 +  * In developer options select Strict Mode
 +  * Whenever there is an issue, the border will flash red in your app
 +  * StrictMode Class can be used to specify detection criteria and what action to take when the condition is met
 +  * Best to StrictMode.noteSlowCall() before sychronization{}
 +  * No allocations on UI thread! StictMode helps identify
 +
 +Custom Views and Performance
 +
 +  * Don't call view.invalidate() if not necessary
 +  * Always pass a rectangle to invalidate!
 +  * Canvas.clipRect() limits call to 
 +  * Don't draw anything you dont have to
 +  * Don't use draw methods that are not hardware accelerated. Varies by Android version.
 +  * Don't make allocations in onDraw()!!!!
 +
 +Batching Work Until Later
 +
 +  * Batching helps battery power
 +  * Turning on things at irregular times wasted power to turn them on
 +  * Power state transitions are power hungry
 +  * AlarmManager can be in exact or inexact
 +  * Don't call AlarmManager.setExact()!
 +  * Use SyncAdapter in app. Sync adapters are designed specifically for syncing data between a device and the cloud; you should only use them for this type of task.
 +  * JobScheduler in API > 21, can set a lot of conditions, like when charging and on Wifi. 
 +  * In Android N (API level 24), the SyncManager sits on top of the JobScheduler. You should only use the SyncAdapter class if you require the additional functionality that it provides.
 +
 +Image Memory Usage
 +
 +  * PNG and JPG images use CPU memory and are transfered to GPU memory as textures.
 +  * Defaults to 32 bit pixels
 +  * There are differenct formats you can store then in, 565, 4444, etc.
 +<code>
 +bitmap.Options.setPreferredConfig(Bitmap.Config.RGB_565)
 +</code>
 +
 +Smaller PNG data
 +
 +  * Use Web-P format. 
 +  * Web-P is supported natively
 +  * Only helps with data transfer, still takes same size in memory
 +
 +Scaling Bitmaps
 +
 +  * Resize images programmatically to the size they will be on screen.
 +  * createScaledBitmap()
 +  * Can scale image when loading with bitmapOptions
 +  * Glide and Picasso libraries handle resizing
 +  * 
 +
 +Reusing Bitmaps
 +
 +  * You can re-use bitmap objects
 +  * Set the inBitmap option to a bitmap and the decode will reuse
 +  * mBitmapOptions.inBitmap = mCurrentBitmap;
 +  * Size must <= new bitmap size
 +  * Avoids memory fragmentation
 +  * Create ObjectPools, each has a size and RGB format in common.
 +  * Use Glide library to do this
 +
 +Perfomance Tuning Lifestyle
 +
 +  * Think about performance ahead of a time
 +  * Gather, Insight, Action
 +  * Use a tool to gather information.
 +
 +Overdraw
 +
 +  * Overdraw is how many times a pixel is drawn in a single frame
 +  * Turn on Show Overdraw in dev options to debug
 +  * Tints showing overdraws
 +  * Overlapping backgrounds
 +
 +Understanding VSYNC
 +
 +  * Refresh Rate
 +  * Frame Rate
 +  * If refresh rate > Frame Rate, you get tearing
 +  * Double buffering by the GPU 
 +  * VSYNC will copy from GPU to screen during VSYNC
 +
 +Profile GPU Rendering Tool
 +
 +  * In dev mode on device
 +  * Keep all bars < 16ms!
 +
 +GPU
 +
 +  * DisplayList submitted to the GPU
 +  * Turn on the GPU View in dev mode. Invalidates will show in red.
 +  * Use Hierarchy Viewer
 +
 +App Launch Time
 +
 +  * Don't display a splash screen
 +  * Fix the main activity so the load time is quick
 +
 +Smaller APKs
 +
 +  * Smaller resources
 +  * In build.gradle in 
 +  * 
 +<code>
 +BuildTypes{ 
 +  Release { 
 +    minifyEnabled: True
 +    shrinkResources: True
 +    proguardFiles getDefaultProguardFile('proguard-android.txt', 'proguard-tools.pro')
 +  }
 +}
 +</code>
 +
 +  * Toss out images you don't need for all resolutions.
 +  * Use "Vector Drawables"
 +  * Rotate images instead of separate images.
 +
 +  * Smaller code
 +  * minifiedEnabled: true
 +  * Optimize code like enums (which get inflated to a class)
 +  * JARs can be large. Proguard only helps a bit. Find smaller alternatives.
 +  * You can split APKs into different versions
 +  *  
 ====Tools to use==== ====Tools to use====
  
-  * Systrace+  * Systrace Trace.beginSection(), Trace.endSection()
   * Network Monitor Tool - in Android Studio   * Network Monitor Tool - in Android Studio
   * Network Attenuator from AT&T   * Network Attenuator from AT&T
Line 337: Line 496:
   * TraceView   * TraceView
   * Battery Historian   * Battery Historian
-  * +  * DDMS in Android Studio. Best of all  memory tools       
 +  * Heirarchy View 
 +  * On device tools: Profile GPU (Rendering), Show GPU (Overdraw), GPU View (Updates)      ` 
 +  * Heap Tool 
 +  * Memory Monitor 
 +  * Start Method Tracing Tool 
 +  * Method Profile Tool 
 +  * APK Analyzer 
 + 
 +Vector Drawable 
 + 
 +  * One file generates all resolutions on demand 
 +  * Can be animated with an XML file 
 +  * Can take more time to load into GPU 
 +  * Pay attention to the complexity of the paths in your vector data 
 + 
  
-NOTES:+====NOTES:====
  
 New Java 5 syntax:  New Java 5 syntax: 
android_study.1583771071.txt.gz · Last modified: 2020/03/09 16:24 by jrseti