我被卡住了。我正在尝试使用框架构建动态壁纸。我正在使用的代码无法正常工作。动态壁纸不会通过帧进行电影拍摄。它仅显示最后一帧。我应该对这段代码做些什么才能使其遍历框架?一个数组方法,如果是这样,我将如何在此代码中设置一个图像数组?

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

最佳答案

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

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

int递增器= 0;

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

1)在您的WallpaperSerEngine类中,创建一个像这样的Handler,

 private final Handler handler = new Handler();


2)同样在WallpaperSerEngine类中添加一个Runnable并调用您的drawFrame()

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

              drawFrame();
        }

    };


3)将以下行添加到drawFrame()的底部,以确保在特定时间间隔内调用runnable来绘制框架,

                handler.removeCallbacks(drawRunner);

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


4)现在更改您的drawFrame()方法。您无法以这种方式做到这一点。您必须使用一些位图数组。这很关键。 Kindle明确遵循以下步骤。

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

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


而已。您应该现在就看到相框了。如果有帮助,请接受。

10-08 17:47