我被困住了。我正在尝试使用框架构建动态壁纸。我正在使用的代码不起作用。动态壁纸不会通过帧播放。它只显示最后一帧。我该怎么做才能让它在框架中移动?数组方法,如果是这样,我将如何在此代码中设置图像数组?
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;
}
}
答案 0 :(得分:3)
您必须注册一个处理程序并使用一些线程。
但首先要添加一个像这样的全局int,
int incrementer = 0;
请参阅此修改后的代码。
1)在WallpaperSerEngine
课程中,创建一个像这样的处理程序,
private final Handler handler = new Handler();
2)同样在WallpaperSerEngine
课程中添加可运行并致电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);
}
}
}
就是这样。你现在应该看到你的镜架了。如果这有用,请接受它。