我有一个带有 2 个按钮的布局,每个按钮都会激活动画。在按钮上单击布局,按钮消失,动画开始。除了一个问题外,它可以工作,因为内存问题,我无法在第一个动画之后激活第二个动画。播放其中一个动画后,我必须以某种方式释放内存,以便加载第二个动画。如何释放动画并删除所有帧?
这是我的代码:
public class BermadMain extends Activity {
public static final String TAG = "Bermad";
AnimationDrawable animation, animation2, animation3;
ImageView imageAnim;
RelativeLayout lay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lay = (RelativeLayout) findViewById(R.id.rallay);
imageAnim = (ImageView) findViewById(R.id.imageView1);
final Button refinery = (Button) findViewById(R.id.refinery);
refinery.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i=85;
animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.ref1), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref2), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref3), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref4), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref5), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref6), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref7), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref8), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref9), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref10), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref11), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref12), i);
animation.addFrame(getResources().getDrawable(R.drawable.ref13), i);
imageAnim.setBackgroundDrawable(animation);
imageAnim.post(new Refiney());
lay.setVisibility(View.GONE);
}
});
final Button oil = (Button) findViewById(R.id.oil);
oil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int i=85;
animation2 = new AnimationDrawable();
animation2.addFrame(getResources().getDrawable(R.drawable.oil1), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil2), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil3), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil4), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil5), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil6), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil7), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil8), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil9), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil10), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil11), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil12), i);
animation2.addFrame(getResources().getDrawable(R.drawable.oil13), i);
imageAnim.setBackgroundDrawable(animation2);
imageAnim.post(new Oil());
lay.setVisibility(View.GONE);
}
});
}
public void onBackPressed() {
lay.setVisibility(View.VISIBLE);
return;
}
class Refiney implements Runnable {
public void run() {
animation.start();
}
}
class Oil implements Runnable {
public void run() {
animation2.start();
}
}
}