【问题标题】:Show a gif/animated imageview from a rest server in Android在 Android 中显示来自休息服务器的 gif/动画图像视图
【发布时间】:2011-11-14 05:05:09
【问题描述】:

在我的应用程序中,我想在万圣节、感恩节、圣诞节等特殊活动中展示一些启动动画。

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

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

  1. 将 .gif 数据下载到手机并以编程方式提取帧和帧速率是一个不错的解决方案,否则会为客户端增加不必要的负载?如果它被占用,是否有一些图书馆或资源可以指导我完成这项任务?

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

  3. 在哪里可以找到 gif 的内部结构?我在 Google 中搜索过,但没有找到

提前致谢

【问题讨论】:

  • +1 我遇到了同样的问题,我找不到正确的解决方案希望如果您找到解决方案,请写信给我们

标签: java android image gif


【解决方案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?

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

这两个链接可以帮助你在android中提取gif图像

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

【讨论】:

    【解决方案2】:

    一定要在客户端进行。你见过this吗?将动画拆分为多个图像并在客户端使用 AnimationDrawable 进行渲染可能是唯一的方法。

    【讨论】:

    • 客户端??????我之前看到过这个解决方案,但是他们提供的拆分工具是桌面应用程序
    • 为了更清楚,请使用您喜欢的任何工具将动画 GIF 拆分为多个图像。然后从网络服务器提供这些图像,您的 Android 设备将在该服务器上下载它们并将它们按上述方式拼凑在一起。
    【解决方案3】:

    您需要扩展视图才能播放 .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);
    
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-21
      • 1970-01-01
      • 2018-08-17
      • 1970-01-01
      • 2015-04-30
      • 2018-02-03
      相关资源
      最近更新 更多