I am trying to do the same (to display an animated GIF) using this method.
It only works if you specify use-sdk android: minSdkVersion = "3"
To scale ...
package com.example.GIFShow; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Movie; import android.graphics.Rect; import android.util.Log; import android.view.View; class MYGIFView extends View { private static final boolean twigDebug = true; private Movie mMovie; private long mMovieStart; MYGIFView(Context aContext, Movie aMovie) { super(aContext); if (aMovie == null) return; mMovie = aMovie; mMovieStart = android.os.SystemClock.uptimeMillis(); } protected void onDraw(Canvas aCanvas) { super.onDraw(aCanvas); if (mMovie == null || mMovie.duration() == 0) return; int relTime = (int)((android.os.SystemClock.uptimeMillis() - mMovieStart) % mMovie.duration()); mMovie.setTime(relTime); Bitmap movieBitmap = Bitmap.createBitmap(mMovie.width(), mMovie.height(), Bitmap.Config.ARGB_8888); Canvas movieCanvas = new Canvas(movieBitmap); mMovie.draw(movieCanvas, 0, 0); Rect src = new Rect(0, 0, mMovie.width(), mMovie.height()); Rect dst = new Rect(0, 0, this.getWidth(), this.getHeight()); aCanvas.drawBitmap(movieBitmap, src, dst, null); this.invalidate(); } }
Now you can get and pass the Movie object to the class in one of two ways ...
Get input GIF Movie object from project dropdown
Movie movie = Movie.decodeStream (context.getResources().openRawResource(R.drawable.somefile));
Or get the input GIF Movie object from external storage (/ mnt / sdcard ... / mnt / extSdCard ... etc.)
Movie movie = null; try { FileInputStream stream = new FileInputStream(Environment.getExternalStorageDirectory() + "/somefolder/somefile.gif"); try { byte[] byteStream = streamToBytes(stream); movie = Movie.decodeByteArray(byteStream, 0, byteStream.length); } finally { stream.close(); } } catch (IOException e) { }
Now set the GIF / Movie motion picture to your activity:
View view = new MYGIFView(this, movie); setContentView(view);
If you get a GIF image / Movie object from external storage (second example), you will need a supporting procedure:
private byte[] streamToBytes(InputStream is) { ByteArrayOutputStream os = new ByteArrayOutputStream(1024); byte[] buffer = new byte[1024]; int len; try { while ((len = is.read(buffer)) >= 0) os.write(buffer, 0, len); } catch (java.io.IOException e) { } return os.toByteArray(); }