2014-07-19 17 views
3

我不需要隨手機移動的花式牆紙,我只想使用GIF作爲移動的「現場」壁紙。這是可能的,如果有的話,任何人都可以向我解釋它是如何完成的,或者將我指向一個資源來演示如何?我一直無法找到一個。如何將GIF變成動態壁紙以便在Android應用中使用

+0

如果你有幫助,從我的回答不是不接受的答案,以便其他可以從你的貢獻:) – AndruBoy

回答

5

GIFLiveWallpaper演示看着外面的GitHub庫

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

甜得到幫助!非常感謝:) – djinne95

+0

所以,爲了澄清一下,我可以將資源「sam.png」更改爲gif,並且將作爲LiveWallpaper? :) – djinne95

+0

是的,這是你想要作爲活牆紙動畫的文件 – AndruBoy