在Android中调整图像大小并旋转图像

时间:2010-04-08 06:09:22

标签: java android

我正在尝试在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之间的不同值,但是当我减小比例值时,图像只会变得更像素化,但它在视觉上保持相同的大小;而我想要实现的目标实际上是在视觉上缩小它。我不知道该做什么,感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

您正在做的是操纵位图中的像素数据。您正在减少图像中的像素数,但听起来像(因为我们看不到此代码),就像位图被缩放到屏幕上的相同大小一样。为了减小屏幕尺寸,您必须调整ImageView上的参数,或者用于显示图像的任何技术。