Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Load images from SD CARD and Run Animation using AnimationDrawable or AnimationUtils in Android

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

like image 470
Scorpion Avatar asked Feb 05 '12 11:02

Scorpion


People also ask

What is frame by frame animation in Android?

In Android Frame Animation, you will be swapping frames repeatedly, so that it appears continuous to the human eye and we feel that it is animated. Frame is referred to an image. So to implement frame by frame animation in android, one needs to have set of images, which describes a motion.


1 Answers

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;
}
like image 82
Sunlit Jiang Avatar answered Oct 16 '22 18:10

Sunlit Jiang