i have this code to show gif image with Movie.
public class GIFView extends View{
private Movie movie;
private InputStream is;
private long moviestart;
public GIFView(Context context) {
super(context);
is=getResources().openRawResource(R.drawable.anim_cerca);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
if (moviestart == 0)
moviestart = now;
int relTime = (int)((now - moviestart) % movie.duration());
movie.setTime(relTime);
movie.draw(canvas,10,10);
this.invalidate();
}
}
My problem borns when gif is loaded, it draw very bad, only the first frame is shown and the other are like disturbed. What can i do?
EDIT: THE PROBLEM IS EMULATOR! IT DOESN'T SHOW GIF, BUT ON DEVICE IT'S OK! :)
Good start. Gotta make it more useful for loading different gifs after being added to the view and for either assets or resources. Also, for devices with hardware acceleration I was getting blank views, so I turned it off for this GIFView.
Also, be sure to put animated gifs in the res/drawable-xhdpi directory (or assets if using that way)
Usage:
and
Or you can simply add WebView to your xml and load the gif image inside the webview. You donot need to do anything else. The image will loop automatically inside the webview
The easiest way would be to use a
WebView
.Store your animated GIF in the assets folder. Then load the GIF by using the following code:
WebView wv = (WebView) findViewById(R.id.webView1); wv.loadUrl("file:///android_asset/anim5.gif");
webView1
is yourWebView
id, and anim5.gif is your GIF name.I solved in this way :
and in layout i put this custom view in this way:
in your AndroidManifest.xml, set the following attribute to your Application:
android:hardwareAccelerated="false"
create a class GifDecoder class
create another class GifAnimationDrawable
in Mainactivity class create object of GifAnimationDrawable
by using these classes you can add gif file to any view