animation - 如何在 Android 中使用 AnimationDrawable 或 AnimationUtils 从 SD 卡加载图像并运行动画

标签 animation sd-card android-animation android-imageview

我将图像存储在 SD 卡中,并希望使用该图像来运行动画。我为此使用了以下代码,但我的动画根本不起作用。

代码片段

playAnimation("xxx", medid, 25);//calling method
break;

public void playAnimation(String string, int medid2, int length) {
        // TODO Auto-generated method stub
        animation = new AnimationDrawable();
        Bitmap bitMap;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 2; //reduce quality
        player = MediaPlayer.create(this.getApplicationContext(), medid2);
        try {
            for (int i = 0; i <= length; i++) {
                System.out.println("File Name : - " + Environment.getExternalStorageDirectory().toString() + "/" + string + i);
                bitMap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString() + "/" + string + i);
                Drawable bmp = new BitmapDrawable(bitMap);
                animation.addFrame(bmp, DURATION);
            }
            animation.setOneShot(true);
            animation.setVisible(true, true);
            int frames = animation.getNumberOfFrames();
            System.out.println("Number of Frames are - " + frames);
            img.setBackgroundDrawable(animation);
            img.post(new Starter());

        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }

class Starter implements Runnable {
        public void run() {
            try {
                if(animation.isRunning()) {
                    animation.stop();
                    animation.start();
                    if (player.isPlaying()) {
                        player.stop();
                        player.start();
                    }
                    else {
                        player.start();
                    }
                } else {
                    animation.start();
                    if (player.isPlaying()) {
                        player.stop();
                        player.start();
                    }
                    else {
                        player.start();
                    }
                }
            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
    }

使用帧动画的概念,我需要运行我的动画。我可以获取图像,因为我已经做了一些调试,但是当我单击按钮并且调用此方法时,我的屏幕不显示任何动画。它只显示黑屏。我没有收到任何错误。如果有人有想法请告诉我。

谢谢

最佳答案

AnimationDrawable 只显示黑屏,可能是由不同原因引起的。例如,在 Android 开发指南中,Drawable Animation ,下面的代码可以让你加载一系列Drawable资源。

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}

但是,如果像下面的代码一样在 getBackground() 之后设置资源,屏幕将保持黑屏。

public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
}

如果你想从SD卡加载图片,并将其显示为动画,可以引用以下代码。我在 API 8 (2.3) 上编写和测试。

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    showedImage = (ImageView) findViewById(R.id.imageView_showedPic);
    showedImage.setBackgroundResource(R.drawable.slides);
    frameAnimation = (AnimationDrawable) showedImage.getBackground();
    addPicturesOnExternalStorageIfExist();
}

@Override
public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged (hasFocus);
    frameAnimation.start();
}
private void addPicturesOnExternalStorageIfExist() {
    // check if external storage 
    String state = Environment.getExternalStorageState();
    if ( !(Environment.MEDIA_MOUNTED.equals(state) || 
          Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) ) {
        return;
    } 

    // check if a directory named as this application
    File rootPath = Environment.getExternalStorageDirectory();
    // 'happyShow' is the name of directory
    File pictureDirectory = new File(rootPath, "happyShow"); 
    if ( !pictureDirectory.exists() ) {
        Log.d("Activity", "NoFoundExternalDirectory");
        return;
    }

    // check if there is any picture
    //create a FilenameFilter and override its accept-method
    FilenameFilter filefilter = new FilenameFilter() {
        public boolean accept(File dir, String name) {
        return (name.endsWith(".jpeg") || 
                name.endsWith(".jpg") || 
                name.endsWith(".png") );
        }
    };

    String[] sNamelist = pictureDirectory.list(filefilter);
    if (sNamelist.length == 0) {
        Log.d("Activity", "No pictures in directory.");
        return;
    }

    for (String filename : sNamelist) {
        Log.d("Activity", pictureDirectory.getPath() + '/' + filename);
        frameAnimation.addFrame(
                Drawable.createFromPath(pictureDirectory.getPath() + '/' + filename),
                DURATION);
    }

    return;
}

关于animation - 如何在 Android 中使用 AnimationDrawable 或 AnimationUtils 从 SD 卡加载图像并运行动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9148779/

相关文章:

android - 在android中从右上角、左下角、右下角旋转动画android?

iphone - Apple 如何制作 iBooks 2 抽认卡的动画?

android - 获取 Android 上的所有总空间和可用空间

android - SurfaceView.onDraw 未调用

android - 在 TabHost 内更改 Activity 转换

jquery - 为什么 CSS 动画会有延迟?

android - 背景为 Android 中的 AnimationDrawable 的按钮状态

android - 没有将 SD 卡相关的 Intent 发送到我的广播接收器

android - 如何高效检测SD卡是否存在且可写?

java - ViewGroup 动画不工作