我需要通过Animation Drawable显示近1000张图像。 我在Windows焦点更改方法中启动了一个动画。当我按下一个按钮时,将显示下一个图像动画(仅使用一个活动)。它显示下一个动画8到10次但在此之后崩溃给出错误06-28 06:03:43.312:E / AndroidRuntime(12468):java.lang.OutOfMemoryError:位图大小超过VM预算。
我已经提到了这个Animation Drawable causing OutOfMemoryError on second run in Android,但我不知道如何整合这个,我也试过这个Best Way to Animate images frame by frame in Android。我也经历了这个http://developer.android.com/training/displaying-bitmaps/index.html,但我仍被卡住了...... 请帮帮我......
package com.example.quidest;
import java.util.Random;
import android.app.Activity;
import android.graphics.Typeface;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class GamePlay extends Activity {
String clue_apple= "Keeps the \nDoctor away…";
String clue_banana = "A Monkey's \n Favorite Food…";
String clue_bottle = "Used to \n store Water…";
String clue_beachball = "Make it bounce…";
String clue_boy = "Not yet \n a Man…";
Typeface comic;
Typeface brush;
//******Text View******************
TextView t_imageno;
TextView t_noOfAttempts;
TextView t_itemname;
TextView t_clue;
//*******Edit Text*****************
EditText e_attentEdit;
EditText e_score;
EditText e_entry;
//*******String For Edit Text*******
String sEdit_text_string;
//*******Relative Layout Slider******
RelativeLayout i_points;
RelativeLayout i_cluesslider;
//*******Image View Id's*************
ImageView i_submit;
ImageView i_pass;
ImageView i_itemApple;
ImageView i_banana10;
ImageView i_beach_ball10;
ImageView i_bottle10;
ImageView i_boy;
ImageView i_quidEst_Toast;
ImageView i_error;
//********Animation Variable**********
Animation a_move_Rto_lP;
Animation a_move_L_to_RP;
Animation a_move_Rto_lC;
Animation a_move_L_to_RC;
Animation a_zoomin_out_QuidEst;
Animation a_zoomin_error;
//*********Counter Variable***********
int c_imageno = 1;
int c_attempt=0;
int c_clue=0;
int score = 0;
int c_error=0;
int c_apple=0;
int c_banana=0;
int c_bottle=0;
int c_boy=0;
int c_ball=0;
AnimationDrawable appleanimation10;
AnimationDrawable bananaanimation10;
AnimationDrawable beachanimation10;
AnimationDrawable bollte_last;
AnimationDrawable boy_last;
Random rand = new Random();
Random rand_oncreate = new Random();
RandomImage randomImage = new RandomImage();
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.gameplay);
i_itemApple = (ImageView)findViewById(R.id.i_imageitem);
i_banana10 = (ImageView)findViewById(R.id.i_imageitem1);
i_beach_ball10 = (ImageView)findViewById(R.id.i_imageitem2);
i_bottle10 = (ImageView)findViewById(R.id.i_imageitem3);
t_itemname = (TextView)findViewById(R.id.t_itemname);
i_quidEst_Toast = (ImageView)findViewById(R.id.i_quid_est_toast);
i_error = (ImageView)findViewById(R.id.i_error);
i_boy = (ImageView)findViewById(R.id.i_imageboy);
int c_ranc_create = 5;
switch (rand_oncreate.nextInt(c_ranc_create)) {
case 0:
i_itemApple.setBackgroundResource(R.drawable.apple_appear10);
appleanimation10 = (AnimationDrawable) i_itemApple.getBackground();
i_itemApple.setVisibility(View.VISIBLE);
c_apple++;
break;
case 1:
i_beach_ball10.setBackgroundResource(R.drawable.beachball_appear10);
beachanimation10 = (AnimationDrawable) i_beach_ball10.getBackground();
i_beach_ball10.setVisibility(View.VISIBLE);
c_ball++;
break;
case 2:
i_banana10.setBackgroundResource(R.drawable.banana_appear10);
bananaanimation10 = (AnimationDrawable) i_banana10.getBackground();
i_banana10.setVisibility(View.VISIBLE);
c_banana++;
break;
case 3:
i_bottle10.setBackgroundResource(R.drawable.bottle_last);
bollte_last = (AnimationDrawable) i_bottle10.getBackground();
i_bottle10.setVisibility(View.VISIBLE);
c_bottle++;
break;
case 4:
i_boy.setBackgroundResource(R.drawable.boy_last);
boy_last = (AnimationDrawable) i_boy.getBackground();
i_boy.setVisibility(View.VISIBLE);
c_boy++;
break;
default:
break;
}
// i_boy.setVisibility(View.VISIBLE);
/* i_itemApple.setBackgroundResource(R.drawable.apple_appear10);
i_banana10.setBackgroundResource(R.drawable.banana_appear10);
i_beach_ball10.setBackgroundResource(R.drawable.beachball_appear10);
i_bottle10.setBackgroundResource(R.drawable.bottle_last);
i_boy.setBackgroundResource(R.drawable.boy_last);*/
/*appleanimation10 = (AnimationDrawable) i_itemApple.getBackground();
bananaanimation10 = (AnimationDrawable) i_banana10.getBackground();
beachanimation10 = (AnimationDrawable) i_beach_ball10.getBackground();
bollte_last = (AnimationDrawable) i_bottle10.getBackground();
boy_last = (AnimationDrawable) i_boy.getBackground();*/
//**********************Font Files************************************************
comic = Typeface.createFromAsset(getAssets(), "fonts/COMIC.TTF");
brush = Typeface.createFromAsset(getAssets(), "fonts/Brushy_Cre.ttf");
//**********************Image View IDs*******************************************
t_imageno = (TextView)findViewById(R.id.t_imageno);
e_attentEdit = (EditText)findViewById(R.id.e_attempt);
e_score = (EditText)findViewById(R.id.e_score);
i_points = (RelativeLayout)findViewById(R.id.Pointslider);
i_cluesslider = (RelativeLayout)findViewById(R.id.Clueslider1);
i_boy = (ImageView)findViewById(R.id.i_imageboy);
i_submit = (ImageView)findViewById(R.id.i_submit);
i_pass = (ImageView)findViewById(R.id.i_pass);
e_entry = (EditText)findViewById(R.id.e_entry);
t_noOfAttempts = (TextView)findViewById(R.id.t_noOfAttempt);
t_clue = (TextView)findViewById(R.id.t_clue);
t_imageno.setText(getString(R.string.Imageno) + c_imageno);
//*********************Button On Click********************************************
i_submit.setOnClickListener(onclick);
i_pass.setOnClickListener(onclick);
i_cluesslider.setOnClickListener(onclick);
i_points.setOnClickListener(onclick);
i_error.setOnClickListener(onclick);
//*********************Animation from Right to left and left to right*************
a_move_Rto_lP = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_r_tol);
a_move_L_to_RP = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_l_tor);
a_move_Rto_lC = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_r_tol);
a_move_L_to_RC = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.move_l_tor);
a_zoomin_out_QuidEst = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zinoutquidest);
a_zoomin_error = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zinoutquidest);
a_zoomin_out_QuidEst.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
i_quidEst_Toast.setVisibility(View.GONE);
}
});
//********************Setting Typeface*******************************************
e_score.setTypeface(brush);
t_imageno.setTypeface(brush);
e_attentEdit.setTypeface(comic);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
// int c_r_Images=4;
;
if(hasFocus){
if(c_apple ==1){
// i_itemApple.setVisibility(View.VISIBLE);
appleanimation10.start();
i_banana10.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_bottle10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
}
else if(c_ball ==1){
beachanimation10.start();
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
i_bottle10.setVisibility(View.GONE);
}
else if(c_banana ==1){
bananaanimation10.start();
i_itemApple.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
i_bottle10.setVisibility(View.GONE);
}
else if(c_boy ==1){
boy_last.start();
// i_boy.setVisibility(View.VISIBLE);
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_bottle10.setVisibility(View.GONE);
}
else if(c_bottle ==1){
bollte_last.start();
// i_bottle10.setVisibility(View.VISIBLE);
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
}
}
}
private OnClickListener onclick = new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.i_error:
i_error.setVisibility(View.GONE);
break;
case R.id.i_submit:
c_error++;
sEdit_text_string = e_entry.getText().toString();
if(sEdit_text_string.equals("")){
i_error.setVisibility(View.VISIBLE);
}
else if(sEdit_text_string.equals("aaple")){
score = score+60;
e_score.setText(getString(R.string.score)+score);
}
break;
case R.id.i_pass:
c_imageno++;
t_imageno.setText(getString(R.string.Imageno)+c_imageno);
appearNextImage();
break;
case R.id.Pointslider:
c_attempt++;
if(c_attempt ==1){
i_points.startAnimation(a_move_Rto_lP);
t_noOfAttempts.setText(getString(R.string.NoOfAttempts));
}
else if (c_attempt ==2){
i_points.startAnimation(a_move_L_to_RP);
c_attempt =0;
}
break;
case R.id.Clueslider1:
c_clue++;
if(c_clue ==1){
i_cluesslider.startAnimation(a_move_Rto_lC);
}
else if (c_clue ==2){
i_cluesslider.startAnimation(a_move_L_to_RC);
c_clue =0;
}
break;
}
}
private void appearNextImage() {
// TODO Auto-generated method stub
int c_r_NextImages=5;
switch(rand.nextInt(c_r_NextImages)){
case 0:
t_clue.setText(clue_apple);
i_itemApple.setBackgroundResource(R.drawable.apple_appear10);
appleanimation10 = (AnimationDrawable) i_itemApple.getBackground();
appleanimation10.start();
i_quidEst_Toast.setVisibility(View.VISIBLE);
i_bottle10.setVisibility(View.GONE);
i_quidEst_Toast.startAnimation(a_zoomin_out_QuidEst);
i_itemApple.setVisibility(View.VISIBLE);
i_banana10.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
break;
case 1:
t_clue.setText(clue_banana);
i_banana10.setBackgroundResource(R.drawable.banana_appear10);
bananaanimation10 = (AnimationDrawable) i_banana10.getBackground();
bananaanimation10.start();
i_quidEst_Toast.setVisibility(View.VISIBLE);
i_quidEst_Toast.startAnimation(a_zoomin_out_QuidEst);
i_banana10.setVisibility(View.VISIBLE);
i_bottle10.setVisibility(View.GONE);
i_itemApple.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
break;
case 2:
t_clue.setText(clue_bottle);
i_bottle10.setBackgroundResource(R.drawable.bottle_last);
bollte_last = (AnimationDrawable) i_bottle10.getBackground();
bollte_last.start();
i_quidEst_Toast.setVisibility(View.VISIBLE);
i_quidEst_Toast.startAnimation(a_zoomin_out_QuidEst);
i_bottle10.setVisibility(View.VISIBLE);
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
i_beach_ball10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
break;
case 3:
t_clue.setText(clue_beachball);
i_beach_ball10.setBackgroundResource(R.drawable.beachball_appear10);
beachanimation10 = (AnimationDrawable) i_beach_ball10.getBackground();
beachanimation10.start();
i_beach_ball10.setVisibility(View.VISIBLE);
i_bottle10.setVisibility(View.GONE);
i_boy.setVisibility(View.GONE);
i_quidEst_Toast.setVisibility(View.VISIBLE);
i_quidEst_Toast.startAnimation(a_zoomin_out_QuidEst);
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
break;
case 4:
t_clue.setText(clue_boy);
i_boy.setBackgroundResource(R.drawable.boy_last);
boy_last = (AnimationDrawable) i_boy.getBackground();
boy_last.start();
i_beach_ball10.setVisibility(View.GONE);
i_bottle10.setVisibility(View.GONE);
i_boy.setVisibility(View.VISIBLE);
i_quidEst_Toast.setVisibility(View.VISIBLE);
i_quidEst_Toast.startAnimation(a_zoomin_out_QuidEst);
i_itemApple.setVisibility(View.GONE);
i_banana10.setVisibility(View.GONE);
break;
default:
break;
}
}
};
}