tags:

views:

560

answers:

1

I'm trying to rotate and resize an image in Android. The code I have is as follows, (taken from this tutorial):

UPDATE: More code added below for more constructive feedback

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

}

The image seems to rotate just fine, but it doesn't scale like I expect it to. I've tried different values between 0 and .99, but the image only gets more pixelized as I reduce the scale values, but it remains that same size visually; whereas the goal I'm trying to achieve is to actually shrink it visually. I'm not sure what to do at this point, any help is appreciated.

+1  A: 

What you're doing is manipulating the pixel data from a bitmap. You're reducing the number of pixels in the image, but it sounds like (because we can't see this code) as if the bitmap is being scaled to fit the same size on the screen. In order to reduce the size on the screen, you'll have to adjust parameters on your ImageView, or whatever technique you are using to display the image.

Jim Blackler
THanks Jim, I'm using a Rect object to contain my Bitmap, is there a way to just rotate and scale the Rect the is containing my bitmap?? That way I don't have to mess with the pixel data itself.
kingrichard2005
Absolutely there is, but I can't see all your code, and it depends on how you are displaying the bitmap.
Jim Blackler
Thanks again Jim, basically the way I have it setup is I have a class called AnimatedSprite which uses a Rect to contain the Bitmap object that I'm trying to animate. I'm at work right now, but I'll try and post some of the relevant code parameters for feedback.
kingrichard2005