java图形旋转动画_Java动画:旋转图像

我将假设您了解如何旋转图像一次.如果你不这样做,你可以通过快速谷歌搜索找到它.

您需要的是一个为您旋转它的后台进程.它的工作原理如下:

/**

* Warning - this class is UNSYNCHRONIZED!

*/

public class RotatableImage {

Image image;

float currentDegrees;

public RotateableImage(Image image) {

this.image = image;

this.currentDegrees = 0.0f;

this.remainingDegrees = 0.0f;

}

public void paintOn(Graphics g) {

//put your code to rotate the image once in here, using current degrees as your rotation

}

public void spin(float additionalDegrees) {

setSpin(currentDegrees + additionalDegrees);

}

public void setSpin(float newDegrees) {

currentDegrees += additionalDegrees;

while(currentDegrees < 0f) currentDegrees += 360f;

while(currentDegrees >= 360f) currentDegrees -= 360f;

}

}

public class ImageSpinner implements Runnable {

RotateableImage image;

final float totalDegrees;

float degrees;

float speed; // in degrees per second

public ImageSpinner(RotatableImage image, float degrees, float speed) {

this.image = image;

this.degrees = degrees;

this.totalDegrees = degrees;

this.speed = speed;

}

public void run() {

// assume about 40 frames per second, and that the it doesn't matter if it isn't exact

int fps = 40;

while(Math.abs(degrees) > Math.abs(speed / fps)) { // how close is the degrees to 0?

float degreesToRotate = speed / fps;

image.spin(degreesToRotate);

degrees -= degreesToRotate;

/* sleep will always wait at least 1000 / fps before recalcing

but you have no guarantee that it won't take forever! If you absolutely

require better timing, this isn't the solution for you */

try { Thread.sleep(1000 / fps); } catch(InterruptedException e) { /* swallow */ }

}

image.setSpin(totalDegrees); // this might need to be 360 - totalDegrees, not sure

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值