GifAnimated Wallpaper Android Studio

0

I still can not solve it, when executing the application, the selected gif is normally established, but if I open the application to select a different one, it does not change anymore.

import android.graphics.Canvas; import android.graphics.Movie; import android.os.Handler; import android.os.SystemClock; import android.service.wallpaper.WallpaperService; import android.util.Log; import android.view.SurfaceHolder;

import java.io.IOException; import java.io.InputStream;

/ **  * Created by jreese on 12/7/2015.  * / public class GIFWallpaperService extends WallpaperService {     // static end String TAG="gifService";     static end String TAG="gifWallpaper";     static final Handler gifHandler = new Handler ();

@Override
public void onCreate() {
    super.onCreate();
}

@Override
public Engine onCreateEngine() {
    try {
        return new GifEngine();
    } catch (IOException e) {
        Log.w(TAG, "Error Creando Motor", e);
        stopSelf();
        return null;
    }
}

class GifEngine extends Engine {
    private final Movie gif;
    private final int duration;
    private final Runnable runnable;

    float scaleX;
    float scaleY;
    int when;
    long start;
    /////
    Globals globals= new Globals();
    private int idImagen;

    private Integer []imagenes= {R.raw.uno,R.raw.dos,R.raw.tres,R.raw.cuatro,R.raw.cinco,
            R.raw.seis,R.raw.siete,R.raw.ocho,R.raw.nueve,R.raw.diez,R.raw.once,R.raw.doce,
            R.raw.trece,R.raw.catorce,R.raw.quince,R.raw.dieciseis,R.raw.diecisiete,
            R.raw.dieciocho,R.raw.diecinueve,R.raw.veinte,R.raw.veintiuno,R.raw.veintidos,
            R.raw.veintitres,R.raw.veinticuatro,R.raw.veinticinco,R.raw.veintiseis,
            R.raw.veintisiete,R.raw.veintiocho,R.raw.veintinueve,R.raw.treinta};

    GifEngine() throws IOException {
        idImagen=globals.getIdImagen();
        InputStream is = getResources().openRawResource(imagenes[idImagen]);

        if (is == null) {
            throw new IOException("No se puede cargar la imagen");
        }

        try {
            gif = Movie.decodeStream(is);
            duration = gif.duration();
        } finally {
            is.close();
        }

        when = -1;
        runnable = new Runnable() {
            @Override
            public void run() {
                animateGif();
            }
        };
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        gifHandler.removeCallbacks(runnable);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        super.onVisibilityChanged(visible);
        if (visible) {
            animateGif();
        } else {
            gifHandler.removeCallbacks(runnable);
        }
    }

    @Override
    public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        super.onSurfaceChanged(holder, format, width, height);
        scaleX = width / (1f * gif.width());
        scaleY = height / (1f * gif.height());
        animateGif();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset,
                                 float xOffsetStep, float yOffsetStep,
                                 int xPixelOffset, int yPixelOffset) {
        super.onOffsetsChanged(
                xOffset, yOffset,
                xOffsetStep, yOffsetStep,
                xPixelOffset, yPixelOffset);
        animateGif();
    }

    void animateGif() {
        tick();

        SurfaceHolder surfaceHolder = getSurfaceHolder();
        Canvas canvas = null;

        try {
            canvas = surfaceHolder.lockCanvas();

            if (canvas != null) {
                gifCanvas(canvas);
            }
        } finally {
            if (canvas != null) {
                surfaceHolder.unlockCanvasAndPost(canvas);
            }
        }

        gifHandler.removeCallbacks(runnable);

        if (isVisible()) {
            gifHandler.postDelayed(runnable, 1000L/25L);
        }
    }

    void tick() {
        if (when == -1L) {
            when = 0;
            start = SystemClock.uptimeMillis();
        } else {
            long diff = SystemClock.uptimeMillis() - start;
            when = (int) (diff % duration);
        }

    }

    void gifCanvas(Canvas canvas) {
        canvas.save();
        canvas.scale(scaleX, scaleY);
        gif.setTime(when);
        gif.draw(canvas, 0, 0);
        canvas.restore();
    }
}

}

    
asked by R.Hinostroza 25.09.2017 в 01:29
source

0 answers