具有图像的动态壁纸

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);
        }
    }
}
}

这段代码显示图片过快,因为我不知道如何在显示图片时等待时间…有人能给我一些提示或者展示另一种解决方案的例子吗?非常感谢!更新:通过添加Runnable来解决了我的问题:
private final Runnable drawRunner = new Runnable() 
    {
        @Override
        public void run() {
            drawFrame();
        }

    };

然后通过添加:<\p>
    handler.removeCallbacks(drawRunner);
        if (visible) 
        {
           handler.postDelayed(drawRunner, 1000); // delay 1 sec
        }

在drawFrame()的末尾。
希望这能对某些人有所帮助。
2个回答

6
使用这个开源项目为你的朋友创建漂亮的动态壁纸。 链接它从手机相册读取,你可以编辑它以从项目文件夹中读取,或者直接使用它。 或者使用这个链接来制作其他动态壁纸。

谢谢Ahmed!通过查看您提供的链接源代码,我解决了我的问题。很抱歉我想给您点赞,但它告诉我我需要15个声望才能这样做... - Pozinux

-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);
        }
    }
}
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接