2011-09-03 194 views
4

(對不起,我的英語不太好...希望你能理解我)動態壁紙與圖像

我的朋友是一個非常好的抽屜。我想幫助他通過使他成爲一個動畫與他的圖紙動態壁紙知道作爲一個很好的抽屜。

我想讓他畫幾幀,並使用這些幀來製作一個接一個地顯示它們的動態壁紙。

我很努力地顯示一張圖片,然後等待一下,然後顯示下一張圖片。我敢肯定,我不能成功做到這一點,因爲我不使用正確的方法...

這是我到現在爲止完成:

public class Cercle extends WallpaperService 
{ 
public void onCreate() 
{ 
    super.onCreate(); 
} 

public void onDestroy() 
{ 
    super.onDestroy(); 
} 

public Engine onCreateEngine() 
{ 
    return new CercleEngine(); 
} 

class CercleEngine extends Engine 
{ 
    public Bitmap image1, image2, image3; 

    CercleEngine() 
    {  
     image1 = BitmapFactory.decodeResource(getResources(), R.drawable.img1); 
     image2 = BitmapFactory.decodeResource(getResources(), R.drawable.img2); 
     image3 = BitmapFactory.decodeResource(getResources(), R.drawable.img3); 
    } 

    public void onCreate(SurfaceHolder surfaceHolder) 
    { 
     super.onCreate(surfaceHolder); 
    } 

    public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
    { 
     drawFrame(); 
    } 

    void drawFrame() 
    { 
     final SurfaceHolder holder = getSurfaceHolder(); 

     Canvas c = null; 
     try 
     { 
      c = holder.lockCanvas(); 
      if (c != null) 
      {    
       c.drawBitmap(image1, 0, 0, null); 
       c.drawBitmap(image2, 0, 0, null); 
       c.drawBitmap(image3, 0, 0, null);     
      } 
     } finally 
     { 
      if (c != null) holder.unlockCanvasAndPost(c); 
     } 
    } 
} 
} 

此代碼只是顯示圖片太快,因爲我不知道如何等待圖片之間的間隔...

任何人都可以給我一些提示或給我看一些另一種解決方案的例子嗎?

非常感謝!

UPDATE:

我得到了我的問題,通過增加一個Runnable解決:

private final Runnable drawRunner = new Runnable() 
    { 
     @Override 
     public void run() { 
      drawFrame(); 
     } 

    }; 

,然後通過添加:

handler.removeCallbacks(drawRunner); 
     if (visible) 
     { 
      handler.postDelayed(drawRunner, 1000); // delay 1 sec 
     } 

在並條機的結束()。

希望這會幫助別人。

回答

6

使用這個開源項目創建漂亮的動態壁紙爲你的朋友

link它是從手機圖庫閱讀,您可以編輯它從文件夾中的項目公關閱讀只是用,因爲它是。

或使用該link其他活的壁紙項目

+0

謝謝艾哈邁德!我通過查看鏈接中的源代碼來解決問題。對不起,我想給你投票,但它告訴我,我需要15聲望才能做到這一點... – Pozinux

+0

歡迎隨時 – Alex

-1

@Pozinux ......我是一個新的程序員。我遇到了你聽到的這段代碼。我試圖在我的項目中使用代碼。但我無法讓它工作。當我將它安裝到仿真器時,它不會通過它的框架移動。它只顯示最後一幀。你是怎麼設置的?或者讓這個工作?聽聽我如何在我的項目中進行設置。

import android.graphics.Bitmap; 

import android.graphics.BitmapFactory; 

import android.graphics.Canvas; 

import android.service.wallpaper.WallpaperService; 

import android.view.SurfaceHolder; 

public class WallpaperSer extends WallpaperService { 

public void onCreate() 

{ 
    super.onCreate(); 
} 

public void onDestroy() 
{ 
    super.onDestroy(); 
} 

public Engine onCreateEngine() 
{ 
    return new WallpaperSerEngine(); 
} 

class WallpaperSerEngine extends Engine 
{ 
    public Bitmap image1; 
    public Bitmap image2; 
    public Bitmap image3; 
    public Bitmap image4; 
    public Bitmap image5; 
    public Bitmap image6; 
    public Bitmap image7; 
    public Bitmap image8; 
    public Bitmap image9; 
    public Bitmap image10; 
    public Bitmap image11; 
    public Bitmap image12; 
    public Bitmap image13; 
    public Bitmap image14; 
    public Bitmap image15; 
    public Bitmap image16; 
    public Bitmap image17; 
    public Bitmap image18; 
    public Bitmap image19; 
    public Bitmap image20; 

    WallpaperSerEngine() 
    {  
      image1 = BitmapFactory.decodeResource(getResources(), R.drawable.and1); 
      image2 = BitmapFactory.decodeResource(getResources(), R.drawable.and2);  
      image3 = BitmapFactory.decodeResource(getResources(), R.drawable.and3); 
      image4 = BitmapFactory.decodeResource(getResources(), R.drawable.and4); 
      image5 = BitmapFactory.decodeResource(getResources(), R.drawable.and5); 
      image6 = BitmapFactory.decodeResource(getResources(), R.drawable.and6); 
      image7 = BitmapFactory.decodeResource(getResources(), R.drawable.and7); 
      image8 = BitmapFactory.decodeResource(getResources(), R.drawable.and8); 
      image9 = BitmapFactory.decodeResource(getResources(), R.drawable.and9); 
      image10 = BitmapFactory.decodeResource(getResources(), R.drawable.and10); 
      image11 = BitmapFactory.decodeResource(getResources(), R.drawable.and11); 
      image12 = BitmapFactory.decodeResource(getResources(), R.drawable.and12); 
      image13 = BitmapFactory.decodeResource(getResources(), R.drawable.and13); 
      image14 = BitmapFactory.decodeResource(getResources(), R.drawable.and14); 
      image15 = BitmapFactory.decodeResource(getResources(), R.drawable.and15); 
      image16 = BitmapFactory.decodeResource(getResources(), R.drawable.and16); 
      image17 = BitmapFactory.decodeResource(getResources(), R.drawable.and17); 
      image18 = BitmapFactory.decodeResource(getResources(), R.drawable.and18); 
      image19 = BitmapFactory.decodeResource(getResources(), R.drawable.and19); 
      image20 = BitmapFactory.decodeResource(getResources(), R.drawable.and20); 
    } 

    public void onCreate(SurfaceHolder surfaceHolder) 
    { 
     super.onCreate(surfaceHolder); 
    } 

    public void onOffsetsChanged(float xOffset, float yOffset, float xStep, float yStep, int xPixels, int yPixels) 
    { 
     drawFrame(); 

    } 

    void drawFrame() 
    { 
     final SurfaceHolder holder = getSurfaceHolder(); 

     Canvas c = null; 
     try 
     { 
      c = holder.lockCanvas(); 
      if (c != null) 
      {    
       c.drawBitmap(image1, 0, 0, null); 
       c.drawBitmap(image2, 0, 0, null); 
       c.drawBitmap(image3, 0, 0, null); 
       c.drawBitmap(image4, 0, 0, null); 
       c.drawBitmap(image5, 0, 0, null); 
       c.drawBitmap(image6, 0, 0, null); 
       c.drawBitmap(image7, 0, 0, null); 
       c.drawBitmap(image8, 0, 0, null); 
       c.drawBitmap(image9, 0, 0, null); 
       c.drawBitmap(image10, 0, 0, null); 
       c.drawBitmap(image11, 0, 0, null); 
       c.drawBitmap(image12, 0, 0, null); 
       c.drawBitmap(image13, 0, 0, null); 
       c.drawBitmap(image14, 0, 0, null); 
       c.drawBitmap(image15, 0, 0, null); 
       c.drawBitmap(image16, 0, 0, null); 
       c.drawBitmap(image17, 0, 0, null); 
       c.drawBitmap(image18, 0, 0, null); 
       c.drawBitmap(image19, 0, 0, null); 
       c.drawBitmap(image20, 0, 0, null); 
      } 
     } finally 
     { 
      if (c != null) holder.unlockCanvasAndPost(c); 
     } 
    } 
} 
}