【问题标题】:android : show gif image in androidandroid : 在 android 中显示 gif 图像
【发布时间】:2016-08-24 14:08:23
【问题描述】:

我是 android 新手,我想以 gif 格式显示加载图像。 在谷歌搜索后,我发现了这个:

GIF 视图:

package ComponentGif;

import java.io.InputStream;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.util.AttributeSet;
import android.view.View;

public class GifView extends View {

    private InputStream gifInputStream;
    private Movie gifMovie;
    private int movieWidth, movieHeight;
    private long movieDuration;
    private long mMovieStart;
    private Canvas _canvas = null;

    public GifView(Context context) {
        super(context);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public GifView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        setFocusable(true);
        gifInputStream = context.getResources().openRawResource(com.example.agency.R.drawable.loading);

        gifMovie = Movie.decodeStream(gifInputStream);
        movieWidth = gifMovie.width();
        movieHeight = gifMovie.height();
        movieDuration = gifMovie.duration();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(movieWidth, movieHeight);
    }

    public int getMovieWidth() {
        return movieWidth;
    }

    public int getMovieHeight() {
        return movieHeight;
    }

    public long getMovieDuration() {
        return movieDuration;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        long now = android.os.SystemClock.uptimeMillis();
        if (mMovieStart == 0) { // first time
            mMovieStart = now;
        }

        if (gifMovie != null) {

            int dur = gifMovie.duration();
            if (dur == 0) {
                dur = 1000;
            }

            int relTime = (int) ((now - mMovieStart) % dur);

            gifMovie.setTime(relTime);

            gifMovie.draw(canvas, 0, 0);

            invalidate();

        }
    }

}

在 Main_Activity xml 我添加了这个:

<ComponentGif.GifView
        android:id="@+id/imgWaitLoading"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:layout_marginBottom="30dp"
        android:layout_marginLeft="90dp" />

但图像不会在循环中显示。它只循环一次。我怎样才能在循环中显示它?

【问题讨论】:

标签: android gif


【解决方案1】:

Use this library GitHub GifView

实现'com.github.Pawan245:Hanumanjetlibrary:6.1.0'

【讨论】:

    猜你喜欢
    • 2015-03-20
    • 2015-10-21
    • 2015-11-12
    • 2017-08-01
    • 1970-01-01
    • 2011-12-08
    • 2013-08-22
    • 2014-02-21
    • 1970-01-01
    相关资源
    最近更新 更多