In my application i get image from server and those image build animation.this all things are gone be right way and i create method for it.this is the method::
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 {
p开发者_Go百科ublic void run() {
animation.start();
}
}
}
now my problem is it is take much time to load image from server so simply i plan to use asyncTask. but problem is i cant get judgment that how can i do this?
can you give me example(Note : i know asyncTask and use already but problem is passing argument as per my xyz()
method declare)
Thanks nik
Here is the code:
- Note that the loop now is in the background thread
- After each loop, you publish the progress to setup the animation frame
- At the very end, you run onPostExecute to run the remaining code
Note, that this is just a skeleton and rough sketches, you need to understand and debug it if there is any problem. I haven't run the code yet
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();
}
}
}
精彩评论