简体   繁体   English

在Android中调整和旋转图像

[英]Resizing and rotating an image in Android

I'm trying to rotate and resize an image in Android. 我正在尝试在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; 我尝试使用0到.99之间的其他值,但是当我减小比例值时,图像只会变得更加像素化,但是在视觉上它保持相同的大小。 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. 目前尚不确定该怎么办,我们将不胜感激。

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. 为了减小屏幕尺寸,您必须调整ImageView上的参数,或使用任何用于显示图像的技术。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM