我有一个实现可运行的应用程序。所述runnable每2.5秒运行一次。在 runnable 内部,我做了一个开关,为不同密度的 runnable 设置指令(runnable 在屏幕上移动图像,对于 xhdpi 设备,它们显然需要移动与 mdpi 设备不同的参数)。我的问题是,为每个分辨率列出不同的可运行文件会更有效吗?在运行之前,runnable 是否会通过每个案例的密度?看起来会消耗很多资源。感谢您的任何见解。一些代码贴在下面:
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();