繁体   English   中英

在Android上旋转图片而不缩小

[英]Rotate image without shrinking on Android

我需要在正在使用的应用程序上创建一个指南针。 因此,我尝试创建一个名为CompassView的新视图,该视图基本上扩展了imageview,显示了一个指向东西南北指向的位图,使用传感器来查找手机所指向的度,并相应地旋转图像以便创建一个实际的指南针。 但是问题是,如果我尝试将图像旋转到某些角度(例如45度),则会缩小图像。 这里有一些图片可以更好地解释它。

这个是正常的

这缩小了

如您所见,当我尝试绕45度旋转时,第二张图像缩小了。我要这样做的是: 这个

这是我当前正在使用的代码:

        Bitmap bMap = BitmapFactory.decodeResource(getResources(),
                R.drawable.compass);
        Matrix xMatrix = new Matrix();
        xMatrix.reset();
        xMatrix.postRotate(360-mValue, 75, 75); //This is 75 because 150 is the image width
        Bitmap bMapRotate = Bitmap.createBitmap(bMap, 0, 0,
                bMap.getWidth(), bMap.getHeight(), xMatrix, true);
        setImageBitmap(bMapRotate);

任何帮助,将不胜感激。 谢谢

编辑:(解决方案)感谢接受的答案,我终于使它正常工作。 这是我用于想要了解其工作原理的任何人的代码:

RotateAnimation rAnimAntiClockWise = new RotateAnimation(
                    360 - mValue, 360 - event.values[0],
                    Animation.RELATIVE_TO_SELF, 0.5f,
                    Animation.RELATIVE_TO_SELF, 0.5f);
//mValue is the angle in degrees and i subtracted it from 360 to make it anticlockwise, and event.values[0] is the same thing as mValue
rAnimAntiClockWise.setFillAfter(true);
rAnimAntiClockWise.setInterpolator(new LinearInterpolator());
rAnimAntiClockWise.setDuration(0);
startAnimation(rAnimAntiClockWise);

您可以使用其他技巧,其作用与旋转一样,并且不会调整图像大小。 我实际上以45度角旋转图像,并在动画后保留更改。

rAnimAntiClockWise = new RotateAnimation(0.0f, 45.0f,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                0.5f);
        rAnimAntiClockWise.setFillAfter(true);
        rAnimAntiClockWise.setInterpolator(new LinearInterpolator());       
            bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.rotate);     
        rAnimAntiClockWise.setDuration(100);
        img_rotate.startAnimation(rAnimAntiClockWise);

问题是由于源的角“突出”,您的新图像实际上更大,因此视图将其缩小以适合。

一些可能的方法:

  1. 在上面的代码之后,调用Bitmap.createBitmap(Bitmap source, int x, int y, int width, int height) ,复制正确大小的中心区域。 给定您拥有的代码,这很容易,但是会创建一个无用的中间位图。

  2. 无需将转换图像和源图像提供给createBitmap,只需创建正确大小的可变位图,将其包装在Canvas中,并告诉Canvas渲染旋转的图像。

     bMapRotate = Bitmap.createBitmap( bMap.getWidth(), bMap.getHeight(), bMap.getConfig()); Canvas canvasRotate = new Canvas(bMap); canvasRotate.drawBitmap(bMap, xMatrix, paint); // any opaque Paint should do 
  3. 保留代码,但在渲染时告诉视图裁剪而不是缩放。

暂无
暂无

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

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