动态壁纸中的帧不变

时间:2013-10-30 11:28:53

标签: android live-wallpaper

目前我正在制作动态壁纸,而且我遇到了困难。实际上,我的动态壁纸只有在我触摸屏幕时才有效,但它不会持续变化。我的循环中存在问题。

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

2 个答案:

答案 0 :(得分:1)

添加此

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

在结束画框()我的问题解决了。

答案 1 :(得分:0)

您的代码中没有循环。您需要重复调​​用drawFrame()。

要实现此目的,您可以使用单独的线程进行动画制作。我建议你在GitHub上提供我的wallpaper template