Android Jpg Animation
I would like to download 10 JPG pictures from a site. The pictures where taken at intervals >> that is no problem How do I save pictures in an array of pictures. How would I
Solution 1:
You'll probably want to use AnimationDrawable in an ImageView
.
Add frames using the addFrame(Drawable frame, int duration)
method, and start the animation using start()
.
You can use many different methods to download and store the image (perhaps to external storage, or a db).
Solution 2:
I have an example of this, here I am downloading three images form site and then store in it path "/mnt/sdcard/..". If you want to show this in animation then I think that you can use flipper and then add this image dynamically and using an async task you can flip image next.
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TestActivity extends Activity {
/** Called when the activity is first created. */
Button btn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.startBtn);
final String url1 = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
final String url2 = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
final String url3 = "http://farm1.static.flickr.com/114/298125983_0e4bf66782_b.jpg";
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new AsyncDownload().execute(url1,url2,url3);
}
});
}
//------------------------------Class AsyncDownload----------------------------------
public class AsyncDownload extends AsyncTask<String, String, String>
{
ProgressDialog dialog;
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
Toast.makeText(TestActivity.this,"Downloading complate successfully",2).show();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog=new ProgressDialog(TestActivity.this);
dialog.setMessage("Downloading file..");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMax(100);
dialog.show();
}
@Override
protected void onProgressUpdate(String... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
dialog.setProgress(Integer.parseInt(values[0]));
}
@Override
protected String doInBackground(String... aurl) {
int count;
int lenghtOfFile=0;
long total = 0;
try {
for(int i=0;i<aurl.length;i++)
{
URL url = new URL(aurl[i]);
URLConnection conexion = url.openConnection();
conexion.connect();
lenghtOfFile =lenghtOfFile+conexion.getContentLength();
}
for(int i=0;i<aurl.length;i++)
{
URL url = new URL(aurl[i]);
URLConnection conexion = url.openConnection();
conexion.connect();
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream("/mnt/sdcard/img"+i+".jpg");
byte data[] = new byte[1024];
while ((count = input.read(data)) != -1) {
total += count;
publishProgress(""+(int)((total*100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}
} catch (Exception e) {
e.getMessage();
}
return null;
}
}
}
Post a Comment for "Android Jpg Animation"