【问题标题】:Resizing and rotating an image in Android在 Android 中调整图像大小和旋转图像
【发布时间】: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 之间的不同值,但是当我减小比例值时,图像只会变得更加像素化,但在视觉上它仍然是相同的大小;而我试图实现的目标是在视觉上实际缩小它。我现在不知道该怎么做,感谢任何帮助。

【问题讨论】:

    标签: java android


    【解决方案1】:

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

    【讨论】:

    • 谢谢吉姆,我正在使用 Rect 对象来包含我的位图,有没有办法旋转和缩放包含我的位图的 Rect?这样我就不必弄乱像素数据本身了。
    • 绝对有,但我看不到你所有的代码,这取决于你如何显示位图。
    • 再次感谢 Jim,基本上我设置它的方式是我有一个名为 AnimatedSprite 的类,它使用一个 Rect 来包含我试图制作动画的位图对象。我现在正在工作,但我会尝试发布一些相关的代码参数以供反馈。
    猜你喜欢
    • 1970-01-01
    • 2017-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 2016-12-26
    • 2018-07-20
    • 2018-07-27
    相关资源
    最近更新 更多