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

发布于 2025-01-02 19:18:45 字数 2415 浏览 3 评论 0原文

我将图像存储在 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();
            }
        }
    }

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

谢谢

I am having Images stored in SD Card and using that images i wish to run an animation. I am using the following code for this but my animation is not working at all.

Code Snippet

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

Using concept of Frame Animation i need to run my animation. I am able fetch images as i have done some debugging but when i click on button and this methods are called my screen is not displaying any animation. It just display black screen only. I am not getting any error in this. If anyone having idea please kindly let me know.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

唐婉 2025-01-09 19:18:45

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

An AnimationDrawable just shows black screen, may be caused by different reasons. For example, in the Android Dev Guide, Drawable Animation, the following code lets you load a series of Drawable resources.

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

However, if you set resource after getBackground() like the following code, the screen will keep black.

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

If you want to load images from SD card, and show them as animation, you can refer to the following code. I write and test on 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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文