나는 1 개의 부모 액티비티를 가진 복잡한 배틀 시스템을 가지고 있으며 배틀을 확장하고 해당 컨텍스트를 해당 클래스에 전달함으로써 BattleActivity의 정적 변수에 액세스하는 여러 하위 클래스를가집니다.안드로이드 - 할당 된 메모리 해제 AnimationDrawable 사용 중
이 모든 것이 정상적으로 작동하는 것 같습니다. 그러나 모든 AnimationDrawables를 메모리에서 해제하는 데 문제가 있습니다. 전투 중에 총 24 개의 DrawableAnimations가 사용될 수 있습니다. 이제는 괜찮습니다. 그러나 사용자가 새로운 괴물을 만날 때마다 4 개의 AnimationDrawables가 메모리에 추가됩니다. 메모리에 천천히 그러나 서두름으로 인해 내 앱이 부족 메모리 부족 예외로 인해 중단됩니다.
따라서 전투 시스템이 종료되는 즉시 모든 메모리를 해제 할 수있는 방법을 찾아야합니다. 현재 Sony Z2에서 테스트 중이며 사용자가 전투에 참가하면 mmmory가 91MB에서 230MB로 증가합니다. 전투가 끝나면이 메모리 사용량을 다시 91MB로 낮춰야합니다. 아주 기본적인 코드 스 니펫을 추가하여 현재 앱의 흐름과 메모리를 해제하기 위해 무엇을 하려는지 아이디어를 제공합니다.
public class Battle extends Activity
{
// I have several AnimationDrawables loaded into memory
// These are then assigned the relevent animation to a button swicthing between these animations throughout my battle
ImageButton btnChr1;
AnimationDrawable cAnimHit1;
}
//This is the use of one of those AnimationDrawables
public class Battle_Chr_Anim extends Battle
{
protected Context ctx;
private ImageButton btnChr1;
AnimationDrawable cAnimHit1;
public Battle_Chr_Anim(Context c, ImageButton _btnChr1, AnimationDrawable _cAnimHit1) {
this.ctx = c;
this.btnChr1 = _btnChr1;
this.cAnimHit1 = _cAnimHit1;
}
// Bound the ImageButton
int id = ctx.getResources().getIdentifier("idle", "drawable", ctx.getPackageName());
img_chr1.setBackgroundResource(id);
frameAnimation = (AnimationDrawable)img_chr1.getBackground();
frameAnimation.start()
// Loaded into memory ready so I can swicth them over quickly when user attacks
int ca1 = ctx.getResources().getIdentifier("attack", "drawable", ctx.getPackageName());
cAnimHit1 = (AnimationDrawable)chrHit1.getBackground();
cAnimHit1.start();
}
public class Battle_Ended extends Battle
{
protected Context ctx;
public Battle_Ended(Context c) {
this.ctx = c;
}
//This is a dialog popup when the user completes the battle closing the battle activty
void EndBattle()
{
ImageButton btnSubmit = (ImageButton)dialog.findViewById(R.id.imgBtnSubmit);
btnSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
RecyleAllAnimations();
dialog.dismiss();
((Activity) ctx).finish();
}
});
}
void RecyleAllAnimations()
{
// I want all AnimationDrawables from memory here, I believe the below line removes the one currently in use, however I have no way of releasing the other animations sitting in memory.
img_chr1.setBackgroundResource(android.R.color.transparent);
System.gc();
}
}
응용 프로그램 컨텍스트를 전달하는 정적 변수가 실제로 있습니까? 나는 당신이 당신의 드로어보다 훨씬 빨리 앱을 죽일 것이라고 생각한다. – TheRedFox
네, 이것은 내 Activity의 모든 코드를 원하지 않고 단순히이 클래스를 확장하여 여전히 UI.사용자가 전투에 들어갈 때 애니메이션이 유창 할 수 있도록 Activity를로드하기 전에 모든 AnimationDrawables를로드 할 수 있어야합니다. –