2011-09-03 190 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); 
     } 
    } 
} 
}