简体   繁体   中英

Optimizing performance when supporting multiple devices

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:

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();

would it be more efficient to list different runnables for each resolution instead.

There's a negligible difference. In theory switch/if inside of a loop is slower. As the resolution never changes it costs maybe 1 cycle per iteration. The loop takes some thousand cycles at least, so forget it.

Is the runnable going through each case for densities before running?

No, why?

More important is clean code. You could replace the switch by using a table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM