I'm trying to program for that to happen, but only appears to the user to 
define the application, which is a wallpaper.
Follows the line of code, what am I doing wrong?

package --------------

import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Movie;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;
import android.widget.Toast;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import java.io.IOException;

/**
 * Created by N on 10/06/2016.
 */
public class ServicoWallpaper extends WallpaperService {
    private InterstitialAd interstitial;
    public static boolean previousPreview = false;
    //chama o método que converte o gif em vídeo
    //sobrescrece a engine com o método try
    @Override
    public WallpaperService.Engine onCreateEngine() {
        try {

            Movie movie = Movie.decodeStream(
                    getResources().getAssets().open("petals.gif"));
            return new GIFWallpaperEngine(movie);
        }catch(IOException e){
            Log.d("GIF", "Não foi possível carregar a imagem");
            return null;
        }
    }
    //aqui começa o código que anima o gif
    public class GIFWallpaperEngine extends WallpaperService.Engine {
        private final int frameDuration = 20;

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

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

        @Override
        public void onCreate(SurfaceHolder surfaceHolder) {
            super.onCreate(surfaceHolder);
            this.holder = surfaceHolder;
        }
        //cria o objeto do tipo Runnable que chama o método draw
        private Runnable drawGIF = new Runnable() {
            public void run() {
                draw();
            }
        };

        //este método desenha o gif e o escala de acordo com o tamanho da tela
        private void draw() {
            if (visible) {
                Canvas canvas = holder.lockCanvas();
                canvas.save();
                // Ajuste o tamanho e posição
                // até a imagem ficar boa na tela do aparelho
                // aqui é a escala da imagem em relação a resolução do aparelho
                // 1f = tamanho normal do gif
                canvas.scale((float) 
(Resources.getSystem().getDisplayMetrics().widthPixels/(movie.width()*1.0)), 
(float) 
(Resources.getSystem().getDisplayMetrics().heightPixels/(movie.height()*1.0)));
                //aqui é o alinhamento horizontal e vertical
                //os valores podem ser positivos ex: -100, 0, +100
                //o primeiro numero é o alinhamento horizontal
                //o segundo numeor é o alinhamento  vertical

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

                handler.removeCallbacks(drawGIF);
                handler.postDelayed(drawGIF, frameDuration);
            }
        }
        //verifica se o plano de fundo está visível para mostrar/esconder o gif
        @Override
        public void onVisibilityChanged(boolean visible) {

            System.out.println(isPreview()+ "  "+visible);
            if(!isPreview() && visible && previousPreview){
                previousPreview =false;
                interstitial = new InterstitialAd(getApplicationContext());
                
interstitial.setAdUnitId("ca-app-pub-5579648702298902/2848470655");
                AdRequest adRequest = new AdRequest.Builder()
                        .build();
                interstitial.setAdListener(new AdListener() {
                    public void onAdLoaded() {
                        if (interstitial.isLoaded()) {
                            interstitial.show();
                        }
                    }
                    @Override
                    public void onAdFailedToLoad(int errorCode) {
                        Toast.makeText(getApplicationContext(), "Ad failed to 
load! error code: " + errorCode, Toast.LENGTH_SHORT).show();
                    }
                });
                interstitial.loadAd(adRequest);
                Toast.makeText(getApplicationContext(), "set", 
Toast.LENGTH_SHORT).show();
            }


            if(isPreview()){
                previousPreview = true;
            }else{
                previousPreview = false;
            }

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

        }
    }

}

-- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/android-developers/450251fa-4b3c-4478-98d1-1fb88429736a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to