[General Questions and Answers] azaghalh:
Hi !!!

Firstly sorry for my bad english !!

I try to develop a Live Wallpaper with frames animation.

What I coded work but it need to be improved. Performances are very bad !!!

What could I do ?

I send you the eclipse project ;)

package ca.jvsh.livewallpaper;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.os.Handler;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

public class LiveWallpaper extends WallpaperService
{

public static final String	SHARED_PREFS_NAME	= "livewallpapersettings";
private final Handler mHandler = new Handler();
public Bitmap myBg;
public int idx = 0;
public static final int NUM_RES = 20;

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

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

@Override
public Engine onCreateEngine() {
return new CubeEngine();
}

class CubeEngine extends Engine {

private final Paint mPaint = new Paint();
private boolean mAnime = true;
private Matrix mMatrix = new Matrix();

private final Runnable mDrawAnim = new Runnable() {
@Override
public void run() {
drawFrame();
}
};
private boolean mVisible;

CubeEngine() {
myBg = BitmapFactory.decodeResource(getResources(),R.drawable.n01);
}

@Override
public void onCreate(SurfaceHolder surfaceHolder) {
super.onCreate(surfaceHolder);

setTouchEventsEnabled(false);
}

@Override
public void onDestroy() {
super.onDestroy();
mHandler.removeCallbacks(mDrawAnim);
}

@Override
public void onVisibilityChanged(boolean visible) {
mVisible = visible;
if (visible) {
drawFrame();
} else {
mHandler.removeCallbacks(mDrawAnim);
}
}

@Override
public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
super.onSurfaceChanged(holder, format, width, height);

float w = myBg.getWidth();
float s = width / w;
mMatrix.reset();
mMatrix.setScale(s, s);
drawFrame();
}

@Override
public void onSurfaceCreated(SurfaceHolder holder) {
super.onSurfaceCreated(holder);
}

@Override
public void onSurfaceDestroyed(SurfaceHolder holder) {
super.onSurfaceDestroyed(holder);
mVisible = false;
mHandler.removeCallbacks(mDrawAnim);
}

@Override
public void onOffsetsChanged(float xOffset, float yOffset,
float xStep, float yStep, int xPixels, int yPixels) {
drawFrame();
}

@Override
public void onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
mAnime = !mAnime;
}
super.onTouchEvent(event);
}

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

Canvas c = null;
try {
c = holder.lockCanvas();
if (c != null) {
// draw something
drawAnim(c);
//drawTouchPoint(c);
}
} finally {
if (c != null) holder.unlockCanvasAndPost(c);
}

// Reschedule the next redraw
mHandler.removeCallbacks(mDrawAnim);
if (mVisible && mAnime) {
mHandler.postDelayed(mDrawAnim, 50 );
}
}

void drawAnim(Canvas c) {

if (idx == NUM_RES) {idx = 0;}
idx += 1;
switch (idx) {
case 0: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n01), mMatrix, mPaint); break;
case 1: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n02), mMatrix, mPaint); break;
case 2: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n03), mMatrix, mPaint); break;
case 3: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n04), mMatrix, mPaint); break;
case 4: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n05), mMatrix, mPaint); break;
case 5: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n06), mMatrix, mPaint); break;
case 6: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n07), mMatrix, mPaint); break;
case 7: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n08), mMatrix, mPaint); break;
case 8: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n09), mMatrix, mPaint); break;
case 9: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n10), mMatrix, mPaint); break;
case 10: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n11), mMatrix, mPaint); break;
case 11: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n12), mMatrix, mPaint); break;
case 12: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n13), mMatrix, mPaint); break;
case 13: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n14), mMatrix, mPaint); break;
case 14: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n15), mMatrix, mPaint); break;
case 15: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n16), mMatrix, mPaint); break;
case 16: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n17), mMatrix, mPaint); break;
case 17: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n18), mMatrix, mPaint); break;
case 18: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n19), mMatrix, mPaint); break;
case 19: c.drawBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.n20), mMatrix, mPaint); break;

}

}

}
}