【问题标题】:random frame animation does not work android随机帧动画不起作用android
【发布时间】:2010-11-07 20:40:40
【问题描述】:

我正在尝试从一组图像中实现随机动画,其中每一帧都是图像集中的随机图像。为此,我正在使用 AnimationDrawable 对象的 addFrame 方法,但动画不起作用。我尝试搜索各种教程,也尝试了其他方法,但没有任何帮助。以下是我的程序代码,谁能告诉我做错了什么。在布局中有一个 ImageView。但是当我从 xml 动画定义(不是随机的)尝试动画时,它可以工作。谢谢和问候。

import java.util.Random;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class MemoryGame extends Activity implements OnTouchListener{
MemoryGameView mgv;
ImageView memImg;
int []seqimg = new int[15];
AnimationDrawable animation; 

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.memorylayout);
    memImg = (ImageView) findViewById(R.id.mem_img);
    memImg.setOnTouchListener(this);

    //generate the random sequence of list
    randomImgList();
    defAnimation();
  }

//select the array of random sequence of images for viewing
private void randomImgList(){
    //initialize the sequece
    for (int i = 0; i < 15; i++) {
        seqimg[i] = i+1;
    }
    Random rng = new Random();
    // i is the number of items remaining to be shuffled.
    for (int i = 15; i > 1; i--) {
        // Pick a random element to swap with the i-th element.
        int j = rng.nextInt(i);  // 0 <= j <= i-1 (0-based array)
        // Swap array elements.
        int tmp = seqimg[j];
        seqimg[j] = seqimg[i-1];
        seqimg[i-1] = tmp;
    }
}
//define the animation sequece
private void defAnimation() {
    animation = new AnimationDrawable();
    for(int i=0; i<10; i++) {
        String imgName = "mimg" + seqimg[i];
        int id = getResources().getIdentifier(imgName, "drawable", getPackageName());
        Log.d("id",""+id);
        animation.addFrame(getResources().getDrawable(id), 1000);
    }
    animation.setOneShot(true);
}

//define the touch event from the OnTouchListener interface
public boolean onTouch(View v, MotionEvent arg1) {
    if(v.getId() == memImg.getId()) {
        memImg.setBackgroundDrawable(animation);
        animation.setVisible(true, true);
        animation.stop();
        animation.start();
    }
    return true;
}
}

【问题讨论】:

  • “动画不工作”是什么意思?更具体地说,到底发生了什么?
  • 当我触摸图像视图时,它只会继续显示初始化的图像并且动画不会开始。我尝试记录动画是否正在运行,它表明动画正在运行,但我在 imageview 中没有看到任何变化。

标签: android animation random imageview


【解决方案1】:

我发现了问题,我正在发布解决方案,也许如果其他人发现这个问题会有所帮助。 实际上动画正在工作,但用于初始化 ImageView 的初始图像位于动画上方,因此动画是不可见的。现在我在开始动画之前将 ImageView 设置为空白图像,动画现在可见。 谢谢。

【讨论】:

    猜你喜欢
    • 2013-01-14
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 2021-10-06
    • 2014-09-20
    • 2023-03-22
    • 1970-01-01
    相关资源
    最近更新 更多