我的代码:
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 boolean 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>
图像bubble0总是显示两次;似乎动画已被触发了两次,但我事件试图确保start()方法只触发一次,但动画仍将执行两次:
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);
}
}
帮助!
答案 0 :(得分:6)
对你来说可能有点晚了,但是当我遇到同样的问题时,我遇到了这个问题。
动画运行两次的原因是将视图的可见性更改为VISIBLE将从头开始重新启动动画。
解决方案是启动()动画或将视图可见性更改为View.VISIBLE,但不能同时更改两者。