gpt4 book ai didi

Android AnimationDrawable,为什么总是拍两次?

转载 作者:太空狗 更新时间:2023-10-29 16:24:54 26 4
gpt4 key购买 nike

我的代码:


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);
}
}

帮助!

最佳答案

这对你来说可能有点晚了,但是当我遇到同样的问题时,我遇到了这个问题。

动画运行两次的原因是将 View 的可见性更改为 VISIBLE 将从头开始重新启动动画。

解决方案是启动()动画或将 View 可见性更改为 View.VISIBLE,但不能同时进行。

关于Android AnimationDrawable,为什么总是拍两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4506486/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com