在 Android 中显示来自休息服务器的 gif/动画图像视图

发布于 2024-12-03 21:01:14 字数 512 浏览 1 评论 0原文

在我的应用程序中,我想在一些特殊事件(如万圣节、感恩节、圣诞节等)启动时显示一些动画。

我没有找到在视图中显示动画 .gif 的热门方法。我发现的最接近的解决方案是使用 gif 帧制作一个 AnimationDrawable。

我正在尝试实现这一点,但对如何存储(目前我正在使用 LAMP 服务器)、传输和恢复服务器所需的资源到 Android 设备有一些疑问。

  1. 将 .gif 数据下载到手机并以编程方式提取帧和帧速率是一个很好的解决方案,否则会给客户端增加不必要的负载?如果适用,是否有一些库或源可以指导我完成该任务?

  2. 如果我想在服务器中处理 gif,然后将其逐帧提供给客户端,我该怎么做?我想过用图像的 URL 制作 JSON 并下载它们,但这可能不是一个好的选择,因为我需要大量的 http 连接,并且如果网络延迟很高,加载可能会更慢

  3. Where我能找到gif的内部结构吗?我在 Google 中搜索过,但没有找到任何结果

提前致谢

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 some 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

撩人痒 2024-12-10 21:01:14
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?

如果你想提取 gif 文件来获取帧和帧速率,你需要一个 GIF 解码器,然后将它们应用到 AnimationDrawable 将帮助您以图表方式添加帧。

这两个链接可以帮助您在androidhttp://www.basic4ppc中提取gif图像

。 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

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

椵侞 2024-12-10 21:01:14

一定要在客户端进行。您看过这个吗?将动画分割成多个图像并使用 AnimationDrawable 在客户端渲染它可能是唯一的方法。

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.

音盲 2024-12-10 21:01:14

您需要扩展视图才能播放 .gif
考虑以下代码

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();
}
}

在 xml 中使用此视图并提供 url,
如果存在网络延迟,请在单独的线程中下载 gif 并将输入流提供给构造函数

将其更改为

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

    movie=Movie.decodeStream(in);

    }

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);

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文