Android在SurfaceView上相对于用户平滑旋转

时间:2012-10-27 16:01:13

标签: android surfaceview android-canvas flip

在Android中,如何在SurfaceView上为用户绘制旋转/翻转硬币。使用动画非常容易,并在ImageView中显示翻转硬币,但我要求SurfaceView上具有相同的平滑度。有没有什么方法可以在ImageView中翻动动画,然后在每个瞬间取出它的内容,然后在SurfaceView连续画出来?

1 个答案:

答案 0 :(得分:2)

您可以使用Matrix类在绘制位图上执行此操作。

类似

Paint paint = new Paint();
    paint.setAntiAlias(true);
    Matrix needleTransMatrix = new Matrix();
    needleTransMatrix.postRotate(rotationAngle,needleImage.getWidth()/2,needleImage.getHeight());
    needleTransMatrix.postTranslate( centerX+METER_OFFSET-needleImage.getWidth()/2, centerY-needleImage.getHeight());

    Matrix dialTransMatrix = new Matrix();
    dialTransMatrix.postRotate(rotationAngle,dialImage.getWidth()/2,dialImage.getHeight()/2);
    dialTransMatrix.postTranslate( METER_OFFSET,0);

    canvas.drawBitmap(bgImage, METER_OFFSET, 0, paint);
    canvas.drawBitmap(dialImage, dialTransMatrix, paint);
    canvas.drawBitmap(needleImage, needleTransMatrix, paint);

下面是我正在讨论的绘图线程

private class AnimationThread extends Thread{

    private boolean isRunning = true;
    private int destinationNeedlePosition = NEEDLE_POSITION_ENQURIES;

    public AnimationThread(int destinationNeedlePosition) {
        this.destinationNeedlePosition = destinationNeedlePosition;
    }

    public void cancelAnimation(){
        isRunning = false;
    }

    @Override
    public void run() {

        int destinationAngle = destinationNeedlePosition *45;

        while(isRunning && destinationAngle != rotationAngle){
            if(destinationAngle > rotationAngle)
                rotationAngle++;
            else
                rotationAngle--;
            postInvalidate();
            try {
                sleep(10);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}