【问题标题】:play downloaded Gif image in android在android中播放下载的Gif图像
【发布时间】:2012-03-18 15:51:54
【问题描述】:

我正在从服务器下载我的应用程序中的 GIF 图像,然后我用 ImageView 显示它,但它不是动画的。 有没有其他方法可以播放下载的动画 GIF 图片 。 提前致谢。

【问题讨论】:

标签: android gif animated-gif


【解决方案1】:

经过一些研究,似乎最好(或最简单)的解决方案是使用 WebView :

例如:

将 myAnimatedGif.gif 文件放入 assets 文件夹。

创建一个 xml web 视图:

<WebView
    android:id="@+id/myWebView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

然后将 gif 文件加载到 web 视图中:

    WebView view = (WebView) findViewById(R.id.myWebView);
    view.loadUrl("file:///android_asset/myAnimatedGif.gif");

享受吧!

另一种解决方案是使用这种库: https://github.com/koral--/android-gif-drawable

【讨论】:

  • 这行得通!但是您必须将 myAnimatedGif.gif 放在 app/src/main/assets 中。
【解决方案2】:

您可以使用 WebView 实现此目的。 这是我使用的:

锚定 WebView 类的自定义 LinearLayout

import android.view.View;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Spinner extends LinearLayout{

    public Spinner(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        View.inflate(context, R.layout.spiner, this);
        WebView web = (WebView) findViewById(R.id.web);
        web.loadUrl("file:///android_asset/booting.gif");
    }
    public void setText(String text){
        TextView t = (TextView) findViewById(R.id.txtloading);
        t.setText(text);
    }
    public void refresh(){
        WebView web = (WebView) findViewById(R.id.web);
        web.loadUrl("file:///android_asset/booting.gif");       
    }
}

R.layout.Spinner XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:gravity="center|center_vertical"
    android:padding="10dp">
    <WebView android:id="@+id/web" 
        android:layout_width="24dp"
        android:layout_height="12dp"
        />

    <TextView
        android:id="@+id/txtloading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Loading..."
        />

</LinearLayout>

你可以用作

Spinner spinner = new Spinner(this);
myContainer.addView(spinner);

您可以在微调器上调用刷新以确保图像播放:

spinner.refresh();

希望对您有所帮助。

【讨论】:

    【解决方案3】:

    我正在使用下面的自定义视图而不是图像视图。

    public class SampleView extends View {
    
        private Movie mMovie;
        private long mMovieStart;
    
        public SampleView(Context context) {
            super(context);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.girl_dances);
            mMovie = Movie.decodeStream(is); 
        }
    
        public SampleView(Context context, AttributeSet attrSet) {
            super(context, attrSet);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.girl_dances);
            mMovie = Movie.decodeStream(is);
        }
    
        public SampleView(Context context, AttributeSet attrSet, int defStyle) {
            super(context, attrSet, defStyle);
            setFocusable(true);
    
            java.io.InputStream is;
            is = context.getResources().openRawResource(R.drawable.girl_dances);
            mMovie = Movie.decodeStream(is);
        }
    
        @Override
        protected void onDraw(Canvas canvas) {
            canvas.drawColor(0x00000000);
    
            Paint p = new Paint();
            p.setAntiAlias(true);
    
            long now = android.os.SystemClock.uptimeMillis();
            if (mMovieStart == 0) { // first time
                mMovieStart = now;
            }
            if (mMovie != null) {
                int dur = mMovie.duration();
                if (dur == 0) {
                    dur = 1000;
                }
                int relTime = (int) ((now - mMovieStart) % dur);
                mMovie.setTime(relTime);
                mMovie.draw(canvas, getWidth() / 2 - mMovie.width() / 2,
                        getHeight() / 2 - mMovie.height() / 2);
                invalidate();
            }
        }
        }
    

    XML 布局:

    <com.test.sample.SampleView
    android:id="@+id/gif_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
    

    【讨论】:

    • 无法传递 gif 资源,它向我显示:“预期的资源类型。原始...”。我在 Android Studio 中使用 Android API16
    猜你喜欢
    • 2014-06-27
    • 1970-01-01
    • 2014-12-29
    • 2017-05-06
    • 1970-01-01
    • 2012-07-10
    • 1970-01-01
    • 2017-06-23
    • 1970-01-01
    相关资源
    最近更新 更多