Android AnimationDrawable, why always shot two times?
my Codes:
package net.tq5.bubbleexplosion;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class BubbleExplosionActivity extends Activity {
public static final String TAG = "BubbleExplosionActivity";
/** Called when the activity is first created. */
private FrameLayout parent;
private ExplosionView customView;
private AnimationDrawable exal;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set no title;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Create a FrameLayout and set the background;
parent = new FrameLayout(this);
parent.setBackgroundResource(R.drawable.bg);
customView = new ExplosionView(this);
customView.setVisibility(View.INVISIBLE);
customView.setBackgroundResource(R.anim.explosion);
exal = (AnimationDrawable) customView.getBackground();
parent.addView(customView);
parent.setOnTouchListener(new LayoutListener());
setContentView(parent);
}
class ExplosionView extends ImageView {
public ExplosionView(Context context) {
super(context);
}
// handle the location of the Explosion;
public void setLocation(int left, int top) {
this.setFrame(left, top, left + 40, top + 40);
}
}
class LayoutListener implements OnTouchListener {
@Override
public boolea开发者_如何学Pythonn onTouch(View view, MotionEvent event) {
// first you stop the animation
// you can always restart it;
customView.setVisibility(View.INVISIBLE);
if (exal.isRunning())
return false;
// exal.stop();
float x = event.getX();
float y = event.getY();
Log.i(TAG, "on Touch");
customView.setLocation((int) x - 20, (int) y - 20);
customView.setVisibility(View.VISIBLE);
exal.start();
return false;
}
}
}
<?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/bubble0" android:duration="500" />
<item android:drawable="@drawable/explode1" android:duration="500" />
<item android:drawable="@drawable/explode2" android:duration="500" />
<item android:drawable="@drawable/explode3" android:duration="500" />
<item android:drawable="@drawable/explode4" android:duration="500" />
<item android:drawable="@drawable/explode5" android:duration="500" />
</animation-list>
The image bubble0 always show two times; It seems that the animation has been fired two times, but I event tried this to make sure the start() method only be triggered once, but the animation will still be performed twice:
public class BubbleExplosionActivity extends Activity {
public static final String TAG = "BubbleExplosionActivity";
/** Called when the activity is first created. */
private FrameLayout parent;
private ExplosionView customView;
private AnimationDrawable exal;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set no title;
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Create a FrameLayout and set the background;
parent = new FrameLayout(this);
parent.setBackgroundResource(R.drawable.bg);
customView = new ExplosionView(this);
customView.setVisibility(View.INVISIBLE);
customView.setBackgroundResource(R.anim.explosion);
exal = (AnimationDrawable) customView.getBackground();
parent.addView(customView);
parent.setOnTouchListener(new LayoutListener());
setContentView(parent);
(new Handler()).postDelayed(new Runnable() {
@Override
public void run() {
customView.setLocation((int) 100 - 20, (int) 100 - 20);
customView.setVisibility(View.VISIBLE);
exal.start();
}
}, 3000);
}
}
Help!
It may be a little late for you, but I came across this question when I had the same problem.
The reason for the animation to run twice is that changing the visibility of the view to VISIBLE will restart the animation from the beginning.
The solution is to either start() the animation or to change the view visibility to View.VISIBLE, but not both.
精彩评论