Kar*_*nan 11 size animation android dialog view
现在我注意到应用程序的安装大小非常奇怪.
当我使用从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);
}
Run Code Online (Sandbox Code Playgroud)
切换到创建的对话框后,设置包含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();
}
Run Code Online (Sandbox Code Playgroud)
它减少到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();
}
});
}
Run Code Online (Sandbox Code Playgroud)
当我尝试使用最后一种代码(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
Run Code Online (Sandbox Code Playgroud)
在onCreate方法中:
animation.setDuration(1000);
animation.setFillAfter(false);
Run Code Online (Sandbox Code Playgroud)
之后我把它称为onClickListener:
mapButton.startAnimation(animation);
Run Code Online (Sandbox Code Playgroud)
当我没有添加任何新资源但只更改了代码样式时,为什么应用程序大小会发生如此大的变化?我在初始化对话框中存在的小部件的方式中存在如此巨大的差异?当我按照添加动画的方式添加动画时,为什么会有巨大的差异?
跟进问题:
@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);
}
Run Code Online (Sandbox Code Playgroud)
思索
这可能是由于导入的包或类造成的吗?animation至少对问题有点意义.
添加视图初始化的上下文会减少内存使用吗?
是的,你的猜测接近是正确的,所以你可能知道你的所有Java代码首先被编译为Java字节码javac,然后dx将它们转换为Dalvik字节码,你的dalvik字节码文件(又名dex)被打包在你的apk(btw apk)大小不会随着zip压缩的帮助而变化很大)通常在classes.dex以后当您在设备中安装apk时,Android可能会针对该特定设备(aka odex)优化它.
因此,为了验证您的最终缓存dex实际更改并且负责此大小更改,只需添加动画,您可以通过解压缩apk或在设备内找到它,我总是发现第二个选项更令人兴奋:
所以这是你dex没有动画(你的80kb):

和你的应用信息:

所以这是你dex的动画(你的1 MB):

和你的应用信息:

仔细看看,你可以通过在dalvik字节码常量池中查看它们,它们真的是不同的:

由于某种原因,我没有得到如此激进的尺寸差异,但它是值得注意的,我使用gradle + Android Studio构建了你的应用程序,所以我认为它们可以帮助和优化一些东西.
最后,您可以使用ProGuard但是嘿!有些k现在并不是那么多,我不担心会给你的应用添加一些"胖",只要它变得更好,当然总有改进的余地但还记得你不是在java4k或东西像那样 :)