The gif animation is supported in Android when GIF animation is played as Movie. Movie is a class provided to you by Android SDK.
In your main or any Activity File
public class AA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceStated);
setContentView(new MYGIFView());
}
}
Here's the class
private class MYGIFView extends View{
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public GIFView(Context context) {
super(context);
Provide your own gif animation file
is=context.getResources().openRawResource(R.drawable.earth);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}
In your main or any Activity File
public class AA extends Activity {
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceStated);
setContentView(new MYGIFView());
}
}
Here's the class
private class MYGIFView extends View{
Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public GIFView(Context context) {
super(context);
Provide your own gif animation file
is=context.getResources().openRawResource(R.drawable.earth);
movie=Movie.decodeStream(is);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;
}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}
Get this man a beer! I have been looking for a way to draw an animated gif on a canvas for days...
ReplyDeleteHey!
ReplyDeleteI'm using this method to show a .gif animation. I get a problem though. The gif displayed is larger than the resolution, which results in a clipped picture. I've also tried the following:
C = new GIFView(Image.this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(200,200); C.setLayoutParams(params);
parent.addView(C, index);
Which shows a 200x200 view, but the movie is still clipped within those 200x200.
How would I go about to resize the movie?
Thanks!
This comment has been removed by the author.
ReplyDeletethis is absolutly the esaiest way to do the job..
ReplyDeletenevertheless there are 2 changes to do in the code to get it working...
IN THE FOLLOWING
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceStated);
setContentView(new MYGIFView());
}
}
just replace
setContentView(new MYGIFView());
in
setContentView(new MYGIFView(this));
AND IN
public GIFView(Context context) {
super(context);
Provide your own gif animation file
is=context.getResources().openRawResource(R.drawable.earth);
movie=Movie.decodeStream(is);
}
REPLACE THE FIRST LINE IN
public MYGIFView(Context context)
according to the name of the class...
after done this little changes it should work as for me... thank you man
Hi Erik,
ReplyDeleteDid you figure out how to resize the movie?
Thanks,
Utpal
I did!
ReplyDeleteWithin the MYGIFView class, you have onDraw(Canvas canvas)
Simply, within onDraw, before movie.draw, pass canvas.scale(scalefactorx,scalefactory);
I calculated the scale factors as the ratio between the view size and the movie size, using:
(double)this.getWidth() / (double)movie.width()
and the same for height.
Thanks a lot Erik. I will try out..
ReplyDeleteWorks like a charm!! thanks again
ReplyDeleteGreat! Glad to be of use!
ReplyDeleteHi Erik, i implemented what you advised Utpal above but it didn't work my mine. It just displayed a blank canvas with nothing drawn on it.
Deleteit displays the gif but does not play it properly.. its totally distorted..
ReplyDeleteI am getting same results for few gifs. Did you find answer to this?
DeleteI've had this problem only on specific devices (Namely HTC Desire GSM). However, this only happened on stock roms, not on custom MIUI or Cyanogenmod roms. Also, it happens in the emulator.
DeleteI suspect this is something in the implementation of Movie and thus not something that can be rectified without modifying Movie. While my Android knowledge is very small, I think this means that on devices where the problem occurs, a custom ROM is required. Perhaps it is possible to use a custom implementation of Movie, but if so, I think that it is A LOT simpler to simply use some existing Java library to split any gif file into individual bitmaps, and display them as an animation.
However I have shelved the project where I used GIF so I won't be looking into it anytime soon.
At any rate, I used a Java library for GIF encoding. The only specifically Android lib I could find at the time was a Native lib but I wanted to maintain cross-platform compatibility so I discarded that and used and adapted a Java lib instead. The only problem I had with that was that it assumed RBG instead of RGB. Perhaps this is native to gif or specific to that lib. At any rate, I solved this by doing a pixel-by-pixel conversion to RGB.
Let me know if you try anything!
I am using the above code but my animation is distorted. I have tried different gif images but same result. Do you know why animation is distorting...???
ReplyDeleteThis seems to be an Android problem. Some ROMs exhibit the problem, other not.
DeleteI believe the only solution is to not use this method at all, and simply extract each frame and display separately.
Gif is, unfortunately, poorly supported by Android.
How can we use onClick() method on this gif image?
ReplyDeleteIts working only on API level 3.. This is not working on later versions of SDK.. Anyone faced the same issue??
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHow can I add this to a fragment layout I already have and have it in a specific spot?
ReplyDeletehi...sir..first of all thank u so much for this method...bt i need lil help in displaying the .gif image at a specific location.How cn i do that..???
ReplyDeletethanks mate , it helped a lot
ReplyDeleteUse
ReplyDeletemovie.draw(canvas, ((float) this.getWidth() / (float) movie.width()),
(float) this.getHeight() / (float) movie.height());
instead of
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
Just you cant set scaletype to fitxy for gif..
if you get blank screen, try this:
ReplyDeletePaint p = new Paint();
p.setAntiAlias(true);
setLayerType(LAYER_TYPE_SOFTWARE, p);
Thanks dear.
DeleteWhere to add this code?
Deleteadd before this.invalidate()....worked like a charm for me :)
DeleteThis comment has been removed by the author.
ReplyDeleteHi! that's very nice work. I need help in showing gif animation in view so that i could set its position in activity and want to add some other items to that activity.
ReplyDeleteHey, I want to set Gif Image for particular ListView.
ReplyDeletei want to do that for MediaPlayer, so what i have to do..??
Hey it crash and error message is :
ReplyDeleteatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 7333 (example.showgif)
Same issue in here :/
Deleteon Note 3 under Lollipop
any idea/correction please ?
This comment has been removed by the author.
DeleteI had this error. I fixed it by putting application android:hardwareAccelerated="false" into my manifest to disable hardware acceleration.
DeleteThis android:hardwareAccelerated="false" works like a charm.
DeleteThis comment has been removed by the author.
ReplyDeleteWhat about the xml file ??? What should i pun in my xml file?
ReplyDeleteThanks, works fine.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletei am getting airthmetic exception - divided by zero at
ReplyDeletethis line -
int relTime = (int) ((now - moviestart) % movie.duration());
and why you change name of your constructor-
ReplyDeletei think constructor name should same as class name...not??
you defined this :-
public GIFView(Context context) {
super(context);
// Provide your own gif animation file
is = context.getResources().openRawResource(R.raw.gif_demo_img);
movie = Movie.decodeStream(is);
}
and this is giving complile time error,
correct me if i am wrong
finally i change constructor name and replace
ReplyDeleteSystem.out.println()
with
Toast popup
and when i launch, its only showing popup which indicating
moviestart=4415249
time=524
reltime=4480
but not gif,
whole activity is blank white
Gr8. Now also tell how to use it
ReplyDeleteFetching Problem while changing gif file. I had 2 gif in my draw able folder. But when i try to put it the place u indicates. its showing error.
ReplyDelete