0

Currently I am working on live wallpaper and I am stuck at a point. Actually, my live wallpaper only works when I touch the screen, but it doesn't change continuously. There is a problem in my loop.

class Diwali extends Engine  {
    private boolean mVisible;

    private final Runnable diwaliImg = new Runnable() {
        public void run() {
            drawFrame();
        }
    };

    int i=0;
    int[] pirates = {
        R.drawable.a1, R.drawable.a2,
        R.drawable.a3, R.drawable.a4,
        R.drawable.a5, R.drawable.a6,
        R.drawable.a7, R.drawable.a8,
        R.drawable.a9, R.drawable.a10,
        R.drawable.a11, R.drawable.a12,
        R.drawable.a13, R.drawable.a14
    };

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

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

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

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

    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceCreated(holder);
    }

    @Override
    public void onSurfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        super.onSurfaceDestroyed(holder);
        mVisible = false;
        mHandler.removeCallbacks(diwaliImg);
    }


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

    @Override
    public void onTouchEvent(MotionEvent event) {
        super.onTouchEvent(event);
    }

    private void drawFrame() {
        // TODO Auto-generated method stub
        final SurfaceHolder holder = getSurfaceHolder();
        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                drawPirate(c);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
        mHandler.removeCallbacks(diwaliImg);
    }

    private void drawPirate(Canvas c) {
        // TODO Auto-generated method stub
        Bitmap icon = BitmapFactory.decodeResource(getResources(),pirates[i]);

        i++;

        if (i == 13) {
            i = 0;
        }
        Matrix matrix = new Matrix();

        c.drawBitmap(icon, matrix, null);
        icon.recycle();
    }
}
Alvin Wong
  • 12,210
  • 5
  • 51
  • 77
jam
  • 11
  • 5

2 Answers2

1

After adding this

if (mVisible) { Handler.postDelayed(diwaliImg,80); }

At the end draw frame() my problem was solved.

jam
  • 11
  • 5
0

There is no loop in your code. You need to call drawFrame() repeatedly.

To achieve this you may use separate thread for animation. I recommend you my wallpaper template available on GitHub.

Chiral Code
  • 1,426
  • 2
  • 12
  • 12
  • What do you mean? Use my template and replace content of Scene.draw() method with the content of your drawPirate() method. Of course you need to load bitmaps earlier. – Chiral Code Oct 31 '13 at 07:26
  • Dear I found the solution.I am posting my code.well thanx for your help :) – jam Oct 31 '13 at 07:55