与用户相对于用户,Android在SurfaceView上平稳旋转



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

您可以使用矩阵类在绘图位图上完善这些操作。

之类的东西
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();
            }
        }
    }
}

相关内容

最新更新