开发者

Show a gif/animated imageview from a rest server in Android

开发者 https://www.devze.com 2023-04-03 17:55 出处:网络
In my application I\'d like to show some animations at the startup in some special events like Halloween, Thanksgiving, Christmas etc.

In my application I'd like to show some animations at the startup in some special events like Halloween, Thanksgiving, Christmas etc.

I didn't find a hot to show an animated .gif in a View. The closest solution I found is to make an AnimationDrawable with the gif frames.

I'm trying to implement that but have s开发者_JAVA百科ome questions about how to store (currently i'm using a LAMP server), transfer and recover the resources needed from the server to the Android device.

  1. Downloading the .gif data to the phone and extract there the frames and framerate programmatically is a nice solution or it will add an unnecessary load to the client? If its appropriated is there some library or source for guiding me in with that task?

  2. If I want to handle the gif in the server and then I want to serve it to the client frame-by-frame, how can I do that? I've thought in making a JSON with the URL's of the images and download them but maybe is not a nice option since I'd need a lot of http connections and the load could be slower if the network latency is high

  3. Where can I find the internal structure of a gif? I have searched in Google but nothing found

Thanks in advance


Downloading the .gif data to the phone and extract there the frames and framerate 
programmatically is a nice solution or it will add an unnecessary load to the client?
If its appropriated is there some library or source for guiding me in with that task?

if you want to extract the gif file to get the frames and the frame rate you need a GIF decoder and then applay them to the AnimationDrawable this will help you to add frames diagrammatically.

this two links can help you to extract the gif image in android

http://www.basic4ppc.com/android/help/gifdecoder.html

http://code.google.com/p/loon-simple/source/browse/trunk/android/LGame-Android-0.2.6S/org/loon/framework/android/game/core/GIFDecoder.java?r=7


Definitely do it on the client side. Have you seen this? Splitting the animation into multiple images and rendering it on the client side with an AnimationDrawable may be the only way to go.


You will require to extend a view to make it to play .gif Consider the following code

private class GifView extends View{

Movie movie;
InputStream in=null;
long moviestart;
Url url

public GifView(Context context,String downloadUrl) {
super(context);
this.url=new Url(downloadUrl);
URLConnection con = url.openConnection();



in=con.getInputStream()
movie=Movie.decodeStream(in);

}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
Log.v("tag","now="+now);
if (moviestart == 0) { // first time
moviestart = now;

}
Log.v("tag","\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
Log.v("tag""time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}

Use This view in you xml and supply the url, If network Latency is present download the gif in separate thread and supply the input stream to the constructor

Change it to

public GifView(Context context,InputStream in) {
    super(context);

    movie=Movie.decodeStream(in);

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号