调整大小和旋转Android中的图像
问题描述:
我正在尝试在Android中旋转和调整图像大小。我的代码如下,(从this教程获取):调整大小和旋转Android中的图像
UPDATE:更多添加以下代码更具建设性的反馈
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和.99之间的不同值,但图像只是在缩小比例值时获得更多像素化,但它在视觉上保持相同的大小;而我试图实现的目标是实际缩小视觉。我不知道该怎么做,任何帮助表示赞赏。
答
你正在做的是从位图操纵像素数据。您正在减少图像中的像素数量,但听起来像(因为我们无法看到此代码),就好像位图被缩放以适合屏幕上的相同大小。为了减少屏幕尺寸,您必须调整ImageView上的参数,或者您使用任何技术来显示图像。
THanks Jim,我正在使用一个Rect对象来包含我的位图,有没有一种方法来旋转和缩放包含我的位图的Rect?这样我就不必乱用像素数据本身。 – kingrichard2005 2010-04-08 07:02:56
绝对有,但我看不到你所有的代码,这取决于你如何显示位图。 – 2010-04-08 08:59:28
再次感谢Jim,基本上我的设置方式是我有一个名为AnimatedSprite的类,它使用Rect来包含我试图进行动画处理的Bitmap对象。我现在正在工作,但我会尝试发布一些相关代码参数以获得反馈。 – kingrichard2005 2010-04-08 17:54:34