Note that there are some explanatory texts on larger screens.

plurals
  1. POOptimizing performance when supporting multiple devices
    text
    copied!<p>I have an app that implements a runnable. Said runnable runs every 2.5 seconds. Inside of the runnable I made a switch that lays out the instructions for the runnable at different densities (the runnable moves an image across the screen, and for xhdpi devices they need to move a different parameters than mdpi devices, obviously). My question is, would it be more efficient to list different runnables for each resolution instead. Is the runnable going through each case for densities before running? It seems like that would eat up a lot of resource. Thanks for any insight. Some code is posted below:</p> <pre><code>final Handler handler = new Handler(); final Runnable r = new Runnable() { public void run() { RelativeLayout.LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params2 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); RelativeLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); DisplayMetrics metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); switch(metrics.densityDpi){ case DisplayMetrics.DENSITY_LOW: params.topMargin = (int)(Math.random()*704 + 1); params.leftMargin = (int)(Math.random()*1334 + 1); params2.topMargin = (int)(Math.random()*704 + 1); params2.leftMargin = (int)(Math.random()*1334 + 1); params3.topMargin = (int)(Math.random()*704 + 1); params3.leftMargin = (int)(Math.random()*1334 + 1); MapView.loadUrl("file:///android_asset/ldpimap.html"); break; case DisplayMetrics.DENSITY_MEDIUM: params.topMargin = (int)(Math.random()*1299 + 1); params.leftMargin = (int)(Math.random()*2419 + 1); params2.topMargin = (int)(Math.random()*1299 + 1); params2.leftMargin = (int)(Math.random()*2419 + 1); params3.topMargin = (int)(Math.random()*1299 + 1); params3.leftMargin = (int)(Math.random()*2419 + 1); MapView.loadUrl("file:///android_asset/mdpimap.html"); break; case DisplayMetrics.DENSITY_HIGH: params.topMargin = (int)(Math.random()*3039 + 1); params.leftMargin = (int)(Math.random()*5559 + 1); params2.topMargin = (int)(Math.random()*3039 + 1); params2.leftMargin = (int)(Math.random()*5559 + 1); params3.topMargin = (int)(Math.random()*3039 + 1); params3.leftMargin = (int)(Math.random()*5559 + 1); MapView.loadUrl("file:///android_asset/hdpimap.html"); break; case DisplayMetrics.DENSITY_XHIGH: params.topMargin = (int)(Math.random()*5489 + 1); params.leftMargin = (int)(Math.random()*9969 + 1); params2.topMargin = (int)(Math.random()*5489 + 1); params2.leftMargin = (int)(Math.random()*9969 + 1); params3.topMargin = (int)(Math.random()*5489 + 1); params3.leftMargin = (int)(Math.random()*9969 + 1); MapView.loadUrl("file:///android_asset/xhdpimap.html"); break; case DisplayMetrics.DENSITY_XXHIGH: params.topMargin = (int)(Math.random()*8649 + 1); params.leftMargin = (int)(Math.random()*14749 + 1); params2.topMargin = (int)(Math.random()*8649 + 1); params2.leftMargin = (int)(Math.random()*14749 + 1); params3.topMargin = (int)(Math.random()*8649 + 1); params3.leftMargin = (int)(Math.random()*14749 + 1); MapView.loadUrl("file:///android_asset/xxhdpimap.html"); } fImage.setLayoutParams(params); fImage2.setLayoutParams(params2); fImage3.setLayoutParams(params3); handler.postDelayed(this, 2350); } }; r.run(); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload