2011-09-02 2 views
0

Dans mon application je reçois l'image du serveur et les construire d'image animation.this toutes choses sont partis être bonne et je crée la méthode pour patchage est la méthode ::confusion pour passer argument AsyncTask

package com.animation; 

import java.io.InputStream; 
import java.net.URL; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.drawable.AnimationDrawable; 
import android.graphics.drawable.BitmapDrawable; 
import android.graphics.drawable.Drawable; 
import android.os.Bundle; 
import android.widget.Button; 
import android.widget.ImageView; 
public class animation extends Activity { 
    Button Buttona; 
    AnimationDrawable animation; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     ImageView img = (ImageView) findViewById(R.id.simple_anim); 
     animation = new AnimationDrawable(); 

      try { 
       for(int i=0;i<54;i++) 
       {  
       xyz("girl000",i); 
       }   
      animation.setOneShot(false); 
      } catch (Exception e) { 
      } 
      img.setBackgroundDrawable(animation); 
      img.post(new Starter()); 

    } 


    public void xyz(String str,int x) 
    { 
     try { 
      Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
     "http://201.109.115.111/MRESC/images/test/girl/"+"girl000"+x+".png") 
      .getContent()); 
      Drawable frame =new BitmapDrawable(bitmap); 
      animation.addFrame(frame, 50); 

     } catch (Exception e) { 

     } 

    } 
    class Starter implements Runnable { 

     public void run() { 
      animation.start();   
     } 


    } 
} 

maintenant mon problème est il prend beaucoup de temps pour charger l'image à partir du serveur si simplement je prévois d'utiliser asyncTask. mais le problème est que je ne peux pas obtenir le jugement que comment puis-je faire cela? pouvez-vous me donner l'exemple (Note: Je sais que AsyncTask et utilise déjà mais le problème est passage d 'arguments selon ma déclarer la méthode xyz())

Merci nik

Répondre

1

Voici le code:

  • Notez que la boucle est maintenant dans le fil d'arrière-plan
  • Après chaque boucle, vous publiez la progression de la configuration du cadre d'animation
  • A la fin, vous exécutez onPostExecute pour exécuter le code restant

Notez que ceci est juste un squelette et croquis, vous devez comprendre et corriger s'il y a un problème. Je n'ai pas encore exécuté le code


    public class Animation extends Activity { 
     Button Buttona; 
     AnimationDrawable animation; 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.main); 
      animation = new AnimationDrawable(); 

      AsyncTask asyncTask = 
       new AsyncTask() { 

       @Override 
       protected Void doInBackground(Void... params) { 
        try { 
         // Execute this whole loop in background, so it doesn't 
         // block your UI Thread 
         for(int i=0;i<54;i++) {  
          xyz("girl000",i); 
         }   

        } catch (Exception e) { 
         return null; 
        } 
       } 

       public void xyz(String str,int x) { 
        try { 
         Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
           "http://201.109.115.111/MRESC/images/test/girl/"+"girl000"+x+".png") 
         .getContent()); 

         // publish progress so that the bitmap is set on the UI Thread 
         publishProgress(bitmap); 
        } catch (Exception e) { 
         // handle error 
        } 
       } 

       @Override 
       protected void onProgressUpdate(Bitmap... result) { 
        // handle the progress update to add the animation frame 
        Bitmap bitmap = result[0]; 
        Drawable frame =new BitmapDrawable(bitmap); 
        animation.addFrame(frame, 50); 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        if(result != null) { 
         // execute the rest of your instruction after the loop is over here 
         animation.setOneShot(false); 
         ImageView img = (ImageView) findViewById(R.id.simple_anim); 
         img.setBackgroundDrawable(animation); 
         img.post(new Starter()); 
        } else { 
         // handle error 
        } 
       } 
      }; 

      asyncTask.execute(); 
     } 

     class Starter implements Runnable { 
      public void run() { 
       animation.start();   
      } 
     } 
    } 

+0

pouvez-vous donner un exemple? –

+0

je prends à propos de http://developer.android.com/reference/android/os/AsyncTask.html pas le nom de la classe asynctask. –

+0

oui je comprends que, vous enveloppez l'argument dans xyz à un nouvel encapsuleur d'objet et passez l'Object Wrapper comme param à la asyncTask. Laissez-moi mettre le code pour illustrer cela. Voir la mise à jour de la réponse – momo

Questions connexes