【发布时间】:2013-07-12 14:26:23
【问题描述】:
我最近为经常修改设备的 root 用户制作了这个 application。
现在我注意到应用程序的安装大小出现了非常奇怪的行为。
当我使用从 XML 文件初始化视图的常规活动时,大小为 715 kb。
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main_layout);
executeEvents = new ExecuteEvents(this);
display = (TextView) findViewById(R.id.display);
powermenu = (Button) findViewById(R.id.powermenu);
turnoffscreen = (Button) findViewById(R.id.turnscreenoff);
mapButton = (ImageButton) findViewById(R.id.mapButton);
SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
display.setText(getResources().getString(R.string.configured));
mapButton.setOnClickListener(this);
powermenu.setOnClickListener(this);
turnoffscreen.setOnClickListener(this);
}
在我切换到一个创建的对话框后,该对话框设置了一个包含 XML 文件中的小部件的视图。应用大小现在为200kb。
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
//setContentView(R.layout.main_layout);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main_layout, null);
executeEvents = new ExecuteEvents(this);
display = (TextView) view.findViewById(R.id.display);
powermenu = (Button) view.findViewById(R.id.powermenu);
turnoffscreen = (Button) view.findViewById(R.id.turnscreenoff);
mapButton = (ImageButton) view.findViewById(R.id.mapButton);
SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
display.setText(getResources().getString(R.string.configured));
mapButton.setOnClickListener(this);
powermenu.setOnClickListener(this);
turnoffscreen.setOnClickListener(this);
Dialog dialog = new Dialog(this);
dialog.setContentView(view);
dialog.setTitle(getResources().getString(R.string.app_name));
dialog.show();
}
当我使用这个时它减少到80kb:
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main_layout);
executeEvents = new ExecuteEvents(this);
display = (TextView) dialog.findViewById(R.id.display);
powermenu = (Button) dialog.findViewById(R.id.powermenu);
turnoffscreen = (Button) dialog.findViewById(R.id.turnscreenoff);
mapButton = (ImageButton) dialog.findViewById(R.id.mapButton);
SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
display.setText(getString(R.string.configured));
mapButton.setOnClickListener(this);
powermenu.setOnClickListener(this);
turnoffscreen.setOnClickListener(this);
dialog.setTitle(getString(R.string.app_name));
dialog.show();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
dialog.dismiss();
MainActivity.this.finish();
}
});
}
当我尝试使用最后一种代码样式(80kb)添加动画时,我还注意到应用程序大小的另一个奇怪变化。应用程序大小变得很大1 MB。这就是我初始化动画并尝试在单击按钮时调用它的方式:
private static final ScaleAnimation animation = new ScaleAnimation(1, .95f, 1, .95f, Animation.RELATIVE_TO_SELF, (float)0.5, Animation.RELATIVE_TO_SELF, (float)0.5);//declared as global variable
onCreate 方法内部:
animation.setDuration(1000);
animation.setFillAfter(false);
之后我在onClickListener中调用它:
mapButton.startAnimation(animation);
为什么在我没有添加任何新资源而只是更改代码样式的情况下,应用程序大小会发生如此剧烈的变化?我初始化对话框中出现的小部件的方式哪里有这么大的不同?为什么以我添加动画的方式添加动画会有很大的不同?
后续问题:
@Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.main_layout);
executeEvents = new ExecuteEvents(this);
display = (TextView) this.findViewById(R.id.display);
powermenu = (Button) this.findViewById(R.id.powermenu);
turnoffscreen = (Button) this.findViewById(R.id.turnscreenoff);
mapButton = (ImageButton) this.findViewById(R.id.mapButton);
SP = getSharedPreferences(PBConstants.Power_Button_SP, MODE_MULTI_PROCESS);
if(SP.contains(PBConstants.INPUT_DEVICE_TAG))
display.setText(getResources().getString(R.string.configured));
mapButton.setOnClickListener(this);
powermenu.setOnClickListener(this);
turnoffscreen.setOnClickListener(this);
}
推测
这可能是由于导入了包或类吗?至少对于问题的animation 位是有意义的。
添加视图初始化的上下文会减少内存使用吗?
【问题讨论】:
-
您可以解压 APK 并分析其组件。您还可以剖析 dex 文件 (code.google.com/p/smali) 以查看其结构。
-
APK 大小相同。安装尺寸不同。
-
哇,这很有趣。不过,在有根设备上,您可以分析与已安装应用程序相关的单个文件。比如
ls -l /data/app/{package}、ls -l /data/data/{package}。
标签: android animation view dialog size