gpt4 book ai didi

android - 制作动态壁纸

转载 作者:行者123 更新时间:2023-11-29 18:11:05 24 4
gpt4 key购买 nike

我卡住了。我正在尝试使用框架制作动态壁纸。我正在使用的代码不起作用。动态壁纸不会通过帧拍摄电影。它只显示最后一帧。我应该如何处理此代码以使其在其框架中移动?一个数组方法,如果是这样,我将如何在此代码中设置图像数组?

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,

整数增量=0;

查看您修改后的代码。

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

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

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

关于android - 制作动态壁纸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10677698/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com