2012-02-05 10 views
9

Tengo imágenes almacenadas en la tarjeta SD y usando esas imágenes deseo ejecutar una animación. Estoy usando el siguiente código para esto, pero mi animación no funciona en absoluto.Cómo cargar imágenes desde la tarjeta SD y ejecutar la animación usando AnimationDrawable o AnimationUtils en Android

Fragmento de código

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

Usando concepto de animación fotograma se necesita para ejecutar mi animación. Puedo buscar imágenes, ya que he realizado algunas depuraciones, pero cuando hago clic en el botón y se llaman estos métodos, mi pantalla no muestra ninguna animación. Solo muestra una pantalla negra. No estoy obteniendo ningún error en esto. Si alguien tiene una idea, por favor hágamelo saber.

Gracias

+0

Por favor, si alguien tiene alguna idea o sugerencia amable ayuda ... – Scorpion

Respuesta

2

Un AnimationDrawable solo muestra la pantalla en negro, puede ser causada por diferentes razones. Por ejemplo, en la Guía de desarrollo de Android, Drawable Animation, el siguiente código le permite cargar una serie de recursos 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(); 
} 

Sin embargo, si se establece de recursos después de getBackground() como el siguiente código, la pantalla mantendrá negro.

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

Si desea cargar imágenes de la tarjeta SD y mostrarlas como animación, puede consultar el siguiente código. Escribo y pruebo en 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; 
} 
Cuestiones relacionadas