带有图像的动态壁纸

发布于 2024-12-02 22:58:24 字数 2161 浏览 1 评论 0原文

(抱歉我的英语不太好......希望你能理解我)

我的朋友是一个非常好的抽屉。我想通过为他制作一张用他的画作动画的动态壁纸来帮助他成为一名优秀的抽屉。

我希望他画几帧,然后用这些帧逐一显示来制作动态壁纸。

我非常努力地显示一张图片,然后稍等一下并显示下一张。我很确定我无法成功做到这一点,因为我没有使用正确的方法...

这就是我到目前为止所做的:

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

    };

然后

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

在 drawFrame() 末尾添加 : 解决了我的问题。

希望这会对某人有所帮助。

(sorry for my Englih is not so good... hope you will understand me)

My friend is a really good drawer. I would like to help him to be know as a good drawer by making him a live wallpaper animated with his drawings.

I would like him to draw few frames and use these frames to make a live wallpaper by displaying them one after the other.

I'm struggling so much to display one picture then wait a bit and display the next one. I'm quite sure that I can't succeed to do it because I don't use the right approach...

This is what I have done until now :

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

This code just display the pictures too quickly because I don't know how to wait between diplaying pictures...

Can anyone give me some tips or show me some example of another solution ?

Thanks so much !

UPDATE :

I got my problem resolved by adding a Runnable :

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

    };

and then by adding :

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

at the end of drawFrame().

Hope this will help someone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↘紸啶 2024-12-09 22:58:25

使用这个开源项目为您的朋友创建漂亮的动态壁纸

链接它是从手机图库中读取的,您可以编辑它以从项目中的文件夹中读取,然后按原样使用。

或使用此链接进行其他动态壁纸项目

use this open source project to create nice live wallpaper for your friend

link it is read from phone gallery, you can edit it to read from folder in the project pr just use as it is.

or use this link for other live wallpaper project

迷乱花海 2024-12-09 22:58:25

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

@Pozinux... I'm a new programmer. I came across this code you have hear. I'm trying to use the code in my project. But I can't get it to work. When I install it to the emulator it doesn't move through its frames. It only shows the last frame. How did you set this up? Or get this to work? Hears how I set it up in my project.

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);
        }
    }
}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文