I’m stuck. I’m trying to build a live wallpaper using frames. The code I’m using is not working. The live wallpaper doesn’t movie through the frames. It only shows the last frame. What should I do to this code to get it to move through its frames? A array method, if so how would I set up a array of images in this code?
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);
}
}
}
}
This Is a edit To my code. Not the solution….
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;
}
}
You have to register a Handler and make use of some threads.
But first of all add a global int like this,
int incrementer=0;
See this modified code of yours.
1) Inside your
WallpaperSerEngineclass, create a Handler like this,2) Similarly add a Runnable inside
WallpaperSerEngineclass and call yourdrawFrame(),3) Add the below lines to the bottom of your drawFrame() to ensure that your runnable is called on a certain time interval to draw the frames,
4) Now change your drawFrame() method. YOu cannot do it in this way. You have to make use of some Bitmap Arrays. This is critical. Kindle follow the below steps clearly.
5) Instead of declaring variable for each bitmap, put it in a Bitmap array .
6) Now declare a int array for your Drawable resources.
7) Now change your
WallpaperSerEngine()like this.8) And now finally a little bit modification in your drawFrame(),
That’s it. You should be seeing your frames now. And accept it if this is helpful.