1

我被困住了。我正在尝试使用框架构建动态壁纸。我正在使用的代码不起作用。动态壁纸不会通过帧拍摄。它只显示最后一帧。我应该对这段代码做些什么来让它在它的框架中移动?数组方法,如果是这样,我将如何在此代码中设置图像数组?

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, image2, image3, image4, image5, image6, image7, image8, image9,image10,
        image11, image12, image13, image14, image15, image16, image17, image18, image19,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);
            }
        }
    }
}

这是对我的代码的编辑。不是解决方案....

 import android.graphics.Bitmap;

 import android.graphics.BitmapFactory;

 import android.graphics.Canvas;

 import android.os.Handler;

 import android.service.wallpaper.WallpaperService;

 import android.view.SurfaceHolder;

 public class WallpaperSer extends WallpaperService {

int incrementer=0;
Bitmap bmps[]=new Bitmap[10];

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

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

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

class WallpaperSerEngine extends Engine 
{

    int res[]={R.drawable.and1,R.drawable.and2,R.drawable.and3,R.drawable.and4,R.drawable.and5,R.drawable.and6,R.drawable.and7,R.drawable.and8,R.drawable.and9,R.drawable.and10};
    WallpaperSerEngine() 
    {  
          for(int i=0;i<=10;i++)
              {
                    bmps[i]= BitmapFactory.decodeResource(getResources(),res[i]);                     
               }
    }
}

    private final Handler handler = new Handler();
     private final Runnable drawRunner = new Runnable() {
            @Override
            public void run() {

                  drawFrame();
                  handler.removeCallbacks(drawRunner);

                  handler.postDelayed(drawRunner, 200);
            }

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

            Canvas c = null;
            try 
            {
                c = holder.lockCanvas();
                if (c != null) 
                {              

                     c.drawBitmap(bmps[incrementer], 0, 0, null);

                    incrementer=(incrementer==10)?0 : incrementer+1; 
                }
            } finally 
            {
                if (c != null) holder.unlockCanvasAndPost(c);
            }
        }

        private SurfaceHolder getSurfaceHolder() {
            // TODO Auto-generated method stub
            return null;
        }
    }
4

1 回答 1

3

您必须注册一个处理程序并使用一些线程。

但首先像这样添加一个全局int,

整数增量=0;

请参阅您的修改后的代码。

1)在你的 WallpaperSerEngine类中,创建一个像这样的处理程序,

 private final Handler handler = new Handler(); 

2)类似地在类中添加一个RunnableWallpaperSerEngine并调用你的 drawFrame()

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

              drawFrame();              
        }

    };

3) 将以下行添加到您的 drawFrame() 的底部,以确保您的可运行文件在特定时间间隔内被调用以绘制帧,

                handler.removeCallbacks(drawRunner);

        handler.postDelayed(drawRunner, 200); //delay milliseconds 200. Change it for your need.

4)现在改变你的drawFrame()方法。你不能这样做。您必须使用一些位图数组。这很关键。Kindle 清楚地遵循以下步骤。

5)不要为每个位图声明变量,而是将其放入位图数组中。

Bitmap bmps[]=new Bitmap[10]; // assign its size based on your need. I just need 10 for demo.

6)现在为您的 Drawable 资源声明一个 int 数组。

   int res[]={R.drawable.and1,R.drawable.and2,R.drawable.and3,R.drawable.and4,R.drawable.and5,R.drawable.and6,R.drawable.and7,R.drawable.and8,R.drawable.and9,R.drawable.and10};

7)现在改变你的WallpaperSerEngine()样子。

WallpaperSerEngine() 
        {  
              for(int i=0;i<=10;i++)
                  {
                        bmps[i]= BitmapFactory.decodeResource(getResources(),res[i]);                     
                   }
        }

8)现在终于在您的drawFrame()中进行了一些修改,

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

            Canvas c = null;
            try 
            {
                c = holder.lockCanvas();
                if (c != null) 
                {              

                     c.drawBitmap(bmps[incrementer], 0, 0, null);

                    incrementer=(incrementer==10)?0 : incrementer+1; 
                }
            } finally 
            {
                if (c != null) holder.unlockCanvasAndPost(c);
            }
        }
    }

就是这样。你现在应该看到你的框架了。如果这有帮助,请接受它。

于 2012-05-21T04:53:23.420 回答