2014-07-19 1 views
3

Je n'ai pas besoin d'un fond d'écran fantaisie qui se déplace avec votre téléphone, je voudrais juste utiliser un gif comme un fond d'écran animé. Est-ce possible, et si oui, est-ce que quelqu'un pourrait m'expliquer comment cela est fait, ou me diriger vers une ressource qui montre comment? J'ai été incapable d'en trouver un pour le moment.Comment transformer un gif en un fond d'écran en direct pour une utilisation dans l'application Android

+0

si vous avez l'aide de ma réponse que ne pas accepter réponse afin que les autres peuvent obtenir l'aide de votre contribution :) – AndruBoy

Répondre

5

Voir GIFLiveWallpaper démo regarder à github repo

public class GifLiveWallPaper extends WallpaperService { 

static final String TAG = "LIVE_WALLPAPER"; 
static final Handler liveHandler = new Handler(); 

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

class WallPaperEngine extends Engine { 

    private Movie liveMovie; 
    private int duration; 
    private Runnable runnable; 
    float mScaleX; 
    float mScaleY; 
    int mWhen; 
    long mStart; 

    public WallPaperEngine() throws IOException { 

     InputStream is = getResources().openRawResource(R.raw.sam); 

     if (is != null) { 

      try { 
       liveMovie = Movie.decodeStream(is); 
       duration = liveMovie.duration(); 

      } finally { 
       is.close(); 
      } 
     } else { 
      throw new IOException("Unable to open R.raw.hand"); 
     } 
     mWhen = -1; 
     runnable = new Runnable() { 
      public void run() { 
       nyan(); 
      } 
     }; 
    } 

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

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

    @Override 
    public void onSurfaceChanged(SurfaceHolder holder, int format, 
      int width, int height) { 
     super.onSurfaceChanged(holder, format, width, height); 
     mScaleX = width/(1f * liveMovie.width()); 
     mScaleY = height/(1f * liveMovie.height()); 
     nyan(); 
    } 

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

    void nyan() { 
     tick(); 
     SurfaceHolder surfaceHolder = getSurfaceHolder(); 
     Canvas canvas = null; 
     try { 
      canvas = surfaceHolder.lockCanvas(); 
      if (canvas != null) { 
       drawGif(canvas); 
      } 
     } finally { 
      if (canvas != null) { 
       surfaceHolder.unlockCanvasAndPost(canvas); 
      } 
     } 
     liveHandler.removeCallbacks(runnable); 
     if (isVisible()) { 
      liveHandler.postDelayed(runnable, 1000L/25L); 
     } 
    } 

    void tick() { 
     if (mWhen == -1L) { 
      mWhen = 0; 
      mStart = SystemClock.uptimeMillis(); 
     } else { 
      long mDiff = SystemClock.uptimeMillis() - mStart; 
      mWhen = (int) (mDiff % duration); 
     } 
    } 

    void drawGif(Canvas canvas) { 
     canvas.save(); 
     canvas.scale(mScaleX, mScaleY); 
     liveMovie.setTime(mWhen); 
     liveMovie.draw(canvas, 0, 0); 
     canvas.restore(); 
    } 
} 
} 
+1

Sweet! Merci beaucoup :) – djinne95

+0

Donc juste pour clarifier, je peux changer la ressource "sam.png" à un gif, et cela agira comme le LiveWallpaper? :) – djinne95

+0

oui c'est le fichier que vous voulez animer en tant que papier peint – AndruBoy

Questions connexes