Android动画drwable代码在2.2中正常工作,但在2.3中不工作?
问题描述:
我是android新手。Android动画drwable代码在2.2中正常工作,但在2.3中不工作?
我的动画代码在Android 2.2的工作正常,但在Android 2.3的不工作,当我运行的是Android 2.3的显示错误,如下面的动画代码:
java.lang.RuntimeException: Canvas: trying to use a recycled bitmap
[email protected]
在我的活动我有三个按钮和一个图像视图中,我点击图像视图一个动画正在播放,但当我强硬的ball_but按钮第一次它会播放一些动画到现在它工作正常,但是当我再次点击ball_but但它强制关闭错误像这样在日志猫。
我对此没有任何想法。
我的动画XML文件:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" >
<item
android:drawable="@drawable/cap_animation01"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation02"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation03"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation04"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation05"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation01"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation02"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation03"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation04"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation05"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation01"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation02"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation03"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation04"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation05"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation01"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation02"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation03"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation04"
android:duration="100"/>
<item
android:drawable="@drawable/cap_animation05"
android:duration="100"/>
<item
android:drawable="@drawable/elephant_first"
android:duration="100"/>
</animation-list>
我的活动:
public class Home extends Activity implements OnClickListener {
Button ball_but,drum_but,hat_but,freeapps_but,moreapps_but,start_but;
MediaPlayer mp;
int flag = 0;
ImageView elepahant_img;
private static Boolean isSoundPlaying=false;
static SelectTalkTask mytask;
AnimationDrawable drawable=null;
AnimationDrawable drawable_one=null;
AnimationDrawable drawable_two=null;
public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.talking_elephant_home);
elepahant_img=(ImageView)findViewById(R.id.elephant_img);
elepahant_img.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
isSoundPlaying = true;
release(drawable);
AnimationDrawable drawable = (AnimationDrawable) getResources().getDrawable(R.layout.water);
ImageView im = (ImageView) findViewById(R.id.elephant_img);
im.setBackgroundDrawable(drawable);
im.post(drawable);
play_water(); ///play the water
checkIfAnimationDone(drawable);
}
private void play_water() {
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Home.this,R.raw.water);
mp.start();
}
});
drum_but=(Button)findViewById(R.id.drum_but);
drum_but.setOnClickListener(this);
hat_but=(Button)findViewById(R.id.cap_but);
hat_but.setOnClickListener(this);
ball_but=(Button)findViewById(R.id.ball_but);
ball_but.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ball_but:
release(drawable);
Log.i("TAG", "this is ball_but"+isSoundPlaying);
drawable = (AnimationDrawable) getResources().getDrawable(R.layout.elephant);
elepahant_img = (ImageView) findViewById(R.id.elephant_img);
elepahant_img.setBackgroundDrawable(drawable);
elepahant_img.post(drawable);
play();
checkIfAnimationDone(drawable);
break;
case R.id.drum_but:
isSoundPlaying = true;
release(drawable);
Log.i("TAG", "this is cap_but"+isSoundPlaying);
drawable = (AnimationDrawable) getResources().getDrawable(R.layout.drum);
elepahant_img = (ImageView) findViewById(R.id.elephant_img);
elepahant_img.setBackgroundDrawable(drawable);
elepahant_img.post(drawable);
play_one();
checkIfAnimationDone(drawable);
break;
case R.id.cap_but:
isSoundPlaying = true;
release(drawable);
Log.i("TAG", "this is cap_but"+isSoundPlaying);
drawable = (AnimationDrawable) getResources().getDrawable(R.layout.cap);
elepahant_img = (ImageView) findViewById(R.id.elephant_img);
elepahant_img.setBackgroundDrawable(drawable);
elepahant_img.post(drawable);
play_two();
checkIfAnimationDone(drawable);
break;
}
}
protected void play() {
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Home.this,R.raw.ball);
mp.start();
}
private void play_two() {
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Home.this,R.raw.cap);
mp.start();
}
private void play_one() {
if(mp!=null){
mp.reset();
mp.release();
}
mp = MediaPlayer.create(Home.this,R.raw.drums);
mp.start();
}
protected void onPause() {
super.onPause();
try{
mp.stop();
}catch (NullPointerException e) {
e.printStackTrace();
}
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
this.finish();
System.exit(0);
return true;
}
return super.onKeyDown(keyCode, event);
}
public void onDestroy() {
super.onDestroy();
System.out.println("OnDestroy");
System.gc();
Runtime.getRuntime().gc();
*/
elepahant_img = (ImageView)findViewById(R.id.elephant_img);
if (elepahant_img != null)
{
((BitmapDrawable)elepahant_img.getDrawable()).getBitmap().recycle();
elepahant_img.setImageDrawable(null);
}
System.gc();
}
public void release(AnimationDrawable ad){
if(ad==null)
return;
ad.stop();
for (int i = 0; i < ad.getNumberOfFrames(); ++i){
Drawable frame = ad.getFrame(i);
if (frame instanceof BitmapDrawable) {
((BitmapDrawable)frame).getBitmap().recycle();
}
frame.setCallback(null);
frame=null;
}
ad.setCallback(null);
ad=null;
}
}
你可以在这里发布你的代码吗? – JiTHiN 2012-08-14 10:31:12