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

如何从SD卡加载图像并在Android中使用AnimationDrawable或AnimationUtils运行动画,animation,sd-card,android-animation,android-imageview,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 A

我有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();
            }
        }
    }
播放动画(“xxx”,medid,25岁)//调用方法
打破
public void播放动画(字符串、整数medid2、整数长度){
//TODO自动生成的方法存根
animation=新的AnimationDrawable();
位图;
BitmapFactory.Options=new-BitmapFactory.Options();
options.inSampleSize=2;//降低质量
player=MediaPlayer.create(this.getApplicationContext(),medid2);
试一试{

对于(int i=0;iAnimationDrawable只显示黑屏,可能是由不同的原因造成的

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()之后设置resource,屏幕将保持黑色

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卡加载图像并将其显示为动画,可以参考以下代码


如果有人有任何想法或建议,请帮助。。。。
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;
}