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.
ReplyDelete