【发布时间】:2010-04-08 06:09:22
【问题描述】:
我正在尝试在 Android 中旋转图像并调整其大小。我的代码如下,(取自this教程):
更新:下面添加了更多代码以获得更多建设性反馈
public class AnimatedSprite {
private Bitmap bitmap;
private double posX, posY;
private double velocityX, velocityY;
private int width, height;
private int numFrames, frameRate, curFrame;
private int startFrame, endFrame;
private Rect dstRect, srcRect;
private long lastTime, lastFrameTime;
private boolean looping;
private enum SpaceshipState {ALIVE, EXPLODE, DEAD};
public SpaceshipState curState;
private int respawnTime;
public AnimatedSprite(Context context, int id, int frames, int fps) {
this.bitmap = BitmapFactory.decodeResource(context.getResources(),id);
this.posX = 0;
this.posY = 0;
this.velocityX = 0;
this.velocityY = 0;
this.numFrames = frames;
this.frameRate = fps;
this.width = this.bitmap.getWidth() / frames;
this.height = this.bitmap.getHeight();
this.dstRect = new Rect((int)this.posX,(int)this.posY,(int)this.posX+this.width,(int)this.posY+this.height);
this.startFrame = 0;
this.endFrame = frames-1;
this.looping = true;
this.lastFrameTime = 0;
this.respawnTime = 0;
this.setFrameRect(this.startFrame);
}
.
.
//Other methods
.
.
//Returns a rotated copy of the AnimatedSprite a
public AnimatedSprite rotateSprite(AnimatedSprite originalSprite, float angle)
{
AnimatedSprite rotatedSprite = originalSprite;
int orgHeight = originalSprite.bitmap.getHeight();
int orgWidth = originalSprite.bitmap.getWidth();
//Create manipulation matrix
Matrix m = new Matrix();
// resize the bit map
m.postScale(.25f, .25f);
// rotate the Bitmap by the given angle
//Static angle for testing
m.postRotate(180);
//Rotated bitmap
Bitmap rotatedBitmap = Bitmap.createBitmap(originalSprite.bitmap, 0, 0,
orgWidth, orgHeight, m, true);
//Set the new sprite bitmap to the rotated bitmap and return
rotatedSprite.bitmap = rotatedBitmap;
return rotatedBitmap;
}
}
图像似乎可以很好地旋转,但它并没有像我预期的那样缩放。我尝试了 0 到 0.99 之间的不同值,但是当我减小比例值时,图像只会变得更加像素化,但在视觉上它仍然是相同的大小;而我试图实现的目标是在视觉上实际缩小它。我现在不知道该怎么做,感谢任何帮助。
【问题讨论】: