0

Tengo este código:

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 7/12/2015.
 */
public class GIFWallpaperService extends WallpaperService{
    //static final String TAG = "gifService";
    static final 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();
        }
    }
}

Tengo un activity de inicio el cual me envía un número de índice para elegir el wallpaper que se mostrara como fondo de pantalla, bueno al ejecutar la aplicación en el emulador se elije y establece normalmente el wallpaper pero si deseo cambiar abro la aplicación otra vez y elijo y supuestamente aplico el nuevo gif pero se mantiene el que esta no cambia nada.

¿En qué estoy fallando?

11
  • Donde recibes el número del índice que envias desde el activity? el 23 sep. 2017 a las 3:50
  • Puedes agregar el codigo de la clase Globals. el 23 sep. 2017 a las 4:04
  • public class Globals { private static Globals instance; private static int idImagen; public int getIdImagen() { return Globals.idImagen; } public void setIdImagen(int t) { Globals.idImagen = t; } public static synchronized Globals getInstance(){ if (instance==null){ instance=new Globals(); } return instance; } } el 23 sep. 2017 a las 5:02
  • de mi activity principal le envio un valor a la variable declarada en la clase Globals (setIdImagen()), y desde la clase GIFWallpaperService leo ese valor (getIdImagen()) y lo indexo al array imagenes[] el 23 sep. 2017 a las 5:05
  • Ya has comprobado colocando la posición manualmente InputStream is = getResources().openRawResource(imagenes[5]); el 23 sep. 2017 a las 10:54

0

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

Examina otras preguntas con la etiqueta o formula tu propia pregunta.