2017-03-29 4 views
2

Je reçois des exceptions de mémoire lors du chargement des images dans mon application. J'ai intégré Picasso pour charger les images, mais le code ci-dessous ne fonctionne pas avec une liste d'animation pour un AnimationDrawable. L'animation est nulle:Comment charger des listes d'animation avec Picasso?

Picasso.with(this).load(R.drawable.qtidle).into(qtSelectButton); 
qtIdleAnimation = (AnimationDrawable)qtSelectButton.getDrawable(); 
if (qtIdleAnimation != null) 
    qtIdleAnimation.start(); 

Les travaux AnimationDrawable, si j'utilise ce code sans Picasso:

qtIdleAnimation = new AnimationDrawable(); 
qtIdleAnimation.setOneShot(false); 
for (int i = 1; i <= 7; i++) { 
    int qtidleId = res.getIdentifier("qtidle" + i, "drawable", this.getPackageName()); 
    qtIdleAnimation.addFrame(res.getDrawable(qtidleId), 100); 
} 
qtSelectButton.setImageDrawable(qtIdleAnimation); 
if (qtIdleAnimation != null) 
    qtIdleAnimation.start(); 

Mais ce code provoque l'exception de la mémoire. Est-il possible de charger des listes d'animation avec Picasso?

+0

Avez-vous trouvé quelque chose? Répondez, s'il vous plaît. –

+0

Désolé, je n'ai jamais compris et j'ai abandonné le projet. – Christian

Répondre

0

Picasso ne peut pas charger directement un animation-list défini dans un fichier xml dans une vue. Mais il est pas trop difficile à imiter le comportement de base de Picasso vous:

1. Définir et lancer l'animation par programmation, comme vous l'avez fait, mais à l'intérieur d'un AsyncTask pour éviter hors de mémoire exceptions

Resources res = this.getResources(); 
ImageView imageView = findViewById(R.id.image_view); 
int[] ids = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3}; 

Créer un sublcass de AsyncTask:

private class LoadAnimationTask extends AsyncTask<Void, Void, AnimationDrawable> { 
    @Override 
    protected AnimationDrawable doInBackground(Void... voids) { 
     // Runs in the background and doesn't block the UI thread 
     AnimationDrawable a = new AnimationDrawable(); 
     for (int id : ids) { 
      a.addFrame(res.getDrawable(id), 100); 
     } 
     return a; 
    } 
    @Override 
    protected void onPostExecute(AnimationDrawable a) { 
     // This will run once 'doInBackground' has finished 
     imageView.setImageDrawable(a); 
     if (a != null) 
      a.start(); 
    } 
} 

Lancez ensuite la tâche de charger l'animation dans votre ImageView:

new LoadAnimationTask().execute() 

2. Si vos dessinables sont plus grandes que votre point de vue, économiser de la mémoire en ne chargeant les dessinables à la résolution requise

Au lieu d'ajouter le drawable directement:

a.addFrame(res.getDrawable(id), 100); 

Ajouter une échelle Version -down de celui-ci:

a.addFrame(new BitmapDrawable(res, decodeSampledBitmapFromResource(res, id, w, h)); 

w et h sont les dimensions de votre affichage, et decodeSampledBitmapFromResource() est une méthode définie dans la documentation officielle Android (Loading Large Bitmaps Efficiently)