在android surfaceview中平滑动画

时间:2014-03-25 14:53:16

标签: java android animation surfaceview

我已经在Android上创建了一个俄罗斯方块游戏,我不认为动画是尽可能好的。当块快速移动(在刻度之间100ms)时,它在动画中是一个非常明显的闪烁,好像在非常短的时间内你可以看到移动块的先前位置的痕迹。

这是动画循环,在一个单独的线程中实现,作为SurfaceView的扩展:

public void run() 
{       
    while(running)
    {
        if(surfaceHolder.getSurface().isValid()) 
        {
            sleep(1);
            canvas = surfaceHolder.lockCanvas();

            canvas.drawColor(Color.BLACK); //reset canvas


            int width = canvas.getWidth();
            int squareSpaceW = (width/4)*3;
            int squareSize = squareSpaceW/10;
            squareSpaceW = squareSize*10;
            int squareSpaceH = squareSpaceW*2;


            //Draw lines on the play field
            canvas.drawLine(squareSpaceW, 0, squareSpaceW, squareSpaceH, whitePaint);
            canvas.drawLine(0, squareSpaceH, squareSpaceW, squareSpaceH, whitePaint);

            //draw next piece
            int nxtPieceSize = board.getNextPiece().getBlocks().size() -1;
            while(nxtPieceSize >= 0) 
            {
                rectPaint.setColor(Color.parseColor(board.getNextPiece().getBlocks().get(nxtPieceSize).getColor()));
                int x = board.getNextPiece().getCoordList().get(nxtPieceSize).getX();
                int y = board.getNextPiece().getCoordList().get(nxtPieceSize).getY();
                nxtPieceSize--;
                rect.set(x*(squareSize/2) + (squareSpaceW/17)*16, y*(squareSize/2) + (squareSize/2), x*(squareSize/2) + (squareSize/2) + (squareSpaceW/17)*16, y*(squareSize/2)+ (squareSize/2) + (squareSize/2));
                canvas.drawRect(rect, rectPaint);
            }

            //draw text
            canvas.drawText("Score: " + Integer.toString(board.getScore()), width - (width/8), (width/8) + (squareSize), whitePaint);
            canvas.drawText("Level: " + Integer.toString(board.getLevel()), width - (width/8), (width/8) + (squareSize*2), whitePaint);

            //draw the blocks
            for(int x = 0; x < board.getWidth(); x++) 
            {
                for(int y = 0; y < board.getHeight(); y++)
                {
                    tempBlock = board.getBlock(x, y);
                    if(tempBlock != null)
                    {
                        rectPaint.setColor(Color.parseColor(tempBlock.getColor()));
                        rect.set(x*squareSize, y*squareSize, x*squareSize + squareSize, y*squareSize+ squareSize);
                        canvas.drawRect(rect, rectPaint);

                    }
                }
            }

            //draw pause icon
            rect.set(squareSize/2,squareSize/2, (squareSize/4)*3, squareSize + squareSize/2);
            canvas.drawRect(rect, whitePaint);  
            rect.set(squareSize ,squareSize/2, squareSize + squareSize/4, squareSize + squareSize/2);
            canvas.drawRect(rect, whitePaint);


            surfaceHolder.unlockCanvasAndPost(canvas);
        }
    }
}

希望我已经提供了您需要的信息。对不起,如果代码有点乱......

0 个答案:

没有答案