自定义 WallpaperService 仅显示 GIF 的第一帧

Custom WallpaperService shows only the first frame of the GIF

我正在做一个小项目,我正在制作我自己的自定义动态壁纸应用程序,我应该能够 select gif 并自动制作动态壁纸。因此,我需要编写自己的墙纸服务,到目前为止它可以工作,但它似乎只绘制了第一帧。

代码如下:

package com.example.r.c;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.os.Environment;
import android.service.wallpaper.WallpaperService;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.SurfaceHolder;
import android.graphics.Movie;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import android.os.Handler;

public class CustomWallpaperService extends WallpaperService {
@Override
public Engine onCreateEngine() {

    try{

        File f = new File("/storage/emulated/0/testgif.gif");
        Movie m = Movie.decodeStream(new FileInputStream(f));
        return new wEngine(m);

    } catch (Exception e){
        return null;
    }


}

public class wEngine extends Engine{

    private Movie movie;
    private SurfaceHolder holder;
    private Handler handler;
    private boolean visible = true;

    public wEngine(Movie m){
        this.movie = m;
        handler = new Handler();
    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);
        this.holder = surfaceHolder;
    }

    private Runnable drawRunnable = new Runnable() {
        @Override
        public void run() {
            draw();
        }
    };

    private void draw(){
        Canvas canvas = holder.lockCanvas();
        canvas.save();

        canvas.scale(2f,2f);
        movie.draw(canvas, 0, 0);
        canvas.restore();
        holder.unlockCanvasAndPost(canvas);
        movie.setTime((int) System.currentTimeMillis() % movie.duration());

        handler.removeCallbacks(drawRunnable);
        handler.postDelayed(drawRunnable, 20);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);

        this.visible = visible;
        if (visible) {
            handler.post(drawRunnable);
        } else {
            handler.removeCallbacks(drawRunnable);

        }
    }
}
}

正如我所说,代码确实绘制了第一帧,因此打开或访问文件没有问题,如果我在 android 文件管理器中打开 gif,它工作正常。

感谢各种帮助!

找到问题了!

movie.setTime((int) System.currentTimeMillis() % movie.duration());

需要:

movie.setTime((int) (System.currentTimeMillis() % movie.duration()));