Воспроизвести загруженное изображение Gif в Android - PullRequest
16 голосов
/ 29 февраля 2012

Я загружаю изображение GIF в свое приложение с сервера, а затем показываю его с ImageView, но оно не было анимированным. Есть ли другой способ воспроизвести загруженное анимированное GIF изображение . Заранее спасибо.

Ответы [ 3 ]

10 голосов
/ 29 февраля 2012

Я использую приведенный ниже пользовательский вид вместо просмотра изображений.

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" />
2 голосов
/ 27 апреля 2016

После некоторых исследований кажется, что лучшим (или самым простым) решением является использование WebView:

ex:

, поместите файл myAnimatedGif.gif в папку активов.

создание веб-приложения в формате XML:

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

и загрузка файла GIF в веб-представление:

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

Наслаждайтесь!

Другоерешение состоит в том, чтобы использовать этот вид библиотеки: https://github.com/koral--/android-gif-drawable

0 голосов
/ 22 января 2015

Вы можете достичь этого с помощью WebView.Вот что я использую:

Пользовательский LinearLayout, который привязывает класс WebView

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

XML-файл R.layout.Spinner:

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

Вы можете использоватьas

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

Вы можете вызвать обновление на счетчике, чтобы обеспечить воспроизведение изображения:

spinner.refresh();

Надеюсь, это поможет.

...