2

在 Android 中,如何在SurfaceView. 使用动画并在 中显示抛硬币非常容易ImageView,但我要求它SurfaceView具有相同的平滑度。有没有什么办法可以让我ImageView在每一个瞬间取出它的内容然后SurfaceView连续绘制它?

4

1 回答 1

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

}
于 2012-10-27T16:04:18.107 回答